Trying to read from a text file to generate a graph
Steve
Gronicus at SGA.Ninja
Wed Jul 28 11:58:21 EDT 2021
I forgot about the no-file rule...
On 28Jul2021 02:55, Steve <Gronicus at SGA.Ninja> wrote:
>I am going though a struggle with this and just don't see where it fails.
>I am using the Dual Bar Graph.py program from
https://matplotlib.org/stable/gallery/index.html website.
>The file from the web site works so that shows that all my installations
are complete.
>
>My program, LibreGraphics 05.py program runs but the graph is all smutched
up. I am pulling data from the EXCEL-FILE.txt into the program, selecting
three values for each line and creating three variables formatted as is
shown in the original demo file.
>
>When you run the program, choose 112 when prompted. You will see the values
of the variables I want to pass to the graph section of the code. If the
values are hardcoded, the graphs look good. When the variables generated by
my section of the code, it does not.
>
>I am not sure what more to explain.
>Please help me....
>Steve
>
>I am attaching a zip file. I hope it gets through.
Alas, the python-list is text only, and attachments are discarded.
Here is my code for the main program:
=====================================================================
#https://matplotlib.org/stable/gallery/index.html
import matplotlib.pyplot as plt
import numpy as np
## In this first half of the program, I am reading lines of data from
## a file and reformatting them to create comms separated values into
## three variables.
Sensors = ""
TestStrips = ""
SampleNumber = ""
x = 1
SensorNumber = input("Enter senaor number: ")
with open("_EXCEL-FILE.txt" , 'r') as infile:
for lineEQN in infile: # loop to find each line in the file for that
dose
if (lineEQN[0:1]== "."):
SN = lineEQN[44:48].strip()
if (SensorNumber == SN):
SN = x
sn = "'" + str(SN) + "', "
SampleNumber = SampleNumber + sn
sv = lineEQN[25:29].strip()
sv = sv + ", "
Sensors = Sensors + sv
tv = lineEQN[32:37].strip()
tv = tv + ", "
TestStrips = TestStrips + tv
x += 1
SnLen = len(SampleNumber) -2
SampleNumber = SampleNumber[0:SnLen]
labels = "[" + SampleNumber + "]"
print("labels = " + labels)
SenLen = len(Sensors) -2
Sensors = Sensors[0:SenLen]
Sensors = "[" + Sensors + "]"
print("Sensors = " + Sensors)
TsLen = len(TestStrips) -2
TestStrips = TestStrips[0:TsLen]
TestStrips = "[" + TestStrips + "]"
print("TestStrips = " + TestStrips)
labels = SampleNumber
## =============================================================
## In this second half of the program, I want to use the three
## variables ## to populate a fraph.
## There are problems with this technique.
## =============================================================
## With the following 6 lines of code commented-out, the graphing
## program uses the variables from the first half of the program
## and the graph fails
## =============================================================
## Uncommented, the following works by overwriting the variables
## from the previous code and generates a proper graph.
#label = ['1', '2', '3', '4', '5']
#Sensor = [150, 132, 182, 75, 117]
#TestStrip = [211, 144, 219, 99, 142]
#labels = label
#Sensors = Sensor
#TestStrips = TestStrip
## ===========================================================
## The follows is the original cose from the sample program
## with minor variable names and label changes.
x = np.arange(len(labels)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, Sensors, width, label='Sensors')
rects2 = ax.bar(x + width/2, TestStrips, width, label='TestStrips')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Glucose Readings')
ax.set_title('Sensors VS Test Strip')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()
ax.bar_label(rects1, padding=3)
ax.bar_label(rects2, padding=3)
fig.tight_layout()
plt.show()
===========================================================
And here is a sample of the data file:
.Thu Jul 22, 2021 20:47 250 277 27 111 2
.Fri Jul 23, 2021 00:05 188 194 6 111 3
.Fri Jul 23, 2021 09:08 142 166 24 111 3
.Fri Jul 23, 2021 12:58 138 165 27 111 3
.Fri Jul 23, 2021 22:32 356 391 35 111 3
.Sat Jul 24, 2021 09:44 150 211 61 112 4
.Sat Jul 24, 2021 13:24 132 144 12 112 4
.Sat Jul 24, 2021 16:40 182 213 31 112 4
.Sat Jul 24, 2021 19:52 75 99 24 112 4
.Sat Jul 24, 2021 23:19 117 142 25 112 4
More information about the Python-list
mailing list