Python 从两列中绘制熊猫数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41825939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
plot pandas dataframe two columns from
提问by Alessandro Sorvillo
i have a pandas dataframe which has dates as indexes and some columns: I would like to plot a line chart with 2 lines (let's say 'ISP.MI' and 'Ctrv'); on the x axis I need the 'Date'
我有一个 Pandas 数据框,它有日期作为索引和一些列:我想用 2 行绘制折线图(比如“ISP.MI”和“Ctrv”);在 x 轴上我需要“日期”
Ticker ISP.MI Daily returns Ctrv Inv_Am Giac_Media
Date
2016-01-01 2.90117 NaN 100.000000 100 100.0
2016-01-04 2.80159 -0.034927 196.507301 200 150.0
2016-01-05 2.85608 0.019263 300.292610 300 200.0
2016-01-06 2.77904 -0.027345 392.081255 400 250.0
2016-01-07 2.73206 -0.017050 485.396411 500 300.0
2016-01-08 2.72267 -0.003443 583.725246 600 350.0
采纳答案by epattaro
if you dont care about axis scale:
如果您不关心轴比例:
plt.figure()
x = df['Date']
y1 = df['ISP.MI']
y2 = df['Ctrv']
plt.plot(x,y1)
plt.plot(x,y2)
if you do care about it:
如果你真的关心它:
fig, ax1 = plt.subplots()
x = df['Date']
y1 = df['ISP.MI']
y2 = df['Ctrv']
ax2 = ax1.twinx()
ax1.plot(x, y1, 'g-')
ax2.plot(x, y2, 'b-')
回答by jezrael
I think the simpliest is select columns by subset and then DataFrame.plot
:
我认为最简单的是按子集选择列,然后DataFrame.plot
:
df[['ISP.MI','Ctrv']].plot()
回答by alexbhandari
回答by Pezze
So, here is the code that from scratch creates a dataframe that looks like yours and generates the plot you asked for:
因此,这是从头开始创建一个看起来像您的数据框并生成您要求的图的代码:
import pandas as pd
import datetime
import numpy as np
from matplotlib import pyplot as plt
# The following two lines are not mandatory for the code to work
import matplotlib.style as style
style.use('dark_background')
def create_datetime_range(numdays=10):
"""Creates the timestamp range"""
base = datetime.datetime.today()
datelist = pd.date_range(base, periods=numdays).to_pydatetime()
return datelist
def convert_to_date(datetime_list):
"""Converts a timestamp array into a date array"""
return [x.date() for x in datetime_list]
a = pd.DataFrame(
{
'ISP.MI': np.random.normal(2,1,10),
'Ctrv' : np.random.normal(200,150,10)
},
index=convert_to_date(create_date_range())
)
a.plot()
However, I believe that your dataframe is different in two ways:
但是,我相信您的数据框在两个方面有所不同:
- It seems that there are two levels in the index (the Date title seems on a second row to the Ticker title). I imagine this could be because you used something like .groupby() or .unstack() or other aggregation/pivoting method. I suggest you to look in the reset_index() method.
- 索引中似乎有两个级别(日期标题似乎在代码标题的第二行)。我想这可能是因为您使用了 .groupby() 或 .unstack() 或其他聚合/旋转方法。我建议您查看 reset_index() 方法。
2.Your dataframe has more columns that you need. As suggested by @jezrael, you should first select only these. You can do it with something like:
2.您的数据框有更多您需要的列。正如@jezrael 所建议的,您应该首先只选择这些。你可以这样做:
df[['ISP.MI','Ctrv']]
and then using the .plot() method on the smaller dataframe and let pandas handle the rest.
然后在较小的数据帧上使用 .plot() 方法,让 Pandas 处理剩下的事情。
回答by shantanu pathak
Now in latest pandas you can directly use df.plot.scatter function
现在在最新的 Pandas 中你可以直接使用 df.plot.scatter 函数
df = pd.DataFrame([[5.1, 3.5, 0], [4.9, 3.0, 0], [7.0, 3.2, 1],
[6.4, 3.2, 1], [5.9, 3.0, 2]],
columns=['length', 'width', 'species'])
ax1 = df.plot.scatter(x='length',
y='width',
c='DarkBlue')
https://pandas.pydata.org/pandas-docs/version/0.23/generated/pandas.DataFrame.plot.scatter.html
https://pandas.pydata.org/pandas-docs/version/0.23/generated/pandas.DataFrame.plot.scatter.html