如何使用seaborn绘制成对的直方图

Sam Sirmaxford:

我想提出一个配对的直方图像所示的在这里使用seaborn distplot。这种曲线图也可以被称为后端到背面直方图示出此处,或bihistogram如所讨论的沿着x轴反向/镜像这里

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

green = np.random.normal(20,10,1000)
blue = np.random.poisson(60,1000)

fig, ax = plt.subplots(figsize=(8,6))

sns.distplot(blue, hist=True, kde=True, hist_kws={'edgecolor':'black'}, kde_kws={'linewidth':2}, bins=10, color='blue')
sns.distplot(green, hist=True, kde=True, hist_kws={'edgecolor':'black'}, kde_kws={'linewidth':2}, bins=10, color='green')
ax.set_xticks(np.arange(-20,121,20))
ax.set_yticks(np.arange(0.0,0.07,0.01))
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

plt.show()

这是输出: 海上分布图

当我使用此处讨论的方法(plt.barh)时,会得到下面显示的条形图,这不是我想要的。水平条形图

Or maybe I haven't understood the workaround well enough... A simple/short implementation of python-seaborn-distplot similar to these kinds of plots would be perfect. I edited the figure of my first plot above to show the kind of plot I hope to achieve (though y-axis not upside down): 配对直方图

Any leads would be greatly appreciated.

JohanC :

Here is a possible approach using seaborn's displots. Seaborn doesn't return the created graphical elements, but the ax can be interrogated. To make sure the ax only contains the elements you want upside down, those elements can be drawn first. Then, all the patches (the rectangular bars) and the lines (the curve for the kde) can be given their height in negative. Optionally the x-axis can be set at y == 0 using ax.spines['bottom'].set_position('zero').

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

green = np.random.normal(20, 10, 1000)
blue = np.random.poisson(60, 1000)

fig, ax = plt.subplots(figsize=(8, 6))

sns.distplot(green, hist=True, kde=True, hist_kws={'edgecolor': 'black'}, kde_kws={'linewidth': 2}, bins=10,
             color='green')
for p in ax.patches:  # turn the histogram upside down
    p.set_height(-p.get_height())
for l in ax.lines:  # turn the kde curve upside down
    l.set_ydata(-l.get_ydata())

sns.distplot(blue, hist=True, kde=True, hist_kws={'edgecolor': 'black'}, kde_kws={'linewidth': 2}, bins=10,
             color='blue')
ax.set_xticks(np.arange(-20, 121, 20))
ax.set_yticks(np.arange(0.0, 0.07, 0.01))
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

pos_ticks = np.array([t for t in ax.get_yticks() if t > 0])
ticks = np.concatenate([-pos_ticks[::-1], [0], pos_ticks])
ax.set_yticks(ticks)
ax.set_yticklabels([f'{abs(t):.2f}' for t in ticks])
ax.spines['bottom'].set_position('zero')

plt.show()

样例

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用seaborn仅绘制y轴

如何使用元组绘制直方图

使用Seaborn Python绘制CDF +累积直方图

如何使用matplotlib绘制collections.Counter直方图?

如何在与Seaborn相同的图上绘制多个直方图

如何与Matplotlib / Seaborn并排绘制两个堆叠的直方图

使用Seaborn为数据帧绘制直方图

如何使用ZingChart正确绘制直方图?

创建for循环以使用Seaborn绘制DataFrame的各个列的直方图

如何使用Seaborn连续绘制多个图形

如何使用Seaborn绘制阶跃函数?

如何使用Python绘制成本曲线

如何使用Folium绘制成对的起点和终点之间的路线?

如何使用gnuplot绘制直方图

使用python(matplotlib,seaborn或plotly)将全球COVID-19演变绘制成线

如何使用Matplotlib按DataFrame列的顺序绘制直方图网格?

如何使用matplotlib ArtistAnimation绘制直方图或条形动画?

如何使用seaborn绘制将直方图条围绕刻度线居中?堆叠条是必不可少的

如何在ggplot中的散点图中绘制成对的数据?

如何使用熊猫绘制时间间隔直方图?

我如何从数组中绘制成对的点

如何使用matplotlib绘制中心值不为零的垂直直方图

如何绘制这种直方图

如何使用 bitblt 在屏幕上绘制由数组制成的位图?

如何使用pandas在python的直方图上绘制特定范围的值?

在seaborn中绘制重叠直方图

在 Seaborn 直方图中绘制多个分布

在直方图中绘制成对的 bin 以与 seaborn 进行比较

如何使用列值的颜色函数绘制直方图?