Reputation: 103
I have a simple text file with scientific data, with one data item on each line. I want to plot it simply as sequential items as y-axis. I call this: list_y
Within the Python script I create a list that is a simple sequential number list for the x-axis. I call this: list_x
When I plot, using the matplotlib calls, the same as dozens of examples that I have read, I get unexpected results. Matplotlab does not seem to give a y-axis from the smallest-to-largest of my y value data items. It seems to plot number pairs - not what I would call a "graph".
I have read various examples and watched Youtube demonstrations with the exact code that I am using, and those examples all give a result that I would expect.
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 10 12:18:57 2020
@author: usera
"""
from matplotlib import pyplot as plt
# data in data01a.txt is: 0.1 0.22 0.33 0.5 0.6 0.66 1 .9
data_han = open('data01a.txt', 'r')
list_y = data_han.readlines()
data_han.close()
y_size = len(list_y)
ii = 0
list_x = []
#follow while stmt creates a numeric list for x-axis
while ii < y_size:
list_x.append(ii)
ii += 1
plt.xlabel('Sequence')
plt.ylabel('Value')
plt.legend()
plt.grid('True')
#following 3 lines of code commented out
#since it causes strange results
#axes = plt.gca()
#axes.set_xlim(0,10)
#axes.set_ylim(0,1)
#following commented out code does not give correct x axis
#and no line is plotted at all
#plt.axis(0,10,0,1)
#following commented out code does not give correct y axis
#plt.xlim(0,10)
#plt.ylim(0,1)
plt.plot(list_x, list_y, label='Test Plot')
plt.savefig('test01a.png')
plt.show()
Any help is much appreciated.
Thank you.
Upvotes: 0
Views: 113
Reputation: 8623
A very "quick and dirty" approach would be something like the following:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from matplotlib import pyplot as plt
y_data = [0.1, 0.22, 0.33, 0.5, 0.6, 0.66, 1, 0.9]
y_data = sorted(y_data)
x_data = [i+1 for i in range(len(y_data))]
plt.plot(x_data, y_data)
plt.show()
I stripped off, every part not needed for a very basic example. So, feel free to add any configuration for grid, title or labels later.
I assumed, you are able to read and parse the data from your input file into a list of floats called y_data
. In addition, I assumed that you want to sort the y-values from lowest to highest, as you have stated:
does not seem to give a y-axis from the smallest-to-largest of my y value data items
If you want to keep the original order of your y_data
, simply remove the line y_data = sorted(y_data)
.
Next thing we should elaborate on, is plotting with plt.plot()
. According to the docs, providing x-values is optional and we are allowed to provide y-values without any x-values. When doing so, the element index of the corresponding y-value will be used as the x-value. This would result in a value range of 0...N-1. You might want to have 1...N which we can achieve by calculating a very basic list of x-values based on the length list containing the y-values y_data
. By doing so, we can pass both lists x_data
and y_data
to plt.plot()
and get the desired output:
The output without explicitly given x-values would be quite similar, but 0-based instead of 1-based on the x-axis. Change plt.plot(x_data, y_data)
into plt.plot(y_data)
to see the difference:
Upvotes: 1