Home > Mobile >  How to plot all the xaxis values in a df.plot?
How to plot all the xaxis values in a df.plot?

Time:04-06

I have this df:

      CODE         MONTH    PP
24    000136       Enero   57.9
25    000136     Febrero  124.3
26    000136       Marzo  147.7
27    000136       Abril   71.5
28    000136        Mayo   13.2
     ...         ...    ...

I'm plotting the figure with this code:

fig = plt.figure('Graphic', figsize=(30,15), dpi=150)
    
ax1 = fig.add_axes([0.2, 0.25, 0.60, 0.60])

df.plot(x='MONTH',y='PP',style='--o',color='black',linewidth=8,marker='o',markersize=12, ax=ax1)

ax1.xaxis.set_major_locator(MaxNLocator(min_n_ticks=12))

I want to see all the 12 values of the months in my xaxis figure but i only got 6 of them in the figure.

I tried with ax1.xaxis.set_major_locator(MaxNLocator(min_n_ticks=12)) but it only plot the ticks, not the values in the xaxis.

Do you know how can i plot all the xaxis values? I can only use a df.plot

Thanks in advance.

CodePudding user response:

The x-axis scale is automatically displayed, so add the number of pieces you wish to display to the x-axis setting. This will display 12 months. The missing data is added as appropriate.

import matplotlib.pyplot as plt

fig = plt.figure('Graphic', figsize=(20,10), dpi=150)
    
ax1 = fig.add_axes([0.2, 0.25, 0.60, 0.60])

df.plot(x='MONTH',y='PP',style='--o',color='black',linewidth=8,markersize=12, xticks=range(12), ax=ax1)
plt.show()

enter image description here

  • Related