如何组合线图& Seaborn/Matplotlib中的传奇
我会有这样的数据框
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
单独绘制。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用纯Matplotlib。作为UP系列的一部分,在UP之后选择行,以确保加入积分。对于传说,添加一个以要跳过的下划线开头的标签:
输出:
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:
output: