【问题标题】:Pandas Dataframe Multicolor Line plotPandas Dataframe 多色线图
【发布时间】:2014-08-26 17:28:52
【问题描述】:

我有一个带有 DateTime 索引和两列代表风速和环境温度的 Pandas 数据框。这是半天的数据

                        temp        winds

2014-06-01 00:00:00     8.754545    0.263636
2014-06-01 01:00:00     8.025000    0.291667
2014-06-01 02:00:00     7.375000    0.391667
2014-06-01 03:00:00     6.850000    0.308333
2014-06-01 04:00:00     7.150000    0.258333
2014-06-01 05:00:00     7.708333    0.375000
2014-06-01 06:00:00     9.008333    0.391667
2014-06-01 07:00:00     10.858333   0.300000
2014-06-01 08:00:00     12.616667   0.341667
2014-06-01 09:00:00     15.008333   0.308333
2014-06-01 10:00:00     17.991667   0.491667
2014-06-01 11:00:00     21.108333   0.491667
2014-06-01 12:00:00     21.866667   0.395238

我想将此数据绘制为一条颜色随温度变化的线。因此,例如从浅红色到深红色,温度越高。

我在 matplotlib 中找到了这个 example of multicolored 行,但我不知道如何将它与 pandas DataFrame 一起使用。有谁知道我能做什么? 如果可以做到这一点,是否也可以作为附加功能根据风速改变线的宽度?所以风速越快线越宽。

感谢您的帮助!

【问题讨论】:

    标签: python numpy matplotlib pandas


    【解决方案1】:

    pandas 中的内置 plot 方法可能无法做到这一点。您需要提取数据并使用matplotlib 绘制它们。

    from matplotlib.collections import LineCollection
    import matplotlib.dates as mpd
    
    x=mpd.date2num(df.index.to_pydatetime())
    y=df.winds.values
    c=df['temp'].values
    points = np.array([x, y]).T.reshape(-1, 1, 2)
    segments = np.concatenate([points[:-1], points[1:]], axis=1)
    lc = LineCollection(segments, cmap=plt.get_cmap('copper'), norm=plt.Normalize(0, 10))
    lc.set_array(c)
    lc.set_linewidth(3)
    ax=plt.gca()
    ax.add_collection(lc)
    plt.xlim(min(x), max(x))
    ax.xaxis.set_major_locator(mpd.HourLocator())
    ax.xaxis.set_major_formatter(mpd.DateFormatter('%Y-%m-%d:%H:%M:%S'))
    _=plt.setp(ax.xaxis.get_majorticklabels(), rotation=70 )
    plt.savefig('temp.png')
    

    有两个问题值得一提,

  • 颜色渐变的范围由norm=plt.Normalize(0, 10)控制
  • pandasmatplotlib 绘制时间序列的方式不同,这需要在绘制之前将 df.index 转换为 float。通过修改major_locators,我们将xaxis majorticklabels 恢复为日期时间格式。
  • 当我们想要绘制多条线时,第二个问题可能会导致问题(数据将绘制在两个单独的 x 范围内):

    #follow what is already plotted:
    df['another']=np.random.random(13)
    print ax.get_xticks()
    df.another.plot(ax=ax, secondary_y=True)
    print ax.get_xticks(minor=True)
    
    [ 735385.          735385.04166667  735385.08333333  735385.125
      735385.16666667  735385.20833333  735385.25        735385.29166667
      735385.33333333  735385.375       735385.41666667  735385.45833333
      735385.5       ]
    [389328 389330 389332 389334 389336 389338 389340]
    

    因此我们需要不使用pandas.plot()方法:

    ax.twinx().plot(x, df.another)
    

    【讨论】:

    • 这看起来很棒,而且效果很好。能否顺便告诉我如何为彩色线条添加颜色条?
    • 我用我的颜色条解决了这个问题 :) 任何想法如何在这样一个带有单独 y 轴的图中添加第二条线?我刚试过 df['anotherline'].plot(ax=axes, secondary_y=True) 但这搞砸了整个情节。即使没有secondary_y,情节也不再显示任何内容
    • 查看编辑,只需要ax.twinx().plot(x, df.another)。干杯!
    猜你喜欢
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多