简体   繁体   中英

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:

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). I first tried using a marker, but the attribute .axvline doesn't work. 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. not a 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.

I am not sure that I have understood your question so I will go with an example:

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. Personnaly, I went for something like that:

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 ! Hope it helps !

You can plot a normal time series plot first

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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