Python 在matplotlib中绘制具有不同颜色的图形
我有下表Python 在matplotlib中绘制具有不同颜色的图形,python,pandas,dataframe,graph,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
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中执行此操作,但我不确定pandas中是否有类似的功能。我尝试了此操作,其他人可能会继续,因为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()
我做了一次尝试,其他人可能会继续尝试,因为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()
另一种方法是在整个曲线顶部绘制条件部分:
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()
输出:
另一种方法是在整个曲线顶部绘制条件部分:
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()
输出:
好的,所以同一行有不同的颜色。我想我们需要创建两行并合并它们。你知道在R怎么做吗?那么为什么不分享这些知识呢?@AntonvBR我没有将它们合并到R中。在颜色发生变化时,这条线没有连接。对不起,我没有澄清那部分。谢谢你的回答。好的,同一行不同的颜色。我想我们需要创建两行并合并它们。你知道在R怎么做吗?那么为什么不分享这些知识呢?@AntonvBR我没有将它们合并到R中。在颜色发生变化时,这条线没有连接。对不起,我没有澄清那部分。谢谢你的回答。