gpt4 book ai didi

python - 向程序添加子图会改变直方图的显示方式

转载 作者:太空宇宙 更新时间:2023-11-03 17:25:47 24 4
gpt4 key购买 nike

我正在使用 Pyplot 直方图的示例代码,取自 this page将 matplotlib 网站作为构建其他内容的起点。

当我想修改它以使用子图(目的是使用其他子图来显示其他类型的图)时,如下面的代码所示,输出全部搞砸了,直方图分布在多个图上,甚至虽然我只引用第一个元素。我错过了什么?

Display showing the different subplots being used

原代码:

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

# example data
mu = 100 # mean of distribution
sigma = 15 # standard deviation of distribution
x = mu + sigma * np.random.randn(10000)

num_bins = 50
# the histogram of the data
n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5)
# add a 'best fit' line
y = mlab.normpdf(bins, mu, sigma)
plt.plot(bins, y, 'r--')
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')

# Tweak spacing to prevent clipping of ylabel
plt.subplots_adjust(left=0.15)
plt.show()

修改后的代码:

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

f, axarr = plt.subplots(3, sharex=False, sharey=False)
# example data
mu = 100 # mean of distribution
sigma = 15 # standard deviation of distribution
x = mu + sigma * np.random.randn(10000)
num_bins = 50
# the histogram of the data
n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5)
# add a 'best fit' line
y = mlab.normpdf(bins, mu, sigma)
axarr[0].plot(bins, y, 'r--')
axarr[0].set_title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')
plt.show()

最佳答案

您需要了解子图的工作原理。看看一些例子 here

这里是让 3 个子图做不同事情的代码。在这里,我再次绘制了相同的直方图 3 次,但您可以根据需要进行更改。

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(311) # This represents a (3x1) grid (row x col) and we are plotting the (1) subplot. The last number increments row-wise.

# example data
mu = 100 # mean of distribution
sigma = 15 # standard deviation of distribution
x = mu + sigma * np.random.randn(10000)
num_bins = 50

# the histogram of the data
n, bins, patches = ax.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5)
# add a 'best fit' line
y = mlab.normpdf(bins, mu, sigma)
ax.plot(bins, y, 'r--')
ax.set_title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')

ax2 = fig.add_subplot(312) # Second subplot

# example data
mu = 100 # mean of distribution
sigma = 15 # standard deviation of distribution
x = mu + sigma * np.random.randn(10000)
num_bins = 50

# the histogram of the data
n, bins, patches = ax2.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5)
# add a 'best fit' line
y = mlab.normpdf(bins, mu, sigma)
ax2.plot(bins, y, 'r--')

ax3 = fig.add_subplot(313) # And the third subplot

# example data
mu = 100 # mean of distribution
sigma = 15 # standard deviation of distribution
x = mu + sigma * np.random.randn(10000)
num_bins = 50

# the histogram of the data
n, bins, patches = ax3.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5)
# add a 'best fit' line
y = mlab.normpdf(bins, mu, sigma)
ax3.plot(bins, y, 'r--')
plt.show()

Histograms in different subplots

关于python - 向程序添加子图会改变直方图的显示方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32584692/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com