在Matplotlib中将多个项目绘制为线图

2024-05-14 10:41:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样一个pandas数据帧:

Date     Allotment  NDII_Mean  NDVI_Mean  RGR_Mean  SATVI_Mean            
1984137   Arnston  -0.053650   0.414868  0.938309    0.332712   
1984185   Arnston   0.074928   0.558329  0.867951    0.334555   
1984249   Arnston  -0.124691   0.352225  1.041513    0.331821   
1985139   Arnston  -0.075537   0.468092  0.929414    0.383750   
1985171   Arnston   0.017400   0.493443  0.889835    0.314717   
1986206   Annex     0.151539   0.626690  0.775202    0.332507   
1986238   Annex     0.142235   0.604764  0.823083    0.303600   
1987241   Annex    -0.005423   0.506760  0.911124    0.338675   
1987257   Annex    -0.058166   0.449052  0.961348    0.336879

我想根据分配绘制,所以我需要使用groupby。因此,对于每个分配,我需要X轴上的日期,以及名称中的所有四个列(名称为mean)在图形上显示为线,它们的值在Y轴上。然后我会将它们保存为pdf格式,尽管如果有人知道其他方法,我也不必这样做。我可以用这段代码来绘制一个值(在这个例子中我的意思是很多NDII_),但是我想要绘制所有四列,而不仅仅是一列。我使用的代码是:

^{pr2}$

这是其中一个图的样子(与显示的数据不同,因为我缩短了示例表):

enter image description here

编辑:

这是通过添加编辑这一行来实现的

Hurst_plots=group.plot(x='Date', y=['NDII_Mean', 'RGR_Mean', 'SATVI_Mean', 'SWIR32_Mean'],title=str(i)).get_figure()

enter image description here

但有人知道如何把传说完全排除在图表之外吗?在


Tags: 数据代码名称编辑pandasdate绘制mean
1条回答
网友
1楼 · 发布于 2024-05-14 10:41:34

我曾有过使用pandas来制作图形的混合经验,大多数时候我都会将列作为numpy数组从数据框中拉出,然后直接使用matplotlib进行绘图。就我个人而言,我觉得我可以使用matplotlib本身对绘图进行更多的控制,比如设计绘图、线条颜色(通常我发现自己根据一些计算动态生成RGB三元组)和控制图例!我建议首先搜索matplotlib文档,尝试在matplotlib中搜索多行打印。看起来你在尝试绘制时间序列。Matplotlib有很好的(尽管一开始有点混乱)处理日期的界面,所以一旦你弄清楚了事情的工作原理,你就可以根据自己的喜好进行定制。在

这是我最近用来生成多行timeseries图的一个片段,注意这个最近添加的样式特性的使用,它使绘图看起来非常漂亮。从ipython笔记本上取的。在

import pandas as pd
from matplotlib import pyplot as plt
from matplotlib import dates as mdates
%matplotlib inline
import datetime
from datetime import datetime as dt

plt.style.use('fivethirtyeight')

months = mdates.MonthLocator(range(1,13), bymonthday=1)
monthsFmt = mdates.DateFormatter('%b')

fig, ax = plt.subplots()
plt.hold(True)
for year in range(2010,2016):
    vals = dfs[str(year)]['my_awesome_points'].cumsum().values
    adjusted_d_obj = ['2014'+x[4:] for x in dfs[str(year)]['date']]
    date_objs = [dt.strptime(x, '%Y-%m-%d') for x in adjusted_d_obj]
    dates = [mdates.date2num(x) for x in date_objs]
    #dateints = range(len(dates))
    if year == 2015:    
        ax.plot_date(dates, vals, '-', color=colors[str(year)],                          label=str(year))
    else:
        ax.plot_date(dates,vals, 'r-', color=colors[str(year)],    label=str(year), alpha=0.4)
fig.set_size_inches((14,10))
fig.set_dpi(800)

ax.grid(True)
fig.autofmt_xdate()
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(monthsFmt)

plt.savefig('sick_pick.png')

它使图形看起来像

decent looking matplotlib!

在我的例子中,我有一个预先存在的数据帧字典,其中每个数据帧都是以年为键访问的。保存到PDF是可能的,保存到PNG图像文件更容易,我认为如上所示保存图('文件名.png')应该行得通。PDF功能绝对很酷。我已经有唠叨的客户(不知道他们在说什么)要求报告/图表等。你可以建立一个循环,写一个有成百上千页的PDF,每一页都是一个格式很好的图表,有标题,图例,轴标签等。传统上,对matplotlib的抱怨是枯燥和通用的图形。新的matplotlib样式非常简单!在

编辑: 看看这个很棒的答案来解决你的传奇问题。https://stackoverflow.com/a/4701285/2639868

相关问题 更多 >

    热门问题