Quantcast

Subplots problem: Last tick-formatting not working

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Subplots problem: Last tick-formatting not working

lorny
This post was updated on .
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:
EDIT: Added image and sorry for the html-copy-paste, added the tab-spacing.

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)

Looks like this...
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Subplots problem: Last tick-formatting not working

Nicolas P. Rougier

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 <[hidden email]> 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
> [hidden email]
> https://mail.python.org/mailman/listinfo/matplotlib-users

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Subplots problem: Last tick-formatting not working

lorny
Wow, thanks a lot, Nicolas!
I was quite blind - just added once the plt.yscale at the end, because it was sometimes changing automatically on "log"-scale.
Now I did the change and it worked

Regards, Jonas
Loading...