Failed to get desired results while using the pyplot library.

Asked 1 years ago, Updated 1 years ago, 87 views

import matplotlib.pyplot as plt import csv

def analyze(): day = range(1, 31) temp = [] datafile = '15_01.csv' with open(datafile, 'rt') as f: data = csv.reader(f, delimiter=',') for d in data: temp.append(d)

print(temp)

x = [day for day, temp, hum in temp]
y = [temp for day, temp, hum in temp]

p = plt.plot(x, y, 'r^--', label='temp')

plt.legend()
plt.title('2015 January tempature')
plt.xlabel('Day'), plt.ylabel('tempature')
plt.show()

if name == 'main': analyze()

The code is the same as above and reads the csv file to draw a temperature graph from the 1st to the 31st

It is not sorted in order as shown above, but is sorted as 1 -> 10 - 19 - > 2 -> 20 - 29 How can I arrange it in the order I want?

python pyplot

2022-09-22 19:34

2 Answers

I think it's a problem to sort the tempo.

print(temp)

Below

Insert the phrase temp.sort() and spin the script.


2022-09-22 19:34

Did you solve it? After a while... I think you might have solved it, but... I had a hard time with a similar problem. To help other people search. I'm leaving it

I think the reason why the axis alignment is weird is that the values in the list are characters. Check it out and convert the values in the list into numbers.


2022-09-22 19:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.