Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Trying to read from a text file to generate a graph

16 views
Skip to first unread message

Steve

unread,
Jul 28, 2021, 3:30:00 AM7/28/21
to
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.





George Melly remarked to Mike Jagger on how lined his face was for one so young. Jagger replied “They’re laughter lines George” to which Melly countered: “Mick, nothing’s that f**king funny!”.

Cameron Simpson

unread,
Jul 28, 2021, 4:41:17 AM7/28/21
to
On 28Jul2021 02:55, Steve <Gron...@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.

Is your programme small enough (one file, not insanely long) to just
include inline in your next message?

Have you printed the variables generated by your code? _Are_ they the
same as the hardcoded values? You may want to graph different stuff, but
start by trying to exactly reproduce the hardcoded value, but extracted
from the file.

Cheers,
Cameron Simpson <c...@cskk.id.au>

Steve

unread,
Jul 28, 2021, 11:58:48 AM7/28/21
to
I forgot about the no-file rule...

On 28Jul2021 02:55, Steve <Gron...@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

Stephen Berman

unread,
Jul 29, 2021, 2:54:48 AM7/29/21
to
[Resending to the list only, since I couldn't post it without subscribing.]

On Wed, 28 Jul 2021 11:58:21 -0400 "Steve" <Gron...@SGA.Ninja> wrote:

> I forgot about the no-file rule...
>
>> On 28Jul2021 02:55, Steve <Gron...@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.

The problem is due to the values of Sensors, TestStrips and SampleNumber
being strings; what you want is for them to be lists, as in the
assignments you commented out. And since the data from the file is read
in as strings, you have to cast the elements of the Sensors and
TestStrips lists to integers, since you want the numerical values. The
following code does the job:

Sensors = []
TestStrips = []
SampleNumber = []

x = 1
SensorNumber = input("Enter senaor number: ")
with open("_EXCEL-FILE.txt", 'r') as infile:
for lineEQN in infile:
if (lineEQN[0:1]== "."):
SN = lineEQN[44:48].strip()
if (SensorNumber == SN):
SN = x
SampleNumber.append(SN)

sv = lineEQN[25:29].strip()
Sensors.append(int(sv))

tv = lineEQN[32:37].strip()
TestStrips.append(int(tv))

x += 1

labels = SampleNumber

Add the rest of your code from the second half to make the desired bar
chart.

Steve Berman

Steve

unread,
Jul 29, 2021, 4:11:36 AM7/29/21
to
Thank you, the responses here have been extremely helpful.
Steve

----------------------------------------------------------------------------
----------------
Footnote:
There's 99 bugs in the code, in the code.
99 bugs in the code.
Take one down and patch it all around.
Now there's 117 bugs in the code.
--
https://mail.python.org/mailman/listinfo/python-list

Anssi Saari

unread,
Jul 29, 2021, 5:45:07 AM7/29/21
to
"Steve" <Gron...@SGA.Ninja> writes:

> I am going though a struggle with this and just don't see where it fails.

It seems to me you're putting your data into strings when you need to
put it into lists. And no, adding brackets and commas to your strings so
that printing out the strings makes them look like lists doesn't make
them into lists.

There's python tutorial at https://docs.python.org/3/tutorial/index.html
which may help with the basics.

0 new messages