Python 如何为matplotlib添加图例,该图例由数组索引名而不是索引中的值组成?
这里是编程新手 我试图基于列表绘制一个图,但列表数组中包含一系列值。 我试图使用for循环将所有类别绘制成一个图。当我试图绘制图表时,除了图例外,一切都很顺利。我需要做的是创建一个图例,说明哪些线是“集装箱”、“货船”、“散货船”。。。。但是当我尝试使用plt.legend()时,它给了我“containers”、“freighters”、“bulkcarrier”的值,而不是标签本身。关于如何仅绘制列表数组而不是图例的值,我是否可以获得帮助 为了进一步澄清,这里是一个列表值,仅适用于容器Python 如何为matplotlib添加图例,该图例由数组索引名而不是索引中的值组成?,python,excel,pandas,matplotlib,Python,Excel,Pandas,Matplotlib,这里是编程新手 我试图基于列表绘制一个图,但列表数组中包含一系列值。 我试图使用for循环将所有类别绘制成一个图。当我试图绘制图表时,除了图例外,一切都很顺利。我需要做的是创建一个图例,说明哪些线是“集装箱”、“货船”、“散货船”。。。。但是当我尝试使用plt.legend()时,它给了我“containers”、“freighters”、“bulkcarrier”的值,而不是标签本身。关于如何仅绘制列表数组而不是图例的值,我是否可以获得帮助 为了进一步澄清,这里是一个列表值,仅适用于容器 In
Input: print(containers)
Output:
month vessel_type number_of_vessels gross_tonnage
0 2019-12 Container 1442 74170.533
8 2020-01 Container 1418 74664.129
16 2020-02 Container 1287 65183.106
24 2020-03 Container 1399 72789.161
32 2020-04 Container 1329 71068.988
40 2020-05 Container 1262 68167.275
48 2020-06 Container 1321 71659.294
56 2020-07 Container 1340 71578.085
这里什么是货船和散货船?这里只显示了容器,类别中的内容是什么?它与容器值相似,只是不同的数字。我用字典代替列表解决了这个问题
categories=[containers,freighters,bulk_carriers,tankers,passengers,barges,tugs,miscellaneous]
for i in categories:
plt.plot(i['month'],i['gross_tonnage'],label=i)
plt.xlabel('months')
plt.ylabel('gross_tonnage')
plt.title('Changes in gross tonnage over time')
plt.legend()```
[![Here is the screenshot of what im trying to achieve, pardon the poor drawing][1]][1]
[![this is the screenshot of the disaster][2]][2]
[1]: https://i.stack.imgur.com/ai30L.png
[2]: https://i.stack.imgur.com/7knj8.png