Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib:在散点图上方绘制直方图图_Python_Matplotlib - Fatal编程技术网

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

Python matplotlib:在散点图上方绘制直方图图,python,matplotlib,Python,Matplotlib,我想在散点图的上方和右侧绘制漂亮的散点图,因为在seaborn with jointplot中这是可能的: 我正在寻找如何实现这一目标的建议。事实上,我在安装pandas时遇到了一些麻烦,而且我也不需要整个seaborn模块,下面是一个如何使用gridspec.gridspec进行安装的示例: import matplotlib.pyplot as plt from matplotlib.gridspec import GridSpec import numpy as np x = np.r

我想在散点图的上方和右侧绘制漂亮的散点图,因为在seaborn with jointplot中这是可能的:


我正在寻找如何实现这一目标的建议。事实上,我在安装pandas时遇到了一些麻烦,而且我也不需要整个seaborn模块,下面是一个如何使用
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()

我今天遇到了同样的问题。此外,我还想为边缘人提供CDF

代码:


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

说清楚,你的问题是如何在vanilla matplotlib中或多或少地实现
sns.jointplot
。我的问题是如何在散点图上方放置另一个框,这样我就可以在散点图上绘制直方图,特别是底部的示例。如果没有gridspec,您可以进一步了解这一点,这里有一个关于stackoverflow的类似示例:不错,但是如何仅从直方图中删除记号(而不抑制轴),以及如何有选择地添加标签?请参见此处,我的标签现在显示在绘图[0,0]而不是[1,0]上。我希望在绘图[0,0]上显示ylabel,在绘图[1,1]上显示xlabel,在绘图[1,0]上显示两个标签。您的图片很漂亮,+1,但代码返回一个错误:
AttributeError:“Polygon”对象没有属性“normed”
。请更正您的解决方案或告诉我我做错了什么。解决方法:将
normed=True
替换为
density=True
@Leo,更新答案。
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()