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

lorny widmer.j at gmail.com
Thu Apr 6 04:21:37 EDT 2017


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: http://matplotlib.1069221.n5.nabble.com/Subplots-problem-Last-tick-formatting-not-working-tp47986.html
Sent from the matplotlib - users mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20170406/0ed819c6/attachment.html>


More information about the Matplotlib-users mailing list