matplotlib 如何用python把一个元组列表字典转换成条形图

plicqrtu  于 2023-02-19  发布在  Python
关注(0)|答案(1)|浏览(149)

这是我的数据:

{
  '1': [(10, 0), (100, 0), (1000, 0), (10000, 1370)], 
  '2': [(20, 0), (200, 0), (2000, 1040), (20000, 11627)], 
  '4': [(40, 0), (400, 50), (4000, 3102), (40000, 31618)], 
  '8': [(80, 0), (800, 406), (8000, 7032), (80000, 67439)],
  '16': [(160, 0), (1600, 895), (16000, 14516), (160000, 149026)],
  '32': [(320, 0), (3200, 1979), (32000, 29760), (320000, 309373)],
  '64': [(640, 0), (6400, 3842), (64000, 60963), (640000, 624006)], 
  '128': [(1280, 0), (12800, 7647), (128000, 100533), (1280000, 1188787)], 
  '256': [(2560, 0), (25600, 16246), (256000, 144885), (2560000, 1360808)]
}

我想做的是一个条形图,其中字典的键代表一组由元组代表的条形,每个元组有2个条形,所以“1”键代表一组2乘2的8个条形。
元组中的每个数字都代表条的高度,它们几乎是一对数字,意思是:发送的消息/丢失的消息,而字典的关键字是向服务器发送消息的客户端的数量。
条本身在条对之间可以具有不同的颜色,以表示发送的消息/丢失的消息
这个想法是让所有的东西都在同一张图上!
参考图像:

尝试使用matplotlib,但无法找到解决方案。
还可以在处理之前访问数据,如下所示:

totalClients, totalMessages, totalMessagesLost
1, 10, 0
1, 100, 0
1, 1000, 0
1, 10000, 1370
2, 20, 0
2, 200, 0
2, 2000, 1040
2, 20000, 11627
4, 40, 0
4, 400, 50
4, 4000, 3102
4, 40000, 31618
8, 80, 0
8, 800, 406
8, 8000, 7032
8, 80000, 67439
16, 160, 0
16, 1600, 895
16, 16000, 14516
16, 160000, 149026
32, 320, 0
32, 3200, 1979
32, 32000, 29760
32, 320000, 309373
64, 640, 0
64, 6400, 3842
64, 64000, 60963
64, 640000, 624006
128, 1280, 0
128, 12800, 7647
128, 128000, 100533
128, 1280000, 1188787
256, 2560, 0
256, 25600, 16246
256, 256000, 144885
256, 2560000, 1360808
nxowjjhe

nxowjjhe1#

To plot all the data on the same graph, you can follow these steps:
import matplotlib.pyplot as plt
labels = [str(item[0]) for item in data['1']]
colors = plt.cm.jet([i/float(len(data)) for i in range(len(data))])
fig, ax = plt.subplots()
for key in data.keys():
    data_line = [item[1] for item in data[key]]
    ax.bar(labels, data_line, color=colors[int(key)-1], label=key)
ax.legend()
ax.set_title('Bar Graph')
plt.show()
import matplotlib.pyplot as plt

data = {'1': [(10, 0), (100, 0), (1000, 0), (10000, 1370)], 
        '2': [(20, 0), (200, 0), (2000, 1040), (20000, 11627)], 
        '4': [(40, 0), (400, 50), (4000, 3102), (40000, 31618)], 
        '8': [(80, 0), (800, 406), (8000, 7032), (80000, 67439)],
        '16': [(160, 0), (1600, 895), (16000, 14516), (160000, 149026)],
        '32': [(320, 0), (3200, 1979), (32000, 29760), (320000, 309373)],
        '64': [(640, 0), (6400, 3842), (64000, 60963), (640000, 624006)], 
        '128': [(1280, 0), (12800, 7647), (128000, 100533), (1280000, 1188787)], 
        '256': [(2560, 0), (25600, 16246), (256000, 144885), (2560000, 1360808)]}

labels = [str(item[0]) for item in data['1']]

colors = plt.cm.jet([i/float(len(data)) for i in range(len(data))])

fig, ax = plt.subplots()
for key in data.keys():
    data_line = [item[1] for item in data[key]]
    ax.bar(labels, data_line, color=colors[int(key)-1], label=key)

ax.legend()
ax.set_title('Bar Graph')
plt.show()
This will produce a single bar graph with all the data from the dictionary plotted on the same graph. Each key in the dictionary is plotted with a different color and is labeled in the legend. The x-axis labels are taken from the first item in each tuple of the first list in the dictionary.

相关问题