将分组条形图与截断折线图组合

Combining a grouped bar plot with a truncated line chart

我知道如何在 matplotlib 中创建一个 grouped bar plot and I know how to create a line plot 但不幸的是,我不知道如何将两者结合起来,根据条形图对线图进行分组,同时保留两个轴.以下草图(希望如此)说明了我试图实现的目标:

这就是我到目前为止所做的:

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

labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]

YearGroup1 = ['G11','G12']
Unemployment_RateGroup1 = [31,32]

x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, men_means, width, label='Men')
rects2 = ax.bar(x + width/2, women_means, width, label='Women')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()

def autolabel(rects):
    """Attach a text label above each bar in *rects*, displaying its height."""
    for rect in rects:
        height = rect.get_height()
        ax.annotate('{}'.format(height),
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)

fig.tight_layout()

plt.plot(YearGroup1, Unemployment_RateGroup1)
plt.show()

但是,我不知道如何手动重新定位折线图的 x 位置。这些线应该在组内并且应该在第二个 y 轴上。

整个图旨在显示不同算法(X_1、X_2、X_3、...)在不同数据类型(红色条、绿色条、蓝色)上的结果酒吧)。第二个 y 轴上的分组折线图应该代表每个算法所需的时间。

非常感谢任何帮助。

假设线图应该显示 组内 数据,您可以这样绘制:

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

labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]

YearGroup1 = ['G11','G12']
Unemployment_RateGroup1 = [31,32]

x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
ax2 = ax.twinx()  # <--- make a second y-axis
rects1 = ax.bar(x - width/2, men_means, width, label='Men')
rects2 = ax.bar(x + width/2, women_means, width, label='Women')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()

def autolabel(rects):
    """Attach a text label above each bar in *rects*, displaying its height."""
    for rect in rects:
        height = rect.get_height()
        ax.annotate('{}'.format(height),
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)

# plot lines    
for i in range(len(labels)):
    ax2.plot(
        [x[i] - width / 2, x[i] + width / 2], 
        Unemployment_RateGroup1, 
        color='C3', marker='+', mec='k'
    )

fig.tight_layout()

请注意我掉了 plt.show()。如果您需要这一行,请不要忘记添加它。

如果每个组有单独的 Unemployment_Rate 数据,f.i。

Unemployment_Rate = [[31, 32], [7, 12], [23, 9], [0, 1], [32, 0]]

您可以将线图替换为:

ax2.plot(
    [x[i] - width / 2, x[i] + width / 2], 
    Unemployment_Rate[i], 
    color='C3', marker='+', mec='k'
)

这将产生: