python - 使用 matplotlib 在 Python 上的散点图上未正确显示 Y 轴标签
问题描述
我正在尝试使用 Python 上的 matlib 包绘制一些数据。但是,当我尝试绘制一组数据然后设置 y 轴限制时,y 轴上的标签不会显示我想要的全部范围。事实上,它只显示写入数据的值。
我尝试更改 y 轴限制。我试过用另一组数据绘制它,但标签似乎没有改变。当我使用具有 80-100 数据的 temp Vec 绘图时,它将显示从 0、100 的整个轴范围。当我尝试绘制相对湿度矢量时,它仅绘制来自可用数据范围的数据大约 0 - 40。
with open('April_26.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter =',')
next(readCSV) #skips the first row which is the headers
#Initialize arrays
timeVec = []
tempVec = []
relHumidityVec = []
#loop through each row
for row in readCSV: #for each row in the CSV file
##Storing specific data
#Note: CSV data is in strings so convert the integers if they are to be
treated as such
time = int(row[0])
temp = int(row[2])
relHum =row[3]
#append to array
timeVec.append(time/1000)
tempVec.append(temp)
relHumidityVec.append(relHum)
#initialize plot
fig, ax = plt.subplots()
#plt.scatter(timeVec, tempVec)
plt.scatter(timeVec, relHumidityVec, s = 4, marker = 'o', c = 'blue' , alpha = 0.4)
plt.scatter(timeVec, tempVec, s = 4, marker = 'o', c = 'red' , alpha = 0.4)
plt.ylim(0, 100)
我希望该图能够绘制我的两个图(温度与时间、相对湿度与时间),同时在 y 轴上从 0 到 100 绘制时间的全范围和全范围。
解决方案
我解决了这个问题。这是因为通过读取 .csv 文件,所有值都是字符串类型。我的相对湿度向量仍然是字符串类型,所以我只需要使用代码转换为 int 即可。
relHum = int(row[3])
推荐阅读
- javascript - 数组元素是未定义的 Javascript
- php - 从不同的数据库中选择两个或多个表在linux cpanel上不起作用
- classification - 如何处理分类中的样本数不均匀?
- ffmpeg - 如何在 FFMpeg 中的视频上添加具有缩小效果的文本?
- ssl - 如何使用端口 587 的亚马逊 smtp 发送电子邮件
- node.js - 运行meteor时不更新的包
- c# - 将左列添加到 MVC Net Core 项目模板视图,该视图会随着用户向下滚动而消失
- replace - 如何从 mule ESB 中的有效负载中删除括号?
- javascript - 试图将状态放入 const React 类 - 总是得到 TypeError: this.setState is not a function
- javascript - 我在检索 GZIP 格式的 API 调用接收到的数据时遇到问题