首页 > 解决方案 > How can I add points onto a seaborn barplot?

问题描述

How can I put points onto a seaborn bar plot? 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()

enter image description here

How can I now add the data in points_df as red points on this graph?

标签: pythonmatplotlibplotseaborn

解决方案


这是你想要的?

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)

出于某种原因,我不得不改变zorder点的,否则有些会被隐藏在栏杆后面

在此处输入图像描述


推荐阅读