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

use*_*257 5 python matplotlib scatter-plot

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

在此处输入图片说明

我可以从数据中绘制直方图,但我希望对同一数据进行散点图绘制。有什么方法可以将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()
Run Code Online (Sandbox Code Playgroud)

Gre*_*reg 5

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

本质上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)
Run Code Online (Sandbox Code Playgroud)

例子

这是我能想到的最好的方法,无需对问题进行更多描述。对不起,如果我误解了。