简体   繁体   English

Python Pandas在一个图上绘制多个图

[英]Python pandas plotting multiple graphs on one plot

I have a dataset that looks like this. 我有一个看起来像这样的数据集。

Date Name High Value
2017-12-31 Bitcoin 14377.40 18723.76 2017-12-30 Bitcoin 14681.90 18766.88 2017-12-29 Bitcoin 15279.00 18755.70 2017-12-28 Bitcoin 15888.40 18820.54 ... ... ... ... 2017-01-08 CannaCoin 0.01 0.02 2017-01-07 CannaCoin 0.01 0.02 2017-01-06 CannaCoin 0.01 0.02 2017-01-05 CannaCoin 0.02 0.01

Date is the index column and is in the datetime format. Date是索引列,采用日期时间格式。 My dataset is big and has more than one item in the Name column. 我的数据集很大,并且“ Name列中有多个项目。 The dates range from the beginning of the year till the end. 日期范围从年初到年底。 Also, not all items are of the same length. 另外,并非所有物品的长度都相同。 Most should finish at the end of the year but they don't necessarily start in the beginning of it, they can start later. 大多数应在年底完成,但不一定从年初开始,而是可以稍后开始。

What I would like to do is, group by Name values, and for each create a separate line on the same graph/plot. 我想做的是,按“ Name值分组,并为每个值在同一图形/图上创建一条单独的线。 Value should be on the y axis. Value应在y轴上。

Because I am used to R, what I did was: df.groupby("Name")["Value"].plot() 因为我习惯了R,所以我所做的是: df.groupby("Name")["Value"].plot()

What I got is a warning: 我得到的是一个警告:

UserWarning: Attempting to set identical left==right results in singular transformations; automatically expanding. left=17531.0, right=17531.0 'left=%s, right=%s') % (left, right))

Also, the plot looked like this: 此外,情节看起来像这样: 缺少半个图,日期顺序错误

As observable, half of the values are missing, as they are outside the plotting area, dates are in a descending instead of an ascending order and half of the plot is empty. 可以观察到,缺少一半的值,因为它们在绘图区域之外,日期以降序而不是升序排列,并且一半的图为空。

How can I fix this, so that the entire plot will be visible, with the dates in a correct order? 如何解决此问题,以便整个图可见,日期顺序正确?

you can pivot on the "Name" and then pass the dataframe to the plot. 您可以在“名称”上旋转,然后将数据框传递到绘图。

pd.pivot_table(data=df, values="Value",columns="Name",index="Date").plot()

Example: 例:

In[]:
idx = ["2017-12-28","2017-12-29","2017-12-30","2017-12-31"] * 2
name = ['Bitcoin'] * 4 + ['CannaCoin'] * 4
vals = np.random.rand(8) * 1000
df = pd.DataFrame({"Date":idx, "Name":name, "Value":vals})
print(df)

Out[]:
         Date       Name       Value
0  2017-12-28    Bitcoin  788.547631
1  2017-12-29    Bitcoin  572.695484
2  2017-12-30    Bitcoin  661.859195
3  2017-12-31    Bitcoin  205.473883
4  2017-12-28  CannaCoin  270.291858
5  2017-12-29  CannaCoin  683.827404
6  2017-12-30  CannaCoin  447.808772
7  2017-12-31  CannaCoin  616.927833

In[]:
pd.pivot_table(data=df, values="Value",columns="Name",index="Date").plot()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM