Python Forum
matplotlib barh y tick alignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
matplotlib barh y tick alignment
#1
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
Reply
#2
And here's the post for Y axis:
http://stackoverflow.com/questions/23637...ib-heatmap
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code for alignment and font size 1418 0 304 Jan-14-2024, 03:56 AM
Last Post: 1418
  make all text in Right to left alignment in tkinter widgets jalal0034 1 1,309 Sep-27-2022, 06:42 PM
Last Post: Larz60+
  Right to left alignment in python report using Reportlab jalal0034 1 1,820 Sep-27-2022, 04:25 AM
Last Post: jalal0034
  Points not plotting with reference to x-tick labels Mark17 2 1,269 Jun-14-2022, 05:38 PM
Last Post: Mark17
  How to get evenly-spaced datetime tick labels regardless of x-values of data points? Mark17 4 5,215 Apr-04-2022, 07:10 PM
Last Post: Mark17
  Floor division problem with plotting x-axis tick labels Mark17 5 2,084 Apr-03-2022, 01:48 PM
Last Post: Mark17
  pandas, tabulate, and alignment menator01 3 7,244 Feb-05-2022, 07:04 AM
Last Post: menator01
  Space between list and column alignment rturus 8 5,075 Mar-17-2021, 04:47 PM
Last Post: rturus
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,208 Mar-11-2021, 10:52 AM
Last Post: buran
  Only show every nth tick in chart tgottsc1 1 4,650 Feb-01-2021, 04:30 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020