如何使用matplotlib中的直方图输出绘制散点图?

时间:2013-08-20 01:42:13

标签: python matplotlib scatter-plot

我想绘制一个与此类似的散点图:

enter image description here

我可以从我的数据中绘制直方图,但我想要一个相同数据的散点图。有没有什么办法可以使用hist()方法输出作为散点图的输入?或者用其他方法在matplotlib中使用hist()方法绘制散点图? 该代码用于绘制直方图如下:

data = get_data()
plt.figure(figsize=(7,4))
ax = plt.subplots()
plt.hist(data,histtype='bar',bins = 100,log=True)
plt.show()

1 个答案:

答案 0 :(得分:3)

我认为您正在寻找以下内容:

基本上plt.hist()输出两个数组(并且Nordev指出了一些补丁)。第一个是每个箱子中的计数(n),第二个是箱子的边缘。

import matplotlib.pylab as plt
import numpy as np

# Create some example data
y = np.random.normal(5, size=1000)

# Usual histogram plot
fig = plt.figure()
ax1 = fig.add_subplot(121)
n, bins, patches = ax1.hist(y, bins=50)  # output is two arrays

# Scatter plot
# Now we find the center of each bin from the bin edges
bins_mean = [0.5 * (bins[i] + bins[i+1]) for i in range(len(n))]
ax2 = fig.add_subplot(122)
ax2.scatter(bins_mean, n)

Example

如果没有对问题的更多描述,这是我能想到的最好的。对不起,如果我误解了。