0

currently my x tick label is the index but I need it to be a column, when I try to change it, the values ​​are compressed in an area of ​​the graph

[graph]

1

my code:

x = df_num['Date'].tolist()
plt.figure(figsize=(16, 6))
fig.tight_layout()
plt.plot(df_num[target_feature])
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
plt.title("Set X labels in Matplotlib Plot")
plt.xticks(x)
plt.figure(figsize=(16, 6))
plt.show()

thanks for your answers, i found the right one:

x = df_num['Date'].tolist()
labels = []
ticks = []

for i in range(0, len(x), 5000):
    #print(x[i])
    ticks.append(i)
    labels.append(x[i])
    
plt.figure(figsize=(16, 6))
fig.tight_layout()
plt.plot(df_num[target_feature])
#plt.xlabel("X-Axis")
#plt.ylabel("Y-Axis")
plt.title(target_feature)
plt.xticks(ticks=ticks, labels=np.array(labels))
#plt.xticks(np.arange(0, 130825, step=5000))
plt.xticks(rotation=90)
plt.show()

works

  • 1
    Welcome to SO. Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples), then edit your question accordingly. Most likely, neither your plot values (which are seemingly an index 0, 1, 2....) nor your xtick labels (which are seemingly strings) are datetimes as suggested by your column label, leading to your problems. Why don't plot the date column on the x-axis `plt.plot(df_num['Date'], df_num[target_feature])`? – Mr. T Mar 08 '22 at 18:13

0 Answers0