使用Python拆分数据并用实线和虚线绘制它们
我想将以下数据分为两部分:2018-09年至2019-11年的观测数据和2019-12年至日期结束列的预测数据,分别用matplotlib、plotly或seaborn等绘制实线和虚线 我尝试了如下代码,首先我得到了一个错误,其次我还没有绘制pct。有人能帮忙吗?多谢各位使用Python拆分数据并用实线和虚线绘制它们,python,pandas,matplotlib,plotly,seaborn,Python,Pandas,Matplotlib,Plotly,Seaborn,我想将以下数据分为两部分:2018-09年至2019-11年的观测数据和2019-12年至日期结束列的预测数据,分别用matplotlib、plotly或seaborn等绘制实线和虚线 我尝试了如下代码,首先我得到了一个错误,其次我还没有绘制pct。有人能帮忙吗?多谢各位 df = df.set_index('date') plt.plot('date', 'price', data=df.loc['2018-09':'2019-11'], marker='o', color='green',
df = df.set_index('date')
plt.plot('date', 'price', data=df.loc['2018-09':'2019-11'], marker='o', color='green', linewidth=2)
plt.plot('date', 'price', data=df.loc['2019-12':], marker='o', color='green', linewidth=2, linestyle = '--')
它生成ValueError:x和y必须具有相同的第一维度,但具有形状1和15
编辑:这段代码已经成功地绘制了价格的绘图,但我需要在同一个绘图上绘制pct
df['date'] = pd.to_datetime(df['date'])
# https://stackoverflow.com/questions/46230864/split-dataframe-on-the-basis-of-date
split_date ='2019-12-01'
plt.figure(figsize=(10, 5))
plt.plot('date', 'rent_price', data = df.loc[df['date'] <= split_date], marker='o', color='red', linewidth=2)
plt.plot('date', 'rent_price', data = df.loc[df['date'] >= split_date], marker='o', color='green', linewidth=2, linestyle = '--')
如果尺寸不同,可以尝试使用子批次单独打印数据。网站上有关于子地块的文档和教程
df = df.set_index('date')
plt.subplot(211)
plt.plot('date', 'rent_price', data=df.loc['2018-09':'2019-11'], marker='o', color='green', linewidth=2)
plt.xlabel('Observed')
plt.subplot(212)
plt.plot('date', 'rent_price', data=df.loc['2019-12':], marker='o', color='green', linewidth=2, linestyle = '--')
plt.xlabel('Predicted')
plt.show()
如果尺寸不同,可以尝试使用子批次单独打印数据。网站上有关于子地块的文档和教程
df = df.set_index('date')
plt.subplot(211)
plt.plot('date', 'rent_price', data=df.loc['2018-09':'2019-11'], marker='o', color='green', linewidth=2)
plt.xlabel('Observed')
plt.subplot(212)
plt.plot('date', 'rent_price', data=df.loc['2019-12':], marker='o', color='green', linewidth=2, linestyle = '--')
plt.xlabel('Predicted')
plt.show()
我认为你所描述的最好是用这样的方式来描述: 完整代码:
我认为你所描述的最好是用这样的方式来描述: 完整代码:
我想我们需要使用fig,ax1=plt.subplot ax2=ax1.twinx。参考:这个答案是调用pandas中数据帧的plot方法。df3.plot,因此如果以这种方式执行,则需要使用df.plot并使用ax=ax1或ax2。我不确定您希望它如何显示,因为日期不同,但也在x轴上使用。我认为我们需要使用fig,ax1=plt.subplot ax2=ax1.twinx。参考:这个答案是调用pandas中数据帧的plot方法。df3.plot,因此如果以这种方式执行,则需要使用df.plot并使用ax=ax1或ax2。我不确定您希望它如何显示,因为日期不同,但也在x轴上使用。很抱歉,它会生成ModuleNotFoundError:没有名为“plotly.SubPlot”的模块。顺便问一下,我们可以用plotly在行上显示值吗?@ahbon现在只在电话上签入。我明天再打给你。当然,谢谢。我将更新我的问题,将pct转换为百分比格式,并需要在绘图上显示pct和价格的值。我试过matplotlib,它对pct的显示值不太起作用。@ahbon Plotly几乎可以显示任何内容。但您必须先安装它=抱歉,它会生成ModuleNotFoundError:没有名为“plotly.SubPlot”的模块。顺便问一下,我们可以用plotly在行上显示值吗?@ahbon现在只在手机上签入。我明天再打给你。当然,谢谢。我将更新我的问题,将pct转换为百分比格式,并需要在绘图上显示pct和价格的值。我试过matplotlib,它对pct的显示值不太起作用。@ahbon Plotly几乎可以显示任何内容。但你必须先安装它=
# imports
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import pandas as pd
# your data
df = pd.DataFrame({'date': {0: '2018-09',
1: '2018-10',
2: '2018-11',
3: '2018-12',
4: '2019-01',
5: '2019-02',
6: '2019-03',
7: '2019-04',
8: '2019-05',
9: '2019-06',
10: '2019-07',
11: '2019-08',
12: '2019-09',
13: '2019-10',
14: '2019-11',
15: '2019-12',
16: '2020-01',
17: '2020-02',
18: '2020-03',
19: '2020-04',
20: '2020-05'},
'price': {0: 10.599,
1: 10.808,
2: 10.418,
3: 10.166,
4: 9.995,
5: 10.663,
6: 10.559000000000001,
7: 10.055,
8: 10.690999999999999,
9: 10.765999999999998,
10: 10.667,
11: 10.504000000000001,
12: 10.284,
13: 10.047,
14: 9.717,
15: 9.908,
16: 9.57,
17: 9.754,
18: 9.779,
19: 9.777000000000001,
20: 9.932},
'pct': {0: 0.02,
1: 0.02,
2: -0.036000000000000004,
3: -0.024,
4: -0.017,
5: 0.067,
6: -0.01,
7: -0.048,
8: 0.063,
9: 0.006999999999999999,
10: -0.009000000000000001,
11: -0.015,
12: -0.021,
13: -0.023,
14: -0.033,
15: -0.028999999999999998,
16: -0.045,
17: -0.023,
18: -0.025,
19: -0.031,
20: -0.02}})
# make timestamp to make plotting easier
df['timestamp']=pd.to_datetime(df['date'])
df=df.set_index('timestamp')
# split data
df_predict = df.loc['2019-11':]
df_actual = df[~df.isin(df_predict)].dropna()
# plotly setup
fig = make_subplots(rows=2,
cols=1,
subplot_titles=('Price', 'Pct'))
# Price, actual
fig.add_trace(go.Scatter(x=df_actual.index, y=df_actual['price'],
name = "price, actual",
mode='lines',
line=dict(color='steelblue', width=2)
)
,row=1, col=1)
# Price, prediction
fig.add_trace(go.Scatter(x=df_predict.index, y=df_predict['price'],
name = "price, prediction",
mode='lines',
line=dict(color='firebrick', width=2, dash='dash')
),
row=1, col=1)
# pct actual
fig.add_trace(go.Scatter(x=df_actual.index, y=df_actual['pct'],
mode='lines',
name = "pct, actual",
line=dict(color='steelblue', width=2)
)
,row=2, col=1)
# pct prediction
fig.add_trace(go.Scatter(x=df_predict.index, y=df_predict['pct'],
name="pct, prediction",
mode='lines',
line=dict(color='firebrick', width=2, dash='dash')
),
row=2, col=1)
fig.show()