Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can't get xticks to work
#1
Hi,

having a little difficulty getting xticks to work, but I can't find syntax for what I am doing. As it happens, I think the syntax is accepted by the interpreter, it's just that I haven't seem to put the parameters in correctly. As my data has strings as the xaxis, I have created a range of integers as Matplotlib does not seem to support the use of strings for this. The two print statements are just to make sure that I have two lists one of integers and the other of the names taken from the data:
#!/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')

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):
    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_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)
    ax1.set_xticks(xar, xar_labels)

    
    ax1.set_title('P1GTR 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()
I was hoping to be able to attach the text file I am drawing the data from, but couldn't work out how to do that on here, essentially, these are anywhere from 8 - 40 customers, so the x axis range is only ever going to be between 8 and 40 lines. The data I have taken is 8 lines of text in the following format of a name, a comma and a number from 1 to 5:
    James,2
    Paul,1
    etc.
If there is a simpler way to do this that would really help, as I am not confident that my code is going to get very slow over time. Eventually, all the data will be taken straight from a NoSQL database if I can work out how to do it.

Regards
iFunc

Hi,

I have solved this, I had the set_xticks, but not the set_xticks labels, that part of the code now looks like this and works;
ax1.clear()
ax1.bar(xar, yar, bar_width, align='center', color=rag_array)
ax1.set_xticks(xar)
ax1.set_xticklabels(xar_labels)
...
Kind regards
Rob
Reply
#2
Here's an example that may be useful
http://stackoverflow.com/questions/17129...matplotlib
Reply


Forum Jump:

User Panel Messages

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