[Matplotlib-users] Subplots problem: Last tick-formatting not working

Nicolas P. Rougier Nicolas.Rougier at inria.fr
Thu Apr 6 07:33:23 EDT 2017


I think it comes from the line plt.yscale('linear') that will act on the last ax (because of plt.).
Instead, you can use ax.set_yscale('linear') *before* calling ticklabelformat().

Nicolas


> On 6 Apr 2017, at 10:21, lorny <widmer.j at gmail.com> wrote:
> 
> Hi, I'm new here and also quite a python/matplotlib-noob (as you will be able to recognize in my code-example).
> I have a problem with plt.subplots: When I plot any amount of bars (or different graphs/diags) in a for-loop through the subplots, and I try to format the yaxis-ticklabels (using the ticker module), the ticklabels are adapting all the subplots except the last subplot yaxis
> Anyone any ideas or hints?
> Thanks a lot for any support!
> Best regards
> 
> Here is the code:
> import matplotlib.pyplot as plt
> import matplotlib.ticker as ticker
> import numpy as np
> from sympy import *
> from random import randint
> 
> init_printing(use_unicode=True)
> np.seterr(all='ignore')
> 
> 
> def graph(numsubplots):
> fig1, axarr = plt.subplots(numsubplots, ncols=1, num="Subplot Problem", figsize=(10, 15), dpi=100)
> fontsize = 16
> 
> for i in range(0, len(axarr)):
> ax = axarr[i]
> n = 5
> indx = np.arange(1, n+1)
> width = randint(1, 5)/10
> ax.barh(indx - width/2, [4, 7, 2, 1, 9], width, color='y', label="Me")
> ax.barh(indx + width/2, [3, 3, 0, 3, 3], width, color='r', label="The other")
> ax.set_xlabel('Hours', fontsize=fontsize)
> ax.set_ylabel('Day', fontsize=fontsize)
> ticklabelformat(ax, which='y', step=1, formating='day %d')
> 
> ax.set_title('Hours working a day', fontsize=fontsize)
> ax.legend(fontsize=fontsize)
> ax.grid(True)
> 
> for i in fig1.axes:
> plt.setp(i.get_xticklabels(), visible=True, rotation=30)
> plt.yscale('linear')
> plt.tight_layout()
> fig1.subplots_adjust(hspace=0.3)
> plt.show()
> 
> 
> def ticklabelformat(axe, which='xy', step=1, formating='%d'):
> majorlocator = ticker.MultipleLocator(step)
> majorformatter = ticker.FormatStrFormatter(formating)
> if which == 'y':
> axe.yaxis.set_major_locator(majorlocator)
> axe.yaxis.set_major_formatter(majorformatter)
> elif which == 'x':
> axe.xaxis.set_major_locator(majorlocator)
> axe.xaxis.set_major_formatter(majorformatter)
> else:
> axe.yaxis.set_major_locator(majorlocator)
> axe.yaxis.set_major_formatter(majorformatter)
> axe.xaxis.set_major_locator(majorlocator)
> axe.xaxis.set_major_formatter(majorformatter)
> 
> 
> if __name__ == "__main__":
> graph(3)
> 
> 
> View this message in context: Subplots problem: Last tick-formatting not working
> Sent from the matplotlib - users mailing list archive at Nabble.com.
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users



More information about the Matplotlib-users mailing list