[英]Pandas dataframe groupby plot
I have a dataframe which is structured as:我有一个数据框,其结构如下:
Date ticker adj_close
0 2016-11-21 AAPL 111.730
1 2016-11-22 AAPL 111.800
2 2016-11-23 AAPL 111.230
3 2016-11-25 AAPL 111.790
4 2016-11-28 AAPL 111.570
...
8 2016-11-21 ACN 119.680
9 2016-11-22 ACN 119.480
10 2016-11-23 ACN 119.820
11 2016-11-25 ACN 120.740
...
How can I plot based on the ticker the adj_close
versus Date
?如何根据adj_close
与Date
的股票代码进行绘图?
you can use:您可以使用:
df.plot(x='Date',y='adj_close')
Or you can set the index to be Date
beforehand, then it's easy to plot the column you want:或者您可以预先将索引设置为Date
,然后很容易绘制您想要的列:
df.set_index('Date', inplace=True)
df['adj_close'].plot()
ticker
on it如果你想要一个图表,上面有一个系列的ticker
You need to groupby before:您需要先分组:
df.set_index('Date', inplace=True)
df.groupby('ticker')['adj_close'].plot(legend=True)
grouped = df.groupby('ticker')
ncols=2
nrows = int(np.ceil(grouped.ngroups/ncols))
fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(12,4), sharey=True)
for (key, ax) in zip(grouped.groups.keys(), axes.flatten()):
grouped.get_group(key).plot(ax=ax)
ax.legend()
plt.show()
Similar to Julien's answer above, I had success with the following:与上面朱利安的回答类似,我在以下方面取得了成功:
fig, ax = plt.subplots(figsize=(10,4))
for key, grp in df.groupby(['ticker']):
ax.plot(grp['Date'], grp['adj_close'], label=key)
ax.legend()
plt.show()
This solution might be more relevant if you want more control in matlab.如果您想在 matlab 中进行更多控制,此解决方案可能更相关。
Solution inspired by: https://stackoverflow.com/a/52526454/10521959解决方案灵感来自: https ://stackoverflow.com/a/52526454/10521959
.pivot
or .groupby
, or by plotting the existing long form dataframe directly with seaborn
.这可以通过使用.pivot
或.groupby
将数据帧重塑为宽格式,或者通过直接使用seaborn
绘制现有的长格式数据帧来实现。'Date'
column has a datetime64[ns] Dtype
.在以下示例数据中, 'Date'
列具有datetime64[ns] Dtype
。
Dtype
with pandas.to_datetime
if needed.如果需要,使用pandas.to_datetime
转换Dtype
。python 3.10
, pandas 1.4.2
, matplotlib 3.5.1
, seaborn 0.11.2
在python 3.10
、 pandas 1.4.2
、 matplotlib 3.5.1
、 seaborn 0.11.2
import pandas as pd
import pandas_datareader as web # for sample data; this can be installed with conda if using Anaconda, otherwise pip
import seaborn as sns
import matplotlib.pyplot as plt
# sample stock data
tickers = ['aapl', 'acn']
df = pd.concat((web.DataReader(ticker, data_source='yahoo', start='2020-01-01', end='2022-06-21')
.assign(tkr=ticker) for ticker in tickers)).iloc[:, [5, 6]].reset_index()
# display(df.head())
Date Adj Close ticker
0 2020-01-02 73.785904 aapl
1 2020-01-03 73.068573 aapl
2 2020-01-06 73.650795 aapl
3 2020-01-07 73.304420 aapl
4 2020-01-08 74.483604 aapl
# display(df.tail())
Date Adj Close ticker
1239 2022-06-14 275.119995 acn
1240 2022-06-15 281.190002 acn
1241 2022-06-16 270.899994 acn
1242 2022-06-17 275.380005 acn
1243 2022-06-21 282.730011 acn
pandas.DataFrame.pivot
& pandas.DataFrame.plot
pandas.DataFrame.pivot
& pandas.DataFrame.plot
pandas
plots with matplotlib
as the default backend.使用matplotlib
作为默认后端的pandas
绘图。pandas.DataFrame.pivot
converts from long to wide form, and puts the dataframe into the correct format to plot.使用pandas.DataFrame.pivot
重塑数据框会从长格式转换为宽格式,并将数据框放入正确的格式进行绘图。.pivot
does not aggregate data, so if there is more than 1 observation per index, per ticker, then use .pivot_table
.pivot
不聚合数据,因此如果每个索引、每个股票有超过 1 个观察值,则使用.pivot_table
subplots=True
will produce a figure with two subplots.添加subplots=True
将生成一个带有两个子图的图形。# reshape the long form data into a wide form
dfp = df.pivot(index='Date', columns='ticker', values='Adj Close')
# display(dfp.head())
ticker aapl acn
Date
2020-01-02 73.785904 203.171112
2020-01-03 73.068573 202.832764
2020-01-06 73.650795 201.508224
2020-01-07 73.304420 197.157654
2020-01-08 74.483604 197.544434
# plot
ax = dfp.plot(figsize=(11, 6))
seaborn
, which accepts long form data, so reshaping the dataframe to a wide form isn't necessary.使用seaborn
,它接受长格式数据,因此无需将数据框重塑为宽格式。seaborn
is a high-level api for matplotlib
seaborn
是matplotlib
的高级 apisns.lineplot
: axes-level plot sns.lineplot
:轴级图fig, ax = plt.subplots(figsize=(11, 6))
sns.lineplot(data=df, x='Date', y='Adj Close', hue='ticker', ax=ax)
sns.relplot
: figure-level plot sns.relplot
: 图形级图row='ticker'
, or col='ticker'
, will generate a figure with two subplots.添加row='ticker'
或col='ticker'
将生成带有两个子图的图形。g = sns.relplot(kind='line', data=df, x='Date', y='Adj Close', hue='ticker', aspect=1.75)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.