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

Python 在seaborn中为时间序列图的一部分着色,python,matplotlib,plot,time-series,seaborn,Python,Matplotlib,Plot,Time Series,Seaborn,基于包含两列的数据帧,一列带有日期和时间,另一列带有价格值,我得到了以下曲线图: 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(marke

基于包含两列的数据帧,一列带有日期和时间,另一列带有价格值,我得到了以下曲线图:

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的东西,其中有一些参数指定间隔和颜色,但在我的例子中,我有一个数组。不是阴谋

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

+-----------------------------------+------------+
|               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月份的图表上色

我不确定我是否理解了你的问题,因此我将举一个例子:

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,另一个包含日期。就我个人而言,我喜欢这样的东西:

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

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

您可以先绘制正常的时间序列图

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 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)
#在正常时间序列的顶部绘制子集
sns.lineplot(x=“日期”,y=“值”,
数据=df[(df['Date']>'2018-11-30')和(df['Date']<'2019-01-01'),
颜色=“绿色”,ax=ax1)

是的,在我的例子中,我有日期而不是数字,我想在中间的颜色和区域上有所不同。好吧,我明白了,你能提供一个你的日期数组的例子吗?你能更准确地描述你想要的区域吗?你如何准确地定义这个区域?