Matplotlib:在两点之间绘制一条线......但以一种不寻常的方式

时间:2021-01-12 23:29:07

标签: python matplotlib matplotlib-basemap

我正在学习使用 Matplotlib 可视化数据,我想以非常定制的方式绘制一条线。我是 Matplotlib 的新手,我不知道我想做的事情是否可行。这是:

设置:假设您有一组随机 (x,y) 点用于线图。出于说明目的,我将 4 个不同的折线图压缩成一个图,但想象一下,如果下面的 4 条线中的每一条都分解成各自的图。

[![在此处输入图片描述][1]][1]

第 1 步)对于每个线图 A、B、C、D 转到全局最大值并将其称为“X”。

第 4 步可能吗?)画一条连接“X”和“Y”的线。是否可以在 matplotlib 中绘制这条线?谢谢。

相关代码如下:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(50, 4), 
        index=pd.date_range('1/1/2000', periods=50), columns=list('ABCD'))
df = df.cumsum()
df.plot();

1 个答案:

答案 0 :(得分:1)

我认为您需要定义计算局部最大值的特定逻辑,而不仅仅是使用“时钟”比喻,但是一旦您使用 scipipandas 或其他库定义了该逻辑,您就可以像我一样创建一个数据框。从那里,您应该能够从下面产生结果。

如果您对 seaborn(建立在 matplotlib 之上)没问题,我认为它会更容易一些,因为您可以传递 hue 参数来创建所有一行代码中每个类别的行。您需要使用要绘制的这些 lines 创建一个新数据框。我通过对值进行排序并获取每组的尾值来做到这一点。请参阅下面的可重现示例。

示例 1(绘制局部最大值):

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import seaborn as sns
fig, ax = plt.subplots()
plt.style.use('classic')
df = pd.DataFrame(np.random.randn(50, 4), 
        index=pd.date_range('1/1/2000', periods=50), columns=list('ABCD'))
df = df.cumsum()
df = df.melt(ignore_index=False).reset_index()
sns.lineplot(data=df, x="index", y="value", hue="variable", ax=ax)
lines_max = (df.sort_values('value').groupby("variable").tail(1)).sort_values('variable')
lines_local_max = df[((df['variable'] == lines_max['variable'].iloc[0]) & (df['index'] > lines_max['index'].iloc[0]))
                | ((df['variable'] == lines_max['variable'].iloc[1]) & (df['index'] > lines_max['index'].iloc[1]))
                | ((df['variable'] == lines_max['variable'].iloc[2]) & (df['index'] > lines_max['index'].iloc[2]))
                | ((df['variable'] == lines_max['variable'].iloc[3]) & (df['index'] > lines_max['index'].iloc[3]))]
lines_local_max = (lines_local_max.sort_values(['variable', 'value']).groupby("variable").tail(1))
lines = lines_max.append(lines_local_max).sort_values('variable')
lines
sns.lineplot(data=lines, x="index", y="value", hue="variable", marker="o",
             style='variable', dashes=[(2, 2), (2, 2), (2, 2), (2, 2)], legend=False, ax=ax)
x_dates = pd.to_datetime(df['index'].unique())
plt.xticks(x_dates[0::7], rotation=45, ha='center')
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b-%d-%Y'))

enter image description here

示例 2(只是画一条线到最后,没有定义局部最大值——目的只是告诉你如何从最大点到另一个定义的点画一条线):

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
plt.style.use('classic')
fmri = sns.load_dataset("fmri")
fmri = fmri.groupby(['event', 'timepoint'], as_index=False)['signal'].mean()
sns.lineplot(data=fmri, x="timepoint", y="signal", hue="event")
lines_max = (fmri.sort_values('signal').groupby("event").tail(1))
lines_last = (fmri.sort_values('timepoint').groupby("event").tail(1))
lines = lines_max.append(lines_last)
sns.lineplot(data=lines, x="timepoint", y="signal", hue="event", marker="o", style='event', dashes=[(2, 2), (2, 2)])

enter image description here

示例 3(另一个示例使用您提供的数据绘制一条线到最后,而不定义局部最大值 - 目的只是向您展示如何从最大值点到另一个点绘制一条线定义点):使用您提供的数据的示例:

import pandas as pd
import numpy as np
fig, ax = plt.subplots()
df = pd.DataFrame(np.random.randn(50, 4), 
        index=pd.date_range('1/1/2000', periods=50), columns=list('ABCD'))
df = df.cumsum()
df = df.melt(ignore_index=False).reset_index()
sns.lineplot(data=df, x="index", y="value", hue="variable", ax=ax)
lines_max = (df.sort_values('value').groupby("variable").tail(1))
lines_last = (df.sort_values('index').groupby("variable").tail(1))
lines = lines_max.append(lines_last).sort_values('variable')
sns.lineplot(data=lines, x="index", y="value", hue="variable", marker="o",
             style='variable', dashes=[(2, 2), (2, 2), (2, 2), (2, 2)], legend=False, ax=ax)
x_dates = df['index'].dt.strftime('%Y-%m-%d').sort_values().unique()
ax.set_xticklabels(labels=x_dates, rotation=45, ha='center')

enter image description here