简体   繁体   English

matplotlib:在散点图上方绘制直方图

[英]matplotlib: plotting histogram plot just above scatter plot

I would like to make beautiful scatter plots with histograms above and right of the scatter plot, as it is possible in seaborn with jointplot:我想用散点图上方和右侧的直方图制作漂亮的散点图,因为在带有联合图的 seaborn 中是可能的:

seaborn 联合情节

I am looking for suggestions on how to achieve this.我正在寻找有关如何实现这一目标的建议。 In fact I am having some troubles in installing pandas, and also I do not need the entire seaborn module事实上,我在安装 Pandas 时遇到了一些麻烦,而且我不需要整个 seaborn 模块

I encountered the same problem today.我今天遇到了同样的问题。 Additionally I wanted a CDF for the marginals.此外,我想要一个边缘的 CDF。

在此处输入图片说明

Code:代码:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np

x = np.random.beta(2,5,size=int(1e4))
y = np.random.randn(int(1e4))

fig = plt.figure(figsize=(8,8))
gs = gridspec.GridSpec(3, 3)
ax_main = plt.subplot(gs[1:3, :2])
ax_xDist = plt.subplot(gs[0, :2],sharex=ax_main)
ax_yDist = plt.subplot(gs[1:3, 2],sharey=ax_main)
    
ax_main.scatter(x,y,marker='.')
ax_main.set(xlabel="x data", ylabel="y data")

ax_xDist.hist(x,bins=100,align='mid')
ax_xDist.set(ylabel='count')
ax_xCumDist = ax_xDist.twinx()
ax_xCumDist.hist(x,bins=100,cumulative=True,histtype='step',density=True,color='r',align='mid')
ax_xCumDist.tick_params('y', colors='r')
ax_xCumDist.set_ylabel('cumulative',color='r')

ax_yDist.hist(y,bins=100,orientation='horizontal',align='mid')
ax_yDist.set(xlabel='count')
ax_yCumDist = ax_yDist.twiny()
ax_yCumDist.hist(y,bins=100,cumulative=True,histtype='step',density=True,color='r',align='mid',orientation='horizontal')
ax_yCumDist.tick_params('x', colors='r')
ax_yCumDist.set_xlabel('cumulative',color='r')

plt.show()

Hope it helps the next person searching for scatter-plot with marginal distribution.希望它可以帮助下一个人搜索具有边际分布的散点图。

Here's an example of how to do it, using gridspec.GridSpec :这是使用gridspec.GridSpec进行操作的gridspec.GridSpec

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
import numpy as np

x = np.random.rand(50)
y = np.random.rand(50)

fig = plt.figure()

gs = GridSpec(4,4)

ax_joint = fig.add_subplot(gs[1:4,0:3])
ax_marg_x = fig.add_subplot(gs[0,0:3])
ax_marg_y = fig.add_subplot(gs[1:4,3])

ax_joint.scatter(x,y)
ax_marg_x.hist(x)
ax_marg_y.hist(y,orientation="horizontal")

# Turn off tick labels on marginals
plt.setp(ax_marg_x.get_xticklabels(), visible=False)
plt.setp(ax_marg_y.get_yticklabels(), visible=False)

# Set labels on joint
ax_joint.set_xlabel('Joint x label')
ax_joint.set_ylabel('Joint y label')

# Set labels on marginals
ax_marg_y.set_xlabel('Marginal x label')
ax_marg_x.set_ylabel('Marginal y label')
plt.show()

在此处输入图片说明

I strongly recommend to flip the right histogram by adding these 3 lines of code to the current best answer before plt.show() :我强烈建议通过将这 3 行代码添加到plt.show()之前的当前最佳答案来翻转正确的直方图:

ax_yDist.invert_xaxis()
ax_yDist.yaxis.tick_right()
ax_yCumDist.invert_xaxis()

翻转正确的直方图后

The advantage is that any person who is visualizing it can compare easily the two histograms just by moving and rotating clockwise the right histogram on their mind.优点是任何可视化它的人都可以轻松地比较这两个直方图,只需在他们的脑海中顺时针移动和旋转右侧的直方图。

On contrast, in the plot of the question and in all other answers, if you want to compare the two histograms, your first reaction is to rotate the right histogram counterclockwise, which inverts the y axis.相比之下,在问题的情节和所有其他答案中,如果要比较两个直方图,您的第一反应是逆时针旋转右侧的直方图,这会反转 y 轴。 Indeed, the right CDF of the current best answer looks decreasing at first sight:事实上,当前最佳答案的正确 CDF 乍一看似乎在下降:

在翻转正确的直方图之前

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

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