我有5个数据集,要从中创建5个单独的直方图。目前,他们都在一张图表上。如何更改此参数,以便生成两个单独的图形?
为简单起见,在下面的示例中,我仅显示两个直方图。我正在查看a
3个不同时间的角度分布,并且角度相同b
。
n, bins, patches = plt.hist(a)
plt.xlabel('Angle a (degrees)')
plt.ylabel('Frequency')
n, bins, patches = plt.hist(b)
label='2pm,3pm,4pm'
loc = 'center'
plt.legend(label, loc)
plt.xlabel('Angle b(degrees)')
plt.title('Histogram of b')
plt.ylabel('Frequency')
label='2pm,3pm,4pm'
loc = 'center'
plt.legend(label, loc)
plt.show()
这可能是您要使用matplotlib的面向对象接口时。有几种方法可以解决此问题。
首先,您可能希望每个图都在一个完全独立的图形上。在这种情况下,matplotlib可让您跟踪各种数字。
import numpy as np
import matplotlib.pyplot as plt
a = np.random.normal(size=200)
b = np.random.normal(size=200)
fig1 = plt.figure()
ax1 = fig1.add_subplot(1, 1, 1)
n, bins, patches = ax1.hist(a)
ax1.set_xlabel('Angle a (degrees)')
ax1.set_ylabel('Frequency')
fig2 = plt.figure()
ax2 = fig2.add_subplot(1, 1, 1)
n, bins, patches = ax2.hist(b)
ax2.set_xlabel('Angle b (degrees)')
ax2.set_ylabel('Frequency')
或者,您可以将图形分为多个子图,并在每个子图上绘制直方图。在这种情况下,matplotlib可让您跟踪各个子图。
fig = plt.figure()
ax1 = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 2)
n, bins, patches = ax1.hist(a)
ax1.set_xlabel('Angle a (degrees)')
ax1.set_ylabel('Frequency')
n, bins, patches = ax2.hist(b)
ax2.set_xlabel('Angle b (degrees)')
ax2.set_ylabel('Frequency')
回答这个问题可以解释中的数字add_subplot
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句