Python Forum
matplotlib barh y tick alignment - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: matplotlib barh y tick alignment (/thread-349.html)



matplotlib barh y tick alignment - iFunKtion - Oct-06-2016

Hi,

Can't seem to get the y tick labels to line up nicely on the horizontal bar chart in this sub plotted chart (bottom chart). I have found various references to alignment but can't get anything to work, here is the code, the data is a simple two column delimited text file comprising of a name and a number from 1 to 6 delimited with a comma and no white space. The Second chart, the one in question, simply takes the total of all the values in the upper graph and adds them together, I hope it's fairly obvious:
#!/usr/bin/python3 # aniMat.py # Testing out updating live charts with MatPlotLib library. import matplotlib.pyplot as plt import matplotlib.animation as animation from matplotlib import style style.use('ggplot')    # style sheet used as grid is handy troubleshooting info. fig = plt.figure() ax1 = plt.subplot2grid((8,1), (0,0), rowspan=3, colspan=1) ax2 = plt.subplot2grid((8,1), (4,0), rowspan=1, colspan=1) bar_width = 0.6 def animate(i):     # Data is taken from simple two column text file of a string and an in     # separated by a comma and no white space. String data is pulled into xar_labels     # and an integer range has been generated for the initial x-axis.     pullData = open('./data_sets/sample.txt', 'r').read()     dataArray = pullData.split('\n')     xar = []     xar_labels = []     yar = []     xhar = [0, 1]     yhar = [40, 0]     t_used  = 0     n = 0     rflag = False     aflag = False     # rag refers to 'red', 'amber', 'green' status, color 'orange is used but label remains 'amber'     # Chart is designed to quickly see if any one customer goes above the allocation.     rag_array = []     for eachLine in dataArray:         if len(eachLine)>1:             x, y = eachLine.split(',')             xar_labels.append(str(x))             xar.append(int(n))             print(xar)             print(xar_labels)             yar.append(int(y))             t_used = t_used + int(y)             n = n + 1             if int(y) < 4:                 rag = 'green'             elif int(y) == 4:                 rag = 'orange'                 aflag = True             else:                 rag = 'red'                 rflag = True             rag_array.append(rag)     ax1.clear()     ax2.clear()     ax1.bar(xar, yar, bar_width, align='center', color=rag_array)            # Put align parameter in here following matplotlib documentation examples     ax1.set_xticks(xar, xar_labels)     ax1.set_title('Tyres')     ax1.set_ylim(0, 6)     ax1.set_ylabel('Tyres Used')     ax1.set_xlabel('VIN Number')         yhar = [40, t_used]     if rflag:         rag_sets = 'red'     elif aflag:         rag_sets = 'orange'     else:         rag_sets = 'green'        ax2.barh(xhar, yhar, color=['grey', rag_sets])     ax2.set_xlabel('Number of Sets')     ax2.set_xlim(0, 50)     ax2.set_ylim(0, 2)   ani = animation.FuncAnimation(fig,animate, interval=1000) plt.show()
regards
iFunk


RE: matplotlib barh y tick alignment - Larz60+ - Oct-06-2016

And here's the post for Y axis:
http://stackoverflow.com/questions/23637219/center-align-tick-labels-of-matplotlib-heatmap


RE: matplotlib barh y tick alignment - iFunKtion - Oct-06-2016

Yes I found that, but it covers the x axis which is fine, not the y axis which is causing the problem, the tick marks line up to the top of the bar, not the middle of it and looks a bit strange.

Ok, I've found solved this/found workaround for this. It's not the most intuitive, but I reset the xticks to [0,1] instead of [0,2] with the view that having the tick labels below the bar on a horizontal chart would look better, and then the align parameter worked:
    ax2.barh(xhar, yhar, align='center', color=['grey', rag_sets])         # align parameter now works     ax2.set_xlabel('Number of Sets')     ax2.set_xlim(0, 50)     ax2.set_yticks([0, 1])                                                                     # so long as the ticks are set in this way     ax2.set_yticklabels(tyreStock)
iFunk


This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.