首页 > 解决方案 > 使用 matlib.plot 绘制二维图形

问题描述

我需要在 python 中使用 matlib.plot 绘制图形,如下所示:

在此处输入图像描述

实际上我正在使用绘制所有条形图的代码

bars = ('Optimum CPLEX', 'eUCB', 'ONETS', 'e-greedy')
    y_pos = [1, 3, 5, 7]
    y_pos_2 = [2, 4, 6, 8]

    plt.ylabel('Average Reward \u03B7_i')
    plt.suptitle('Optimal Model versus Heuristics')
    name = 'grafics/11_reward_utilization.png'
         
    # Create bars
    plt.bar(y_pos, reward, color=['blue', 'blue', 'blue', 'blue'])
    plt.bar(y_pos_2, utilization, color=['yellow', 'yellow', 'yellow', 'yellow'])
         
    # Create names on the x-axis
    plt.xticks(y_pos, bars)
         
    # Show graphic
    plt.savefig(name, transparent = True)
    plt.show()

但结果我收到了一个包含所有条形但尺寸相同的图形,如下所示:

在此处输入图像描述

有人知道我如何在 y 轴上绘制这两个不同维度数据的图形时间?

谢谢你。

标签: python

解决方案


你定义了rewardutilization???

您使用这些定义的示例对我有用:

import matplotlib.pyplot as plt

bars = ('Optimum CPLEX', 'eUCB', 'ONETS', 'e-greedy')
y_pos = [1, 3, 5, 7]
reward = [1,2,3,4]

y_pos_2 = [2, 4, 6, 8]
utilization = [4,3,2,1]

plt.ylabel('Average Reward \u03B7_i')
plt.suptitle('Optimal Model versus Heuristics')
name = '11_reward_utilization.png'

# Create bars
plt.bar(y_pos, reward, color=['blue', 'blue', 'blue', 'blue'])
plt.bar(y_pos_2, utilization, color=['yellow', 'yellow', 'yellow', 'yellow'])

# Create names on the x-axis
plt.xticks(y_pos, bars)
# Show graphic
plt.savefig(name, transparent=True)
plt.show()

推荐阅读