matplotlib questions

MRAB python at mrabarnett.plus.com
Sat Aug 28 19:17:45 EDT 2021


On 2021-08-28 04:39, Steve wrote:
> I would like to know how the data is placed on the Y-axis and at the tops of
> the bars.
> The data is not being shown properly.  With some exceptions, it looks as if
> the data is getting sorted independently from the dates.
> 
> 
> OK, here is the code:
> ==============================================
> # https://matplotlib.org/stable/gallery/index.html
> import matplotlib.pyplot as plt
> import numpy as np
> 
> width = 12 #Width of the graph
> height = 6  #Height of the graph
> 
> plt.rcParams["figure.figsize"] = (width,height)
> plt.rcParams["font.size"] = (9.0)
> 
> 
> Count = 0
> datesList = []   # Each date showing end of bottle use
> hoursList = []   # Number of hours
> # daysList = []  # Number of days calculated from hours/24
> countList = []   # simple tally
> 
> with open("__Insulin_Tracker.txt", 'r') as infile:
>          for lineEQN in infile:
>            insulinHours = lineEQN[47:52].strip()
>            print("Hours = " + insulinHours)
>            hoursList.append(insulinHours)
>            
>            insulinDates = lineEQN[20:30].strip()
>            datesList.append(insulinDates)
>            
>        #    insulinDays= lineEQN[57:62].strip()
>        #    daysList.append(insulinDays)
>            
>            Count += 1
>            countList.append(str(Count))
>           # print("    " + str(Count) + "  " + insulinDates + " Hours: " +
> insulinHours)
> x = Count
> count = str(Count)
> 
> # Each date indicated the date on which a bottle of insulin has been
> depleted
> # The bar is to show the number of hours that the bottle has been in use.
> 
> Labels = datesList
> Xdata= hoursList
> 
> Title = ("Bottle List       Number of entries: " + count)
> x = np.arange(len(Labels))  # the label locations
> 
> width = 0.35  # the width of the bars
> margin = 0
> 
> fig, ax = plt.subplots()
> fig.tight_layout(pad=10) # Sets the size of the graph
> 
> rects1 = ax.bar(x - width/2, Xdata, width, label='Hours') #Populates the x
> axis
> 
> # Add some text for labels, title and custom x-axis tick labels, etc.
> # fontsize = 20
> 
> ax.set_ylabel('Hours of use for each bottle')
> ax.set_title(Title)
> ax.set_xticks(x)
> ax.set_xticklabels((datesList), rotation = 90) #Dates at bottom of the graph
> ax.legend()
> ax.bar_label(rects1, padding=0,rotation = 90)
> 
> plt.show()
> 
[snip]

You're passing ax.bar the contents of Xdata.

What does Xdata contain?

A list of _strings_.

If you want the length of the bars to represent the number of hours, 
then they should be numbers.


More information about the Python-list mailing list