繁体   English   中英

在 seaborn 中为时间序列 plot 的一部分着色

[英]Coloring a part of a time series plot in seaborn

基于包含两列的 dataframe,一列带有日期和时间,一列带有价格值,我得到以下图:

import seaborn as sns
# Use seaborn style defaults and set the default figure size
sns.set(rc={'figure.figsize':(20, 7)})
df['value'].plot(linewidth=0.5);

cols_plot = ['value']
axes = df[cols_plot].plot(marker='.', alpha=0.5, linestyle='None', figsize=(20, 7), subplots=True)
for ax in axes:
      ax.set_ylabel('Price')

我想为图表的一部分(即 7 天)使用不同的颜色。 我首先尝试使用标记,但属性.axvline不起作用。 我知道通常使用plt.plot之类的东西,其中有指定间隔和颜色的参数,但就我而言,我有一个数组。 不是 plot。

编辑:这是数据数组的示例:

+-----------------------------------+------------+
|               Start                   Value    |
+-----------------------------------+------------+
  08.06.2019 08:00                         33
  08.06.2019 09:00                         65      
  08.07.2019 08:00                         45 
  08.07.2019 09:00                         57 
  08.08.2019 08:00                         52 
+-----------------------------------+------------+

我只想为跨越 7 月的图表着色。

我不确定我是否理解了你的问题,所以我将以 go 为例:

import matplotlib.pyplot as plt

t=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

a=[10, 50, 100, 40, 20, 10, 80, 50, 78, 41]

plt.plot(t[0:5], a[0:5], color='red')
plt.plot(t[6:10], a[6:10], color='blue')

你想做类似的事情吗?

编辑:

嗨对不起久等了

因此,我假设您有两个变量,一个包含 valeus,另一个包含日期。 Personnaly,我去了类似的东西:

date = ['08.06.2019', '08.06.2019', '08.07.2019', '08.07.2019', '08.08.2019']
value = [33, 65, 45, 57, 52]


t =[]
a=[]

for i in range(len(date)):
    t.append(date[i].split("."))


for i in range(len(t)):
    a.append(int(t[i][1]))


plt.xticks((6, 7, 8), ('08.06.2019', '08.07.2019', '08.08.2019'))
for i in range(len(a)):
    if a[i] == 7 :
        plt.scatter(a[i], value[i], color = "red")
    else : 
        plt.scatter(a[i], value[i], color ="blue")

它允许您显示散点图 plot,如果您想要带有线条的 plot,您可以从中获得灵感! 希望能帮助到你 !

你可以先 plot 一个正常的时间序列 plot

fig = plt.figure(figsize=(15,4)) 
ax1=plt.subplot(121)

sns.lineplot(x="Date", y="Value", data=df, ax=ax1) # plot normal time series plot
ax1.xaxis.set_major_formatter(mdates.DateFormatter("%b-%Y")) # change to nicer date format

然后 plot 只是将感兴趣的区域覆盖在正常时间序列 plot 之上。

# plot subset on top of the normal time series
sns.lineplot(x="Date", y="Value", 
    data=df[(df['Date'] > '2018-11-30') & (df['Date'] < '2019-01-01')], 
    color='green', ax=ax1)

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM