How to set the same xtick for all subplots in python? - python

I'm creating subplots. I would like to set the same xtick for all subplots. I was able to set the xlabel in common for all subplots but I really don't know how to do for xticks. Any help?
fig, axs = plt.subplots(2, 2)
axs[0,0].plot(np.float64(datatime),np.float64(Tm),'--',color='black')
axs[0,0].set_ylim([min(Tm)-10,max(Tm)+10])
axs[0,0].set_ylabel('Temp. [°C]')
axs[0,1].plot(np.float64(datatime),np.float64(precip),'--',color='black')
axs[0,1].set_ylim([min(precip),max(precip)+20])
axs[0,1].set_ylabel('Rainfall [mm]')
axs[1,0].plot(np.float64(datatime),np.float64(PET),color='magenta')
axs[1,0].set_ylim([min(PET),max(PET)+10])
axs[1,0].set_ylabel('PET [mm]')
axs[1,1].plot(np.float64(datatime),np.float64(delta),color='cyan')
axs[1,1].set_ylim([min(delta),max(delta)+10])
axs[1,1].set_ylabel('P-PET [mm]')
plt.xticks(np.arange(min(datatime), max(datatime)+1, 12)) #here i define xticks
for ax in axs.flat:
ax.set(xlabel='Time [months]')
plt.show()

Within the for loop you can set the same ticks for each subplot
for ax in axs.flat:
ax.set(xlabel='Time [months]')
ax.set_xticks(np.arange(min(datatime), max(datatime)+1, 12))

Related

Move ticks and labels to the top of a pyplot figure

As per this question, moving the xticks and labels of an AxesSubplot object can be done with ax.xaxis.tick_top(). However, I cannot get this to work with multiple axes inside a figure.
Essentially, I want to move the xticks to the very top of the figure (only displayed at the top for the subplots in the first row).
Here's a silly example of what I'm trying to do:
fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True)
fig.set_figheight(5)
fig.set_figwidth(10)
for ax in axs.flatten():
ax.xaxis.tick_top()
plt.show()
Which shows
My desired result is this same figure but with the xticks and xticklabels at the top of the two plots in the first row.
Credits to #BigBen for the sharex comment. It is indeed what's preventing tick_top to work.
To get your results, you can combine using tick_top for the two top plots and use tick_params for the bottom two:
fig, axs = plt.subplots(2, 2, sharex=False) # Do not share xaxis
for ax in axs.flatten()[0:2]:
ax.xaxis.tick_top()
for ax in axs.flatten()[2:]:
ax.tick_params(axis='x',which='both',labelbottom=False)
See a live implementation here.

Trying to plot 2 charts side-by-side, but one of them always comes out empty

I have two plots that I generated from my data:
Here the second plot shows the distribution of results from the first one.
What I want is to plot them side-by-side so you could see both the data and the distribution on the same plot. And I want plots to share y-axis as well.
I tried to do the following:
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(40, 15), sharey=True)
ax1 = sns.lineplot(plotting_df.index, plotting_df.error, color=('#e65400'), lw=2, label='random forest residual error')
ax1 = sns.lineplot(plotting_df.index, plotting_df.val, color=('#9b9b9b'), lw=1, label='current model residual error')
ax1 = sns.lineplot(plotting_df.index, 0, color=('#2293e3'), lw=1)
ax1.xaxis.set_visible(False)
ax1.set_ylabel('Residual Fe bias', fontsize=16)
ax1.set_title('Models residual error comparison', fontsize=20, fontweight='bold')
sns.despine(ax=ax1, top=True, bottom=True, right=True)
ax2 = sns.distplot(results_df.error, hist=True, color=('#e65400'), bins=81,
label='Random forest model', vertical=True)
ax2 = sns.distplot(plotting_df.val, hist=True, color=('#9b9b9b'),
bins=81, label='Rolling averages model', vertical=True)
ax2.set_title('Error distribution comparison between models', fontsize=20, fontweight='bold')
sns.despine(ax=ax2, top=True, right=True)
fig.savefig("blabla.png", format='png')
But when I do run it I get strange results - the first chart is in the second column, whereas I wanted it on the left and the second chart is completely blank. Not sure what I did wrong here.
Both lineplot and distplot accept a matplotlib axes object as an argument, which tells it which axes to plot onto. If no axes is passed into it, then the plot is placed onto the current axes.
You create a figure and 2 axes using :
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(40, 15), sharey=True)
Therefore, ax2 will be the current axes. So your distplot is being plotted on top of your lineplot, both in ax2.
You need to pass the axes into the seaborn plotting functions.
sns.lineplot(..., ax=ax1)
sns.distplot(..., ax=ax2)

Hide ticks in one of many axes from subplots in Matplotlib

In the following plot
f, (ax1,ax2) = plt.subplots(2,1, figsize=(7,8), sharex=True,
gridspec_kw={'height_ratios':[3,1],'hspace':0.05})
I would like to hide ax1.xticks but show ax2.xticks.
With ax1.set_xticks([]) I end up hiding ax1 and ax2 ticks.
found it!
ax1.tick_params(bottom='off')
do the job!

Rotate existing axis tick labels in Matplotlib

I start with tree plots:
df = pd.DataFrame([1,20,3],[2,30,4],[3,40,5],columns=['mean','size','stat'])
fig,[ax1,ax2,ax3] = plt.subplots(1, 3, sharey=True)
ax1.barh(np.arange(len(df)),df['mean'].values, align='center')
ax2.barh(np.arange(len(df)),df['size'].values, align='center')
ax3.barh(np.arange(len(df)),df['stat'].values, align='center')
Is there a way to rotate the x axis labels on all three plots?
When you're done plotting, you can just loop over each xticklabel:
for ax in [ax1,ax2,ax3]:
for label in ax.get_xticklabels():
label.set_rotation(90)
You can do it for each ax your are creating:
ax1.xaxis.set_tick_params(rotation=90)
ax2.xaxis.set_tick_params(rotation=90)
ax3.xaxis.set_tick_params(rotation=90)
or you do it inside a for before showing the plot if you are building your axs using subplots:
for s_ax in ax:
s_ax.xaxis.set_tick_params(rotation=90)
df = pd.DataFrame([1,20,3],[2,30,4],[3,40,5],columns=['mean','size','stat'])
fig,[ax1,ax2,ax3] = plt.subplots(1, 3, sharey=True)
plt.subplot(1,3,1)
barh(np.arange(len(df)),df['mean'].values, align='center')
locs, labels = xticks()
xticks(locs, labels, rotation="90")
plt.subplot(1,3,2)
barh(np.arange(len(df)),df['size'].values, align='center')
locs, labels = xticks()
xticks(locs, labels, rotation="90")
plt.subplot(1,3,3)
barh(np.arange(len(df)),df['stat'].values, align='center')
locs, labels = xticks()
xticks(locs, labels, rotation="90")
Should do the trick.
Here is another more generic solution: you can just use axes.flatten() which will provide you with much more flexibility when you have higher dimensions.
for i, ax in enumerate(axes.flatten()):
sns.countplot(x= cats.iloc[:, i], orient='v', ax=ax)
for label in ax.get_xticklabels():
# only rotate one subplot if necessary.
if i==3:
label.set_rotation(90)
fig.tight_layout()

Matplotlib subplots: legend and axis-scale

I am plotting 4 subplots (i.e 2 rows 2 columns) in this way:
fig1= plt.figure(figsize=(8,6))
ax1 = fig1.add_subplot(221)
ax1.errorbar((r1),(xi1),fmt='',yerr=(low_err_1,upp_err_1),ls='none',color='black')
ax1.scatter((r1),(xi1),c='red',marker="o",s=30,label= r'$\xi(r)$ $0.0<z<0.5$')
ax1.plot((r1),(curve_y_1),'--',label='fit $0.0<z<0.5$')
ax1.set_xscale('log')
ax1.set_yscale('log')
ax2 = fig1.add_subplot(222)
ax2.errorbar((r2),(xi2),fmt='',yerr=(low_err_2,upp_err_2),ls='none',color='black')
ax2.scatter((r2),(xi2),c='blue',marker="o",s=30,label=r'$\xi(r)$ $0.5<z<1.0$')
ax2.plot((r2),(curve_y_2),'--',label='fit $0.5<z<1.0$')
ax2.set_xscale('log')
ax2.set_yscale('log')
ax3 = fig1.add_subplot(223)
ax3.errorbar((r3),(xi3),fmt='',yerr=(low_err_3,upp_err_3),ls='none',color='black')
ax3.scatter((r3),(xi3),c='yellow',marker="o",s=30,label=r'$\xi(r)$ $1.0<z<1.5$')
ax3.plot((r3),(curve_y_3),'--',label='fit $1.0<z<1.5$')
ax3.set_xscale('log')
ax3.set_yscale('log')
ax4 = fig1.add_subplot(224)
ax4.errorbar((r4),(xi4),fmt='',yerr=(low_err_4,upp_err_4),ls='none',color='black')
ax4.scatter((r4),(xi4),c='black',marker="o",s=30,label=r'$\xi(r)$ $1.5<z<2.0$')
ax4.plot((r4),(curve_y_4),'--',label='fit $1.5<z<2.0$')
ax4.set_xscale('log')
ax4.set_yscale('log')
My questions are:
Is there a way to add legends to all these subplots using a single (common) command, instead of typing ax1.legend(loc = 'best'), ax2.legend(loc = 'best') and so on separately for each subplot?
I would like to set log-scaling for each subplot using a single (common) command. As you can see, now I am setting the axis-scales separately to log for each subplot.
Just define a axes formatting function:
def style_ax(ax):
ax.legend(loc='best')
ax.set_yscale('log')
ax.set_xscale('log')
And than call it when finished:
for ax in [ax1, ax2, ax3, ax4]:
style_ax(ax)

Categories

Resources