【问题标题】:Y-axis not properly sorted matplotlib [duplicate]Y轴未正确排序matplotlib [重复]
【发布时间】:2018-06-13 12:21:21
【问题描述】:

我正在尝试在 Python3 中使用 Matplotlib 绘制温度与日期的关系图。日期沿 x 轴按升序排列,但 y 轴不是从小到大设置的。 Matplotlib 看似正确地定义了范围,但随后 y 轴没有从最小值开始,并且在图顶部的顺序变得更加不正确,如下所示:y-axis view

我用来绘制的代码如下。数据来自具有四个键(即:US ...)的字典,每个键都有四个列表作为它们的值。第一个包含日期,第二个包含最高温度,然后是最低温度,然后是平均值。

from matplotlib import pyplot
...
pyplot.figure(figsize = (250, 20))
pyplot.plot(\
station_dict['USW00012839'][0], station_dict['USW00012839'][1], 'b-', \
station_dict['USC00081306'][0], station_dict['USC00081306'][1], 'b-', \
station_dict['USW00092811'][0], station_dict['USW00092811'][1], 'b-', \
station_dict['USC00085667'][0], station_dict['USC00085667'][1], 'b-', \
station_dict['USW00012839'][0], station_dict['USW00012839'][2], 'g-', \
station_dict['USC00081306'][0], station_dict['USC00081306'][2], 'g-', \
station_dict['USW00092811'][0], station_dict['USW00092811'][2], 'g-', \
station_dict['USC00085667'][0], station_dict['USC00085667'][2], 'g-', \
station_dict['USW00012839'][0], station_dict['USW00012839'][3], 'r-', \
station_dict['USC00081306'][0], station_dict['USC00081306'][3], 'r-', \
station_dict['USW00092811'][0], station_dict['USW00092811'][3], 'r-', \
station_dict['USC00085667'][0], station_dict['USC00085667'][3], 'r-' \
)
pyplot.title('Miami Temperature from August 2015 to December 2017\n', \
size = 'x-large', weight = 'bold')
pyplot.ylabel('Temperature (Fahrenheit)', style = 'italic')
pyplot.xlabel('Date', style = 'italic')
pyplot.xticks(rotation = 'vertical')
pyplot.show()

整个剧情可以看这里:plot

完整的脚本可以在这里找到:parseNCEI.py 尽管需要将路径编辑到以下文件所在的位置。

数据来源的文件可以在这里找到:1166240.csv

【问题讨论】:

标签: python python-3.x matplotlib plot


【解决方案1】:

正如 DavidG 建议的那样,我正在检查我的数据并将 y 值从字符串转换为整数,这解决了问题。

【讨论】:

  • 它似乎将字符串解释为不同的值
猜你喜欢
  • 2018-06-12
  • 2022-12-21
  • 1970-01-01
  • 2022-01-01
  • 2023-03-30
  • 2021-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多