【问题标题】:matplotlib: plotting histogram plot just above scatter plotmatplotlib:在散点图上方绘制直方图
【发布时间】:2016-05-03 15:20:55
【问题描述】:

我想用散点图上方和右侧的直方图制作漂亮的散点图,因为在 seaborn 中可以使用联合图:

我正在寻找有关如何实现这一目标的建议。实际上我在安装 pandas 时遇到了一些麻烦,而且我不需要整个 seaborn 模块

【问题讨论】:

  • 说清楚,你的问题是如何在 vanilla matplotlib 中实现sns.jointplot
  • 或多或少。我的问题是如何在散点图上方放置另一个框,以便在那里绘制直方图
  • 查看matplotlib.gridspec.GridSpec,特别是底部的示例。没有gridspec,你可以关注这个clear example
  • 此外,这里有一个关于 stackoverflow 的类似示例:stackoverflow.com/questions/20525983/…

标签: python matplotlib


【解决方案1】:

我今天遇到了同样的问题。此外,我想要一个 CDF 用于边缘。

代码:

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()

希望它有助于下一个人搜索具有边缘分布的散点图。

【讨论】:

  • 你的照片很漂亮,+1,但代码返回错误:AttributeError: 'Polygon' object has no property 'normed'。请更正您的解决方案或告诉我我做错了什么。
  • 想通了:用density=True替换normed=True
  • 非常感谢您的出色回答!
【解决方案2】:

下面是一个使用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()

【讨论】:

  • 不错,但是如何仅从直方图中删除刻度(不抑制轴),以及如何有选择地添加标签?
  • 现在我的标签出现在图 [0,0] 而不是 [1,0] 上。我想要图 [0,0] 上的 ylabel,图 [1,1] 上的 xlabel,以及图 [1,0] 上的两个标签
【解决方案3】:

我强烈建议通过将这 3 行代码添加到 current best answer 之前的 plt.show() 来翻转正确的直方图:

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

优点是任何人只要在脑海中移动和顺时针旋转正确的直方图,就可以轻松地比较这两个直方图。

相比之下,在问题的情节和所有其他答案中,如果您想比较两个直方图,您的第一反应是逆时针旋转右直方图,这会导致错误的结论,因为 y 轴倒置了。事实上,current best answer 的右 CDF 乍一看似乎在减少:

【讨论】:

    猜你喜欢
    • 2013-08-29
    • 1970-01-01
    • 2013-08-21
    • 2022-07-28
    • 1970-01-01
    • 2016-06-09
    • 2015-10-25
    相关资源
    最近更新 更多