在matplotlib中绘制不同颜色的图形

时间:2018-06-10 07:38:16

标签: python pandas dataframe graph

我有下表。

  Date       Score
11-01-02      40
11-01-03      47
11-01-04      41
11-01-05      35
11-01-06      52
11-01-07      47
11-01-08      45
11-01-09      43
11-01-10      40
11-01-11      41
11-01-12      41
11-01-13      49
11-01-14      40
11-01-15      40

我在python中将其作为pandas文件阅读,我想将其绘制为折线图,以便2011/01/08之前的分数为黄色,无论是2011/01/08还是之后同一轴上的红色。

在python中可以吗?我知道怎么在R中做,但我不确定熊猫中是否有类似的功能。

2 个答案:

答案 0 :(得分:2)

另一种方法是将条件部分绘制在整条曲线的顶部:

import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_csv("test.txt", delim_whitespace = True)
df.Date = pd.to_datetime(df.Date, format = '%y-%m-%d')
#define cutoff date
cutoff = "2011-01-08"
#sort dataframe because unsorted dates will not plot properly
df = df.sort_values(["Date"])
#plot the whole dataframe in yellow
plt.plot(df.Date, df.Score, c = "y", label = "before {}".format(cutoff))
#plot the conditional data on top in red
plt.plot(df[df.Date >= cutoff].Date, df[df.Date >= cutoff].Score, c = "r", label = "after {}".format(cutoff))
plt.xticks(rotation = 45)
plt.legend()
plt.show()

输出:

enter image description here

答案 1 :(得分:1)

我试图让其他人可以继续,因为在groupby之后没有连接线。

解决方法是使用介于这些日期之间的另一个掩码。

无论如何,完整的例子如下:

import pandas as pd
import matplotlib.pyplot as plt

csvdata = '''\
Date          Score
11-01-02      40
11-01-03      47
11-01-04      41
11-01-05      35
11-01-06      52
11-01-07      47
11-01-08      45
11-01-09      43
11-01-10      40
11-01-11      41
11-01-12      41
11-01-13      49
11-01-14      40
11-01-15      40'''

# Recreate data and convert Date to datetime
fileobj = pd.compat.StringIO(csvdata)
df = pd.read_csv(fileobj, sep='\s+')
df['Date'] = pd.to_datetime(df['Date'], yearfirst=True)

# Based on the date provided by OP, either RED or YELLOW to col Color
cond = df.Date >= '2011-01-08'
df['Color'] = np.where(cond, 'RED', 'YELLOW')

# Create the frame 
fig, ax = plt.subplots(figsize=(8,6))

# Fill the frame with data (note: missing datapoint!)
for color, dfx in df.groupby('Color'):
   dfx.plot(x='Date', y='Score', color=color, ax=ax) 

# The workaround --> does not feel reliable for consistant use though.
m1 = df['Date'].between('2011-01-07', '2011-01-08')
df[m1].plot(x='Date',y='Score', color=df['Color'], ax=ax)

plt.show()

enter image description here