matplotlib 试图构建条形图的2面板子图,但条形图似乎在第二面板中发生了移动

bejyjqdl  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(113)

我试图创建RMSE和相关值的双面板子图,并将条形图分组(这样每个组合有4个条形图)。然而,在第二个面板中,条形图分组的位置已经偏移,并且xlabels的顺序变得混乱。
你知道这里会发生什么吗?

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.cbook as cbook
import numpy as np
import scipy
import pandas as pd
from itertools import combinations

### Create Plot ###
matplotlib.rc('xtick', labelsize=22) 
matplotlib.rc('ytick', labelsize=22)
    
combos = {'1 Product', '2 Product', '3 Product','4 Product', '5 Product', '6 Product','7 Product'}
    
top30_rmse = {'RMSE Cold Season': [4.91726910421414,3.92792247654507,3.53717233977146,3.32461948965479,3.19029758875898,3.0975153495569,3.02950308430467],
             'RMSE Warm Season': [4.436715943,3.767955663,3.516888357,3.384377437,3.302319607,3.246462314,3.205968468]}

top30_corr = {'Correlation Cold Season': [0.639808613,0.71965916,0.75785035,0.780044305,0.794336664,0.80422278,0.811432104],
            'Correlation Warm Season': [0.888086922,0.936554339,0.952068621,0.958842973,0.962550154,0.964870985,0.966455676]}

depth_rmse = {'RMSE Cold Season': [3.564576325,3.000595232,2.787357328,2.6743701,2.60422583,2.556393944,2.521672889],
               'RMSE Warm Season': [4.706464351,4.416909958,4.316077216,4.264766936,4.233682287,4.212831767,4.197875134]}

depth_corr = {'Correlation Cold Season': [0.822695625,0.855445131,0.868612894,0.875592404,0.87985752,0.882709683,0.884740798],
            'Correlation Warm Season': [0.858527248,0.880016071,0.887233404,0.890810139,0.892940312,0.894352646,0.89535723]}

x = np.arange(1,8,1)

width = 0.1
multiplier = 0
    
fig = plt.subplots(nrows=2,ncols=1,sharex='all',figsize=(20,20))

## Top-30cm ##
#RMSE#
ax1 = plt.subplot(211)
for stat, group in top30_rmse.items():
    offset = width * multiplier
    rects = ax1.bar(x + offset, group, width, label=stat)
    multiplier += 1
ax1.set_xticks(x + width, combos)
ax1.set_ylim(0,10)
                       
#CORR#
ax2 = ax1.twinx()
for stat, group in top30_corr.items():
    offset = width * multiplier
    rects = ax2.bar(x + offset, group, width,  hatch='x', label=stat)
    multiplier += 1    

ax2.set_ylim(0.5,1)
                       
## Depth ##
#RMSE#
ax3 = plt.subplot(212)
for stat, group in depth_rmse.items():
    offset = width * multiplier
    rects = ax3.bar(x + offset, group, width, label=stat)
    multiplier += 1
ax3.set_xticks(x + width, combos)
ax3.set_ylim(0,10)

#CORR#
ax4 = ax3.twinx()
for stat, group in depth_corr.items():
    offset = width * multiplier
    rects = ax4.bar(x + offset, group, width,  hatch='x', label=stat)
    multiplier += 1                        

ax4.set_ylim(0.5,1)          

plt.tight_layout()
plt.show()

7dl7o3gd

7dl7o3gd1#

更新您的两个问题的更完整的答案:
1.您的条形偏移问题:看起来它与您的multiplier变量有关,因为您正在修改它,然后重新使用它。只需在ax3 = plt.subplot(212)行上方使用multiplier=0重置它即可解决此问题。
1.将combos更改为列表而不是集合以解决此问题。

相关问题