如何通过matplotlib缩放直方图绘图

3 投票
1 回答
8092 浏览
提问于 2025-04-18 10:47

你可以看到下面有一个直方图。
它是通过以下代码生成的:pl.hist(data1,bins=20,color='green',histtype="step",cumulative=-1)
那么,如何调整这个直方图的高度呢?
比如说,让直方图的高度变成现在的三分之一。

另外,有没有办法去掉左边的那条竖线呢?

enter image description here

1 个回答

7

matplotlib中的hist其实就是在调用其他一些函数。直接使用这些函数通常会更简单,这样你可以查看数据并直接修改它:

# Generate some data
data = np.random.normal(size=1000)

# Generate the histogram data directly
hist, bin_edges = np.histogram(data, bins=10)

# Get the reversed cumulative sum
hist_neg_cumulative = [np.sum(hist[i:]) for i in range(len(hist))]

# Get the cin centres rather than the edges
bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2.

# Plot
plt.step(bin_centers, hist_neg_cumulative)

plt.show()

hist_neg_cumulative是要绘制的数据数组。所以在把它传给绘图函数之前,你可以根据自己的需要调整它的规模。这样做也不会绘制出竖线。

撰写回答