简体   繁体   English

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

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

Based on a dataframe containing two columns, one with a date and time and one with a price value, I got the following plots:基于包含两列的 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')

I want to use a different color for a part of the graph (namely, a period of 7 days).我想为图表的一部分(即 7 天)使用不同的颜色。 I first tried using a marker, but the attribute .axvline doesn't work.我首先尝试使用标记,但属性.axvline不起作用。 I know that normally one uses something like plt.plot and inside it there are parameters specifying the interval and color, but in my case I have an array.我知道通常使用plt.plot之类的东西,其中有指定间隔和颜色的参数,但就我而言,我有一个数组。 not a plot.不是 plot。

EDIT: This is a sample of the data array:编辑:这是数据数组的示例:

+-----------------------------------+------------+
|               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 
+-----------------------------------+------------+

I only want to color the graph spanning the month July.我只想为跨越 7 月的图表着色。

I am not sure that I have understood your question so I will go with an example:我不确定我是否理解了你的问题,所以我将以 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')

Do you want to do something similar?你想做类似的事情吗?

EDIT:编辑:

Hi sorry for the wait,嗨对不起久等了

So, I assume that you have two variables, one containing the valeus and another one containing the dates.因此,我假设您有两个变量,一个包含 valeus,另一个包含日期。 Personnaly, I went for something like that: 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")

It allows you to display a scatter plot, if you want a plot with lines you can take your inspiration from this !它允许您显示散点图 plot,如果您想要带有线条的 plot,您可以从中获得灵感! Hope it helps !希望能帮助到你 !

You can plot a normal time series plot first你可以先 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

And then plot just the region of interest to overlay it on top of the normal time series plot.然后 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