首页 > 解决方案 > 使用 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 绘制时间的全范围和全范围。

标签: pythonplotlabelaxisscatter

解决方案


我解决了这个问题。这是因为通过读取 .csv 文件,所有值都是字符串类型。我的相对湿度向量仍然是字符串类型,所以我只需要使用代码转换为 int 即可。

relHum = int(row[3])

推荐阅读