【问题标题】:Changing and altering ranges of x and y axis in a line graph in MatplotLib在 MatplotLib 中的折线图中更改和更改 x 和 y 轴的范围
【发布时间】:2018-12-11 10:55:10
【问题描述】:

我在图 1 中创建了一个折线图,而我想将我的 x 轴(即一年中的 365 天)更改为 12 个月,但需要绘制每个日期的值。我想开发一个像2这样的图表。 我的数据框看起来像这样

       Month       Date   max   min
0      1.0 1900-01-01  15.6 -16.0
1      1.0 1900-01-02  13.9 -26.7
2      1.0 1900-01-03  13.3 -26.7
3      1.0 1900-01-04  10.6 -26.1
4      1.0 1900-01-05  12.8 -15.5

【问题讨论】:

标签: python pandas matplotlib charts linegraph


【解决方案1】:

如果没有代码和尝试,我们无法为您提供修复。 但是,使用 MatplotLib,您正在寻找的功能是:

文档提供的一个可以帮助您的示例:

 xticks(np.arange(5), ('Tom', 'Dick', 'Harry', 'Sally', 'Sue'))

# np.arange(5) is the location, i.e. 0, 1, 2, 3, 4
# ('Tom', 'Dick', 'Harry', 'Sally', 'Sue') are the labels

编辑:因为你是数据框存储数据,你可以制作一个字典来将数字转换为月份:

month = {1: "January", 2: "February", ..., 12: "December"}

【讨论】:

    【解决方案2】:

    为此目的使用重采样,做一个:

    df=df.resample("M").last() # M stands for monoth, you can do w for weeks etc
    #also .last() will take the last entry of the month 
    

    除了 .last() 你还可以使用许多其他方法,例如 .min() .max() .mean() 等,它们会相应地取值

    文档是here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-26
      • 2014-01-03
      • 1970-01-01
      • 2015-09-14
      • 2019-10-03
      相关资源
      最近更新 更多