简体   繁体   English

轴内特定坐标的直方图

[英]Histogram at specific coordinates inside axes

What I want to achieve with Python 3.6 is something like this : 我想用Python 3.6实现的是这样的: 直方图和散点在同一轴上

Obviously made in paint and missing some ticks on the xAxis. 显然是用油漆制成的,并且在xAxis上缺少一些刻度。 Is something like this possible? 这样的事情可能吗? Essentially, can I control exactly where to plot a histogram (and with what orientation)? 本质上,我是否可以精确控制直方图的绘制位置(以及方向)?

I specifically want them to be on the same axes just like the figure above and not on separate axes or subplots. 我特别希望它们与上图一样在相同的轴上,而不是在单独的轴或子图上。

fig = plt.figure()
ax2Handler = fig.gca()
ax2Handler.scatter(np.array(np.arange(0,len(xData),1)), xData)
ax2Handler.hist(xData,bins=60,orientation='horizontal',normed=True)

This and other approaches (of inverting the axes) gave me no results. 这种和其他方法(使轴反转)都没有结果。 xData is loaded from a panda dataframe. xData是从熊猫数据帧加载的。

# This also doesn't work as intended
fig = plt.figure()
axHistHandler = fig.gca()
axScatterHandler = fig.gca()
axHistHandler.invert_xaxis()
axHistHandler.hist(xData,orientation='horizontal')
axScatterHandler.scatter(np.array(np.arange(0,len(xData),1)), xData)

A. using two axes A.使用两个轴

There is simply no reason not to use two different axes. 根本没有理由不使用两个不同的轴。 The plot from the question can easily be reproduced with two different axes: 可以很容易地使用两个不同的轴来复制问题中的图:

import numpy as np
import matplotlib.pyplot as plt
plt.style.use("ggplot")
xData = np.random.rand(1000)

fig,(ax,ax2)= plt.subplots(ncols=2, sharey=True)
fig.subplots_adjust(wspace=0)

ax2.scatter(np.linspace(0,1,len(xData)), xData, s=9)
ax.hist(xData,bins=60,orientation='horizontal',normed=True)
ax.invert_xaxis()
ax.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax2.tick_params(axis="y", left=0)

plt.show()

在此处输入图片说明

B. using a single axes B.使用单轴

Just for the sake of answering the question: In order to plot both in the same axes, one can shift the bars by their length towards the left, effectively giving a mirrored histogram. 只是为了回答这个问题:为了在同一根轴上绘制两个图形,可以将条形图的长度向左移动,从而有效地绘制出镜像的直方图。

import numpy as np
import matplotlib.pyplot as plt
plt.style.use("ggplot")
xData = np.random.rand(1000)

fig,ax= plt.subplots(ncols=1)
fig.subplots_adjust(wspace=0)

ax.scatter(np.linspace(0,1,len(xData)), xData, s=9)
xlim1 = ax.get_xlim()
_,__,bars = ax.hist(xData,bins=60,orientation='horizontal',normed=True)

for bar in bars:
    bar.set_x(-bar.get_width())

xlim2 = ax.get_xlim()
ax.set_xlim(-xlim2[1],xlim1[1])

plt.show()

在此处输入图片说明

You might be interested in seaborn jointplots: 您可能对海底联合绘图感兴趣:

# Import and fake data
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
data = np.random.randn(2,1000)
# actual plot
jg = sns.jointplot(data[0], data[1], marginal_kws={"bins":100})
jg.ax_marg_x.set_visible(False) # remove the top axis
plt.subplots_adjust(top=1.15) # fill the empty space

produces this: 产生这个: 在此处输入图片说明

See more examples of bivariate distribution representations , available in Seaborn. 请参阅Seaborn中提供的更多的双变量分布表示示例。

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

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