Python,Matplotlib:当x为时间时如何设置轴范围?

问题描述 投票:0回答:1

我正在家里做我的学校Arduino项目,老师请我为他可视化我的数据。在我的X轴上,我需要显示2万多个时间点,因此我尝试为其设置范围。

我正在尝试实现的图形:“所需图形”“>

我到现在为止:“当前图表”“>

很明显,我做错了事,并且我已经研究出解决方法。

我的绘图部分的#部分是我从互联网上学到的,但在这里不起作用。

[请帮助我,如果有任何不清楚的地方,请告诉我!

import csv
import matplotlib.pyplot as plt
from datetime import datetime

header = []
data = []

path="DATALOG.csv"   #CHANGE filename to match file
file = open(path, newline='')
reader=csv.reader(file)

header=next(reader) #This reads the first row and stores the words in header.
data=[row for row in reader]  #This reads the rest and store the data in a list

file.close()  #closes the file when complete

# This function will print the headings with their index. Helpful when dealing with many columns
def print_header():
   for index, column_header in enumerate(header):
       print(index, column_header)

# I want the high and low temperatures in New Glasgow for June 2010
# Key headings
# 0 date/time
# 1 temperature
# 2 humidity %
# 3 light level%

days = []
light = []

for row in range (len(data)):
    day = data[row][0]
    lights = data[row][3]
    current_date=datetime.strptime(data[row][0], "%A %H:%M:%S %d/%m/%Y")

    light.append(float(lights))        #store the day’s high temp in list
          #store the day’s low temp in list
    days.append(str(day))

fig = plt.figure(dpi=128, figsize = (50, 6))

#x = [dc[0] for dc in days]

#xticks = range(0,len(x),10)
#xlabels=[x[index] for index in xticks]
#xticks.append(len(x))
#xlabels.append(days[-1][0])
#ax.set_xticks(xtick)
#ax.set_xticklabels(xlabels,rotation=40)

plt.plot(days,light,c = 'red',label = 'temprature')

plt.title("LIGHT")
plt.xlabel('days',fontsize = 5)
#ax.plot(np.arange('Wednesday 12:00:00 08/01/2020',' Thursday 11:58:10 09/01/2020'), range(10))
fig.autofmt_xdate()
plt.ylabel('light (%)', fontsize = 12)
plt.tick_params(axis='both', which='major', labelsize = 10)

plt.legend()
plt.show()
plt.savefig('plot.png')

我正在家里做我的学校Arduino项目,老师请我为他可视化我的数据。在我的X轴上,我需要显示2万多个时间点,因此我尝试为其设置范围。 ...

python r matplotlib range axis
1个回答
0
投票
由于您没有提供可复制的示例,因此我根据对您的代码的了解创建了一个伪造的示例。基本上,看起来您有一个包含4列的文件:天数,温度,湿度和光水平。在这里,我仅创建两列。
© www.soinside.com 2019 - 2024. All rights reserved.