将多种情节整合为一个

时间:2019-03-18 20:29:34

标签: python matplotlib

我有以下数组。我想使用random_round_i数组创建3个四分位数图,upper_limits显示每个箱形图的上限,所有底线都作为底线,并再次作为单个图流。

我并没有将所有内容放入箱图中,因为流量,upper_limits和base来自不同的来源,实际上我需要将它们的值与随机值进行比较

random_round_1=[0.508477, 0.509855, 0.517986]
random_round_2=[0.506998, 0.523818, 0.503029]
random_round_3=[0.524584, 0.53033, 0.514867]
flow = [0.503688, 0.507809, 0.504012]
upper_limits = [0.544946, 0.568013, 0.616112]
base = [0.481581] 

我该如何创建一个图,在所有框图中,我将基线作为一条水平长线,然后将框图彼此相邻,最后将流和上限值作为法线图。

enter image description here

当我仅使用下面的代码时,箱形图和折线图在正确的位置不匹配。我希望这些行能通过所有三个箱形图。

    plt.boxplot([random_round_1, random_round_2, random_round_3])
    plt.plot(upper_limits)
    plt.plot(flow)
    plt.hlines(y = base, xmin = 0, xmax = 4)

1 个答案:

答案 0 :(得分:0)

问题在于您的箱形图以x = 1、2和3为中心。但是,当您使用plt.plot(upper_limits)plt.plot(flow)时,您只是在传递y值。在这种情况下,默认的x值从0开始。因此,您的曲线不会落在箱形图顶部的相同x值处。

您需要从1开始的正确x网格,该网格可以使用rangenp.arange生成。

plt.boxplot([random_round_1, random_round_2, random_round_3])
plt.plot(range(1, len(upper_limits)+1), upper_limits)
plt.plot(range(1, len(flow)+1), flow)
plt.hlines(y = base, xmin = 0, xmax = 4)

enter image description here