简体   繁体   中英

X-ticks in a subplots line graph are different python plot

I use the same code for multiple graphs except for the graph type. The X-ticks for the first subplot are ok but as you see below image other two subplots x-ticks got shrink even though the graph line shows exact locations. What might be the solution?

# Library Loading
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import numpy as np
import pandas as pd


df_1 = pd.DataFrame({
    "A":[22.34,25.10,37.57,44.96,53.09],
    "B":[18.42,31.09,33.66,41.73,51.31],})

df_2 = pd.DataFrame({
    "A":[44.85,44.44,44.43,44.06,45.01],  
    "B":[48.46,44.59,43.51,44.42,45.38],})

df_3= pd.DataFrame({
    "A":[2.88,6.58,8.35,14.77,20.21],
    "B":[1.76,5.25,9.18,12.47,19.22],})


plt.figure(num=1)
fig, (ax,ax1,ax2) = plt.subplots(3,1,figsize=(8,12))
# twin_x = ax.twinx() # Create a pseudo axes based off of the original
fig.subplots_adjust(hspace=0.5)

#************** Graph in First row ************************
# Put the bar plot on the "primary y" via ax=ax
df_1[['A','B']]. \
plot(kind='bar',color=['purple','steelblue'], ax=ax, zorder=1,legend=True, width=.40)
ax.grid(True, zorder=0)
ax.set_axisbelow(True)
ax.set_xticklabels(('10', '20','30', '40', '50'),Rotation=360)

# X and Y axis label
ax.set_xlabel('Number of P',fontsize=12)
ax.set_ylabel('(%)',fontsize=12)

#************** Graph in Second row ************************
# Put the bar plot on the "primary y" via ax=ax
df_2[['A','B']]. \
plot(kind='line',color=['purple','steelblue'], ax=ax1, zorder=2,legend=True,marker='*')
ax1.grid(True, zorder=2)
ax1.set_axisbelow(True)
ax1.set_xticklabels(('10', '20','30', '40', '50'),Rotation=360)

# X and Y axis label
ax1.set_xlabel('Number of P',fontsize=12)
ax1.set_ylabel(' (%)',fontsize=12)

#************** Graph in Third row ************************
# Put the bar plot on the "primary y" via ax=ax
df_3[['A','B']]. \
plot(kind='line',color=['purple','steelblue'], ax=ax2, zorder=3,legend=True, marker='o')


ax2.grid(True, zorder=3)
ax2.set_axisbelow(True)
ax2.set_xticklabels(('10', '20','30', '40', '50'),Rotation=360)

# X and Y axis label
ax2.set_xlabel('Number of P',fontsize=12)
ax2.set_ylabel('Time (Seconds)',fontsize=12)

plt.show()

Below is the image out of the code for your reference. 在此处输入图片说明

That's because the latter two plots are line plots, the x-axes are numerical, matplotlib will automatically scale xticks . In this case, xticks for the latter two are [0,0.5,1,...] vs 0,1,2,3,4 in the first plot.

One simple thing you can do is pass sharex=True to subplots :

fig, (ax,ax1,ax2) = plt.subplots(3,1,figsize=(8,12), sharex=True)

And you get:

在此处输入图片说明

Or you can manually force the xticks for the line plots:

# other code
ax2.set_xticks(df_2.index)
ax2.set_xticklabels(('10', '20','30', '40', '50'),Rotation=360)

# same for `ax3`

and you get:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM