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

我想绘制一个类似于这个的散点图:

我可以从我的数据绘制直方图,但我想要一个相同数据的散点图.有没有什么办法可以使用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()

最佳答案 我想你正在寻找以下内容:

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

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)

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

点赞