如何组合线图& Seaborn/Matplotlib中的传奇

发布于 2025-02-04 18:05:22 字数 1612 浏览 2 评论 0 原文

我会有这样的数据框

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame({'A': [1,2,3,4,5,4,3,2,1],
                   'B': [200,300,400,800,1500,900,450,275,200],
                   'c': ['up','up','down','up','up','down','down','up','down'],
                   'd': ['r','r','r','r','r','r','r','r','r'],
                   })

当我使用代码 sns.lineplot(data = df,x ='a',y ='b',ci = none)>时, 。我得到了下面的情节。

如您所见,这些要点尚未完全绘制。当我使用下面的代码进行绘图时,

upDF = df.loc[df['c']=='up']
downDF = df.loc[df['c']=='down']
sns.lineplot(data=upDF, x='A', y='B',ci=None, label = 'up');
sns.lineplot(data=downDF, x='A', y='B',ci=None, label = 'down');
plt.legend()

我会得到像下面的图,

“在此处输入图像描述”

在此图中,所有点都绘制了,但它们'已断开连接'。我如何获得下面的绘图,并连接点;只有一个传奇(列'd'或一个常数可以用于图例)

”在此处输入图像描述

如果我在实际数据上使用 sns.lineplot ,我会在下面获得图形而不是 UP & down 单独绘制。

I have a dataframe like this

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame({'A': [1,2,3,4,5,4,3,2,1],
                   'B': [200,300,400,800,1500,900,450,275,200],
                   'c': ['up','up','down','up','up','down','down','up','down'],
                   'd': ['r','r','r','r','r','r','r','r','r'],
                   })

When I do a line plot with seaborn with the code sns.lineplot(data=df, x='A', y='B',ci=None). I get a plot like below.

enter image description here

As you can see, the points have not completely plotted. When I do the plot with the code below

upDF = df.loc[df['c']=='up']
downDF = df.loc[df['c']=='down']
sns.lineplot(data=upDF, x='A', y='B',ci=None, label = 'up');
sns.lineplot(data=downDF, x='A', y='B',ci=None, label = 'down');
plt.legend()

I get a plot like below

enter image description here

In this plot, all points are plotted, but they are 'disconnected'. How can I get a plot like below with the points connected & just only one legend (column 'D' or a constant can be used for legend)

enter image description here

If I use sns.lineplot on my actual data, I get the graph like below instead of up & down plotted separately.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴情 2025-02-11 18:05:22

您可以使用纯Matplotlib。作为UP系列的一部分,在UP之后选择行,以确保加入积分。对于传说,添加一个以要跳过的下划线开头的标签:

import matplotlib.pyplot as plt
m = df['c']=='up'
upDF = df.loc[m|m.shift()]
downDF = df.loc[~m]
ax = plt.subplot()
upDF.plot(x='A', y='B', label='r', ax=ax)
downDF.plot(x='A', y='B', label='_skip', ax=ax)
ax.legend()

输出:

“在此处输入图像说明”

You can use pure matplotlib. Select the rows after an up as part of the up Series to ensure joining the points. For the legend, add a label that starts with an underscore to be skipped:

import matplotlib.pyplot as plt
m = df['c']=='up'
upDF = df.loc[m|m.shift()]
downDF = df.loc[~m]
ax = plt.subplot()
upDF.plot(x='A', y='B', label='r', ax=ax)
downDF.plot(x='A', y='B', label='_skip', ax=ax)
ax.legend()

output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文