[英]Lineplot with Seaborn and ci=“sd” function in Python
I'm trying to produce some snazzy charts in seaborn and need some help. 我正尝试制作一些Seaborn的时髦图表,需要一些帮助。
I have some stock data, consisting of 5 stocks. 我有一些库存数据,包括5只股票。 I'm basically trying to visually display how
Stock A
has performed in comparison to the others. 我基本上是在试图直观地显示
Stock A
与其他Stock A
相比的表现。 To do this I am looking at cumulative returns, and have also calculated the average cumulative returns for the other 4 stocks. 为此,我查看了累积收益,并且还计算了其他4只股票的平均累积收益。 I have split this data up in to the following 2
df
: 我已将此数据拆分为以下2
df
:
Stock A's data let's call df
: 股票A的数据称为
df
:
Date Stock A
2019-04-24 07:59 0.433366
2019-04-24 08:59 0.397984
2019-04-24 09:59 0.403971
2019-04-24 10:59 0.399131
2019-04-24 11:59 0.386641
2019-04-24 12:59 0.388572
2019-04-24 13:59 0.396266
2019-04-24 14:59 0.391609
2019-04-24 15:59 0.399412
2019-04-24 16:59 0.401715
And then Stocks B, C, D & E, PLUS the calculated average let's call df2
(I can't print all 5 columns): 然后,股票B,C,D和E,再加上计算出的平均值,我们将其称为
df2
(我无法打印所有5列):
Date Stock B Stock C Stock E Average
2019-04-24 07:59 0.273965 0.000982 0.409717 0.472029
2019-04-24 08:59 0.235606 -0.076309 0.345047 0.407299
2019-04-24 09:59 0.240826 -0.059274 0.346769 0.413197
2019-04-24 10:59 0.234849 -0.056013 0.338185 0.407962
2019-04-24 11:59 0.230158 -0.062947 0.331907 0.397927
2019-04-24 12:59 0.237573 -0.055506 0.334907 0.412206
2019-04-24 13:59 0.239994 -0.047875 0.334213 0.413846
2019-04-24 14:59 0.230461 -0.059781 0.312962 0.395924
2019-04-24 15:59 0.236968 -0.054398 0.320990 0.406967
2019-04-24 16:59 0.239918 -0.049522 0.328713 0.412818
What I am ultimately looking to do is chart all 5 stocks plus the average on one graph, that has a nice grey background and perhaps some grid lines etc (at the minute I can only chart with ugly white backgrounds), but I would like the line for Stock A
and for Average
to be slightly different and make use of seaborns standard deviation line plot. 我最终要做的是将所有5只股票加上平均值在一张图表上绘制,该图具有很好的灰色背景,也许有些网格线等(目前我只能绘制难看的白色背景),但是我想对于行
Stock A
和Average
会略有不同,并利用seaborns标准差线图。
I found this example code sns.relplot(x="timepoint", y="signal", kind="line", ci="sd", data=fmri)
but when I tried to alter it to my needs I got error messages and couldn't get all the data to appear on the same chart. 我发现了这个示例代码
sns.relplot(x="timepoint", y="signal", kind="line", ci="sd", data=fmri)
但是当我尝试将其更改为自己的需求时,出现了错误消息,并且无法使所有数据都显示在同一图表上。
Here is a near perfect example of what I'm aiming for, but I would like to include Stock B, C, D & E from df2
and change the axis labeling of course. 这是我要达到的目标的近乎完美的示例,但我想将
df2
B,C,D和E包括在内,并更改轴标签。
Any help greatly appreciated. 任何帮助,不胜感激。 Cheers
干杯
This should produce what you asked for: 这应该产生您所要求的:
sns.set() #This sets the style to the seaborn default (gray background with white grid on)
fig,ax = plt.subplots() #create your figure and ax objects
sns.lineplot('Date', 'Stock A', ci="sd", data=df,ax=ax) #plot lines
sns.lineplot('Date', 'Stock B', ci="sd", data=df2,ax=ax)
sns.lineplot('Date', 'Stock C', ci="sd", data=df2,ax=ax)
sns.lineplot('Date', 'Stock E', ci="sd", data=df2,ax=ax)
sns.lineplot('Date', 'Average', ci="sd", data=df2,ax=ax)
plt.xticks(rotation=-45) #makes ticks visible (a long date would be unreadable otherwise)
Answering OP questions from comments: 通过评论回答OP问题:
Transform your dates from strings to datetime
objects, then matplotlib
will take care of the ticks and tickslabels
. 将您的日期从字符串转换为
datetime
对象,然后matplotlib
将处理ticks和tickslabels
。
As they are right now they are interpreted as strings and they all get plotted. 就像现在一样,它们被解释为字符串,并且都被绘制出来。
df['Date']=pd.to_datetime(df['Date'])
df2['Date']=pd.to_datetime(df2['Date'])
Use the following line to change the ylabel
使用以下行更改
ylabel
ax.set_ylabel('Returns')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.