Python 多条条形图上带棘的多个Y轴

Python 多条条形图上带棘的多个Y轴,python,matplotlib,boxplot,Python,Matplotlib,Boxplot,我想在多个Y轴条形图上表示一些数据。目前,我只能在图1所示的线图上表示它们 下面是我的代码: import matplotlib.pyplot as plt import numpy as np import pandas as pd def make_patch_spines_invisible(ax): ax.set_frame_on(True) ax.patch.set_visible(False) for sp in ax.spines.values():

我想在多个Y轴条形图上表示一些数据。目前,我只能在图1所示的线图上表示它们

下面是我的代码:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

def make_patch_spines_invisible(ax):
    ax.set_frame_on(True)
    ax.patch.set_visible(False)
    for sp in ax.spines.values():
        sp.set_visible(False)

dataset = pd.read_csv('Model Selection (Humidity)_csv.csv')

feature1 = dataset.iloc[:5, 2].values
feature2 = dataset.iloc[:5, 3].values
feature3 = dataset.iloc[:5, 4].values
feature4 = dataset.iloc[:5, 5].values
xaxis = dataset.iloc[:5,1].values

fig, f1 = plt.subplots(figsize= (25,15))
fig.subplots_adjust(right=0.75)

f2 = f1.twinx()
f3 = f1.twinx()
f4 = f1.twinx()

# Offset the right spine of par2.  The ticks and label have already been
# placed on the right by twinx above.
f3.spines["right"].set_position(("axes", 1.1))
f4.spines["left"].set_position(("axes", -0.1))

# Having been created by twinx, par2 has its frame off, so the line of its
# detached spine is invisible.  First, activate the frame but make the patch
# and spines invisible.
make_patch_spines_invisible(f3)
make_patch_spines_invisible(f4)

# Second, show the right spine.
f3.spines["right"].set_visible(True)
f4.spines["left"].set_visible(True)
f4.yaxis.set_label_position('left')
f4.yaxis.set_ticks_position('left')

p1, = f1.plot(xaxis, feature1, 'r-', label="Adjusted R2")
p2, = f2.plot(xaxis, feature2, 'g-', label="Max Absolute Error")
p3, = f3.plot(xaxis, feature3, 'b-', label="Max Error")
p4, = f4.plot(xaxis, feature4, 'y-', label="Root Mean Square Error")

f1.set_ylim(0, 1)
f2.set_ylim(0, 2)
f3.set_ylim(7, 25)
f4.set_ylim(1, 3)

f1.set_xlabel("Model")
f1.set_ylabel("Adjusted R2")
f2.set_ylabel("Max Absolute Error")
f3.set_ylabel("Max Error")
f4.set_ylabel("Root Mean Square Error")

f1.yaxis.label.set_color(p1.get_color())
f2.yaxis.label.set_color(p2.get_color())
f3.yaxis.label.set_color(p3.get_color())
f4.yaxis.label.set_color(p4.get_color())

tkw = dict(size=4, width=1.5)
f1.tick_params(axis='y', colors=p1.get_color(), **tkw)
f2.tick_params(axis='y', colors=p2.get_color(), **tkw)
f3.tick_params(axis='y', colors=p3.get_color(), **tkw)
f4.tick_params(axis='y', colors=p4.get_color(), **tkw)
f1.tick_params(axis='x', **tkw)

lines = [p1, p2, p3, p4]

f1.legend(lines, [l.get_label() for l in lines])

plt.show()
我想实现与下图2类似的功能,但有多个Y轴,每个Y轴对应于各自的彩色条。非常感谢我能得到的任何帮助。谢谢

  • 该问题源于matplotlib api根据绘图类型返回不同对象的方式(例如,具有不同的返回(&H))
  • 线条数据可能是使用绘制的,但不适用于
    bar
  • 我给你两个选择:
  • 用一个y轴绘制条形图,然后将其设置为对数刻度,以补偿值范围内的变化
  • 使用绘制条形图,并将其设置为对数比例
  • 将数据保留在数据框中,以便于打印
设置数据帧
将熊猫作为pd导入
将matplotlib.pyplot作为plt导入
将numpy作为np导入
#测试数据
np.随机种子(10)
行数=5
feature1=np.random.randint(10,size=(行,)/10
feature2=np.random.randint(20,大小=(行,)/10
feature3=np.random.randint(8,25,size=(行,))
feature4=np.random.randint(1,3,size=(行,))
xaxis=范围(行)
#创建数据帧
df=pd.DataFrame({'Adj.R2':feature1,'Max Abs.Error':feature2,'Max Error':feature3,'RMS Error':feature4},
索引=['SVR','DTR','RFR','PR','MLR'])
#显示(df)
形容词。R2最大Abs。误差最大误差均方根误差
SVR 0.9 1.6 18 2
DTR 0.4 1.7 16 2
RFR 0.0.8 12 1
PR 0.1 0.9 24 1
MLR 0.9 0.0 12 2
策划
secondary\u y
ax=df.plot(次要_y=['Max Error','RMS Error'],kind='bar')
ax.right\u ax.set\u yscale('log'))
plt.show()

单y轴
df.plot(kind='bar',logy=True)
plt.show()

  • 该问题源于matplotlib api根据绘图类型返回不同对象的方式(例如,具有不同的返回(&H))
  • 线条数据可能是使用绘制的,但不适用于
    bar
  • 我给你两个选择:
  • 用一个y轴绘制条形图,然后将其设置为对数刻度,以补偿值范围内的变化
  • 使用绘制条形图,并将其设置为对数比例
  • 将数据保留在数据框中,以便于打印
设置数据帧
将熊猫作为pd导入
将matplotlib.pyplot作为plt导入
将numpy作为np导入
#测试数据
np.随机种子(10)
行数=5
feature1=np.random.randint(10,size=(行,)/10
feature2=np.random.randint(20,大小=(行,)/10
feature3=np.random.randint(8,25,size=(行,))
feature4=np.random.randint(1,3,size=(行,))
xaxis=范围(行)
#创建数据帧
df=pd.DataFrame({'Adj.R2':feature1,'Max Abs.Error':feature2,'Max Error':feature3,'RMS Error':feature4},
索引=['SVR','DTR','RFR','PR','MLR'])
#显示(df)
形容词。R2最大Abs。误差最大误差均方根误差
SVR 0.9 1.6 18 2
DTR 0.4 1.7 16 2
RFR 0.0.8 12 1
PR 0.1 0.9 24 1
MLR 0.9 0.0 12 2
策划
secondary\u y
ax=df.plot(次要_y=['Max Error','RMS Error'],kind='bar')
ax.right\u ax.set\u yscale('log'))
plt.show()

单y轴
df.plot(kind='bar',logy=True)
plt.show()

非常感谢您的详细解释。了解多Y轴线形图的工作方式与条形图不同。然而,有没有办法将所有4个Y轴分别叠加在绘图上?@Skyhehe没有这么多。我还没有找到一个可以单独添加条的示例。非常感谢您的详细解释。了解多Y轴线形图的工作方式与条形图不同。然而,有没有办法将所有4个Y轴分别叠加在绘图上?@Skyhehe没有这么多。我还没有找到一个可以单独添加条的示例。