Oct-06-2016, 10:53 AM
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:
iFunk
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()regardsiFunk
