Home > database >  Setting X axis label name more frequently in Matplotlib
Setting X axis label name more frequently in Matplotlib

Time:04-15

I have a dataframe like this

    Time(DDHHMM)    True AOG    Predicted AOG
5184    28:0:0.0    4.0         4.697324
5185    28:0:5.0    10.0        0.366948
5186    28:0:10.0   6.0         6.158011
5187    28:0:15.0   9.0         8.388459
5188    28:0:20.0   24.0        23.883515
... ... ... ...

5759    29:23:55.0  8.0 8.124675

I am trying to plot them using following code

dataframe.plot(x='Time(DDHHMM)', y=['True AOG','Predicted AOG'],kind="line", figsize=(14,7),marker='.',grid=True)
plt.title("Georgia: true and predicted data at Phase 2 (EB-WB), 5 min")
plt.xlabel('Time',labelpad=2)
plt.ylabel('AOG')
plt.legend(loc='best',fancybox=True)
plt.grid(True)
plt.xlim(0,288)
plt.ylim(0,80)
plt.show() 

And getting figure where there are only 3 x-axis value names. enter image description here However, I want to add more x-axis value names but did not find anything in the documentation that would work. I tried using xticks(start,end,step) but it doesnt change anything. Also I tried plot.locator_params(axis='x', nbins=20) from one of the stackoverflow issues which is not working too. How could I fix this?

CodePudding user response:

Before plt.show(), try inserting this code:

# change n to satisfy your needs
n = 3
ticks = [t for t in range(len(df)) if t % n == 0]
labels = [l for i, l in enumerate(df["Time(DDHHMM)"]) if i % n == 0]
plt.xticks(ticks, labels)
  • Related