简体   繁体   English

如何在seaborn条形图上添加点?

[英]How can I add points onto a seaborn barplot?

How can I put points onto a seaborn bar plot?如何将点放在seaborn条形图上? The following code produces the bar plot.以下代码生成条形图。

import matplotlib.pyplot as plt
import seaborn
import matplotlib
matplotlib.use('TkAgg')
seaborn.set_context('talk')

data_df = pandas.DataFrame([3, 1, 2, 4], index=['a', 'b', 'c', 'd']).transpose()
points_df = pandas.DataFrame([3.5, 0.5, 1.75, 4.25], index=['a', 'b', 'c', 'd']).transpose()
plt.figure()
seaborn.barplot(data=data_df)

plt.show()

在此处输入图片说明

How can I now add the data in points_df as red points on this graph?我现在如何将points_df的数据添加为该图上的红点?

Is this what you are looking for?这是你想要的?

data_df = pd.DataFrame([3, 1, 2, 4], index=['a', 'b', 'c', 'd']).transpose()
points_df = pd.DataFrame([3.5, 0.5, 1.75, 4.25], index=['a', 'b', 'c', 'd']).transpose()

plt.figure()
sns.barplot(data=data_df)
sns.scatterplot(data=points_df.T, legend=False, zorder=10)

for some reason, I had to change the zorder of the points, otherwise some would be hidden behind the bars出于某种原因,我不得不改变点的zorder ,否则有些会隐藏在酒吧后面

在此处输入图片说明

You can use the pointplot from seaborn for plotting the red points using join=False and using zorder to bring the dots to the front.您可以使用pointplotseaborn使用join=False绘制红点,并使用zorder将点带到前面。 The key here is to define an axis instance ax and pass it to both the plots.这里的关键是定义一个轴实例ax并将其传递给两个图。

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import matplotlib
matplotlib.use('TkAgg')
seaborn.set_context('talk')

fig, ax = plt.subplots()

data_df = pd.DataFrame([3, 1, 2, 4], index=['a', 'b', 'c', 'd']).transpose()
points_df = pd.DataFrame([3.5, 0.5, 1.75, 4.25], index=['a', 'b', 'c', 'd']).transpose()
sns.barplot(data=data_df, ax=ax, zorder=0)
sns.pointplot(data=points_df, join=False, color='red', ax=ax, zorder=1)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM