python - Matplotlib:具有两个不同 y 轴的 2D 子图
问题描述
我想结合两种解决方案。
我有这样的数据:
db = np.array([('Billboard', 1, 520.0, 3),
('Billboard', 2, 520.0, 2),
('Billboard', 3, 612.0, 0),
('Billboard', 4, 410.0, 4),
('Careerbuilder', 1, 410.0, 0),
('Careerbuilder', 2, 820.0, 0),
('Careerbuilder', 3, 410.0, 1),
('Careerbuilder', 4, 820.0, 0),
('Monster.com', 1, 500.0, 3),
('Monster.com', 2, 500.0, 4),
('Monster.com', 3, 450.0, 0),
('Monster.com', 4, 450.0, 7),
('Ads', 1, 120.0, 0),
('Ads', 2, 0.0, 1),
('Ads', 3, 50.0, 1),
('Ads', 4, 100.0, 0),
], dtype=[('Source', 'U20'), ('Month', int), ('Spent', float), ('count', int)])
db = pd.DataFrame(db)
解决方案 1:良好的布局,但蓝色图不可读,因为与条形图相比,值太小。
plt.figure(figsize=(10, 5))
for i, sourse in enumerate(db['Source'].unique()):
plt.subplot(2, 2, i+1)
plt.title(db['Source'].unique()[i])
subdf = db[db['Source'] == sourse][['Month','count', 'Spent']].set_index('Month')
plt.plot(subdf.index, subdf['count'], color='blue')
plt.bar(subdf.index, subdf['Spent'], color='red', alpha=0.5)
plt.show()
解决方案 2:添加另一个 y 轴后,蓝色图正确显示,但布局是垂直的,不是很好。
for i, sourse in enumerate(db['Source'].unique()):
fig, ax = plt.subplots()
plt.title(db['Source'].unique()[i])
subdf = db[db['Source'] == sourse][['Month','count', 'Spent']].set_index('Month')
ax.bar(subdf.index, subdf['Spent'], color='red', alpha=0.5)
ax.set_xlabel("Months",fontsize=14)
ax.set_ylabel("Money spent",color="red",fontsize=14)
ax.set_xticks(list(range(1,5)))
ax2 = ax.twinx()
ax2.plot(subdf.index, subdf['count'], color='blue')
ax2.yaxis.set_major_locator(ticker.MultipleLocator(1.00))
ax2.set_ylabel('People hired', color='blue',fontsize=14)
plt.ylim(0)
plt.show()
所以我有20多个子图,所以垂直布局不是最好的解决方案。但我想不出同时使用这两种解决方案的方法......我能想到的最好的办法是
m = 0
n = 0
fig, ax = plt.subplots(2, 2, sharex='col', sharey=False, figsize=(10, 5))
for i, sourse in enumerate(db['Source'].unique()):
plt.title(db['Source'].unique()[i])
subdf = db[db['Source'] == sourse][['Month','count', 'Spent']].set_index('Month')
ax[i+m,i+n].bar(subdf.index, subdf['Spent'], color='red', alpha=0.5)
ax.set_xlabel("Months",fontsize=14)
ax.set_ylabel("Money spent",color="red",fontsize=14)
ax2 = ax.twinx()
ax2[i+m,i+n].plot(subdf.index, subdf['count'], color='blue')
ax2.yaxis.set_major_locator(ticker.MultipleLocator(1.00))
ax2.set_ylabel('People hired', color='blue',fontsize=14)
plt.xticks(list(range(1, 13)))
plt.ylim(0)
n += 1
if n == 2:
m += 1
n == 0
plt.show()
但它显示一个错误
AttributeError Traceback (most recent call last)
<ipython-input-142-aa24c581b7bf> in <module>
8
9 ax[i+m,i+n].bar(subdf.index, subdf['Spent'], color='red', alpha=0.5)
---> 10 ax.set_xlabel("Months",fontsize=14)
11 ax.set_ylabel("Money spent",color="red",fontsize=14)
12
AttributeError: 'numpy.ndarray' object has no attribute 'set_xlabel'
为此我找到了这个答案AttributeError: 'numpy.ndarray' object has no attribute 'plot'但不知道如何在这里应用它!
解决方案
我稍微修改了您的解决方案 #1 并应用了双 y 轴图。
plt.figure(figsize=(10, 5))
for i, sourse in enumerate(db['Source'].unique()):
plt.subplot(2, 2, i+1)
plt.title(db['Source'].unique()[i])
subdf = db[db['Source'] == sourse][['Month','count', 'Spent']].set_index('Month')
plt.bar(subdf.index, subdf['Spent'], color='red', alpha=0.5)
ax = plt.gca() # get current axis
twin_ax = ax.twinx()
twin_ax.plot(subdf.index, subdf['count'], color='blue')
plt.tight_layout()
plt.show()
我还可以解释您上一个解决方案中的错误。在下一行
fig, ax = plt.subplots(2, 2, sharex='col', sharey=False, figsize=(10, 5))
这ax
是一个 numpy 数组。它的形状是(2, 2)
,它有点像,
ax = np.array([
[ax_row_0_col_0, ax_row_0_col1],
[ax_row_1_col_0, ax_row_1_col1],
])
在这种情况下,我们不能做
ax.set_xlabel("Months",fontsize=14)
但我们必须做类似的事情
row, col = 0, 0
ax[row, col].set_xlabel("Months",fontsize=14)
推荐阅读
- node.js - Angular 6:如何使用 formControlName 在“选择”下拉列表中设置默认值?
- python - 通过 pyspark py4jjavaerror 使用 spark 加载 csv 文件
- azure - 将密钥保管库生成的证书与服务主体链接
- aws-lambda - 无法从云形成 yaml 中的条件函数返回整数
- julia - Julia 1.0.3 中具有多个条件的 findall() 或 filter() 函数
- dart - Flutter中如何自定义SliverAppBar中的flexibleSpace属性?
- c - 在 C 中未显示文本和背景(sdl2 和 sdl2_ttf)
- typescript - TypeScript 中的动态类型
- python - 如何减去列表中的元组?
- ionic4 - Ionic 4,如何全局更改动画的持续时间?