Python 打印数据框中的两列
我有一个pandas数据框架,其中日期作为索引和一些列: 我想用两条线(比如'ISP.MI'和'Ctrv')绘制一个折线图;在x轴上,我需要“日期”Python 打印数据框中的两列,python,pandas,matplotlib,Python,Pandas,Matplotlib,我有一个pandas数据框架,其中日期作为索引和一些列: 我想用两条线(比如'ISP.MI'和'Ctrv')绘制一个折线图;在x轴上,我需要“日期” Ticker ISP.MI Daily returns Ctrv Inv_Am Giac_Media Date 2016-01-01 2.90117 NaN 10
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
如果您不关心轴比例:
plt.figure()
x = df['Date']
y1 = df['ISP.MI']
y2 = df['Ctrv']
plt.plot(x,y1)
plt.plot(x,y2)
如果你真的关心它:
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-')
我认为最简单的方法是按子集选择列,然后:
因此,以下代码从头开始创建一个与您的数据框相似的数据框,并生成您要求的绘图:
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()
但是,我相信您的数据帧在两个方面是不同的:
df[['ISP.MI','Ctrv']]
然后在较小的数据帧上使用.plot()方法,并让pandas处理其余部分
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
d = {'x' : [1,2,3,4,5,6,7,8,9,10],
'y_one' : np.random.rand(10),
'y_two' : np.random.rand(10)}
df = pd.DataFrame(d)
df.plot('x',y=['y_one','y_two'])
plt.show()
现在,在最新版本中,您可以直接使用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')
我认为这可能是解决这个问题的更好方法
如何添加每列线条的样式和颜色作为列表,例如g-、ro等。请添加到答案中@jezrael@ihightower-有多种解决方案,请检查答案。@jezrael您知道如何保存以这种方式创建的绘图吗?我使用了
plt.savefig(“path/pic1.png”)
它只保存一个空白图像。我发现这种方法很有用,因为它显示了如何使用plot()
从数据框中选择特定列,并将指定列映射到X&Y轴。
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')