Tengo el siguiente código:
import matplotlib.pyplot as plt import numpy as np import pandas as pd data = pd.read_csv("Ari_atlag.txt", sep = '\t', header = 0) #Num_array = pd.DataFrame(data).to_numpy() print(data.head()) data.plot() #data.columns = ['Date', 'Number_of_test', 'Avarage_of_ARI'] #print(Num_array) plt.show()Producción:
Date Number_of_test Avarage_of_ARI 0 2011-01 22 0.568734 1 2011-02 5 0.662637 2 2011-03 0 0.000000 3 2011-04 3 0.307692 4 2011-05 6 0.773611 Process finished with exit code 0y la trama.
Pero con este código en la trama, el eje x es el índice. Pero quiero obtener la Fecha en el eje x.
Cómo trazar la Fecha con Número_de_prueba y Fecha con Avarage_of_ARI
Creo que de alguna manera debería cambiar la cadena (fecha) a las fechas, pero no sé cómo hacerlo.
Mejor
Prueba esto :
#Excutable example df = pd.DataFrame({"Date" : ["2011-01", "2011-02", "2011-03", "2011-04", "2011-05"], "Number_of_test" : [22, 5, 0, 3, 6], "Avarage_of_ARI" : [0.568734, 0.662637, 0.000000, 0.307692, 0.773611]}) df.Date = pd.to_datetime(df.Date) df = df.set_index("Date")Gráfico
plt.style.use('ggplot') plt.rcParams['figure.figsize'] = [13,5] data_ax1 = df.Number_of_test data_ax2 = df.Avarage_of_ARI fig, ax1 = plt.subplots() ax1.set_ylabel('Number_of_test', color = 'tab:red') ax1.plot(data_ax1, color = 'tab:red', linewidth= 1) ax1.tick_params(axis = 'y', labelcolor= 'tab:red', length = 6, width = 1, grid_alpha=0.5) ax2 = ax1.twinx() ax2.set_ylabel('Avarage_of_ARI', color = 'tab:blue') ax2.plot(data_ax2, color = 'tab:blue', linewidth = 1) ax2.tick_params(axis='y', labelcolor = 'tab:blue') fig.tight_layout() plt.title("Plot", fontsize = 15)Puede establecer el índice en la columna de fecha si eso funciona para sus datos.
cols = ['Number_of_test','Avarage_of_ARI'] data.set_index('Date', inplace = True) for c in cols: data[c].plot()