简体   繁体   English

Python3 MatPlotLib:如何获取多个线图

[英]Python3 MatPlotLib : How can I get Multiple Line Plots

I have a list of Tuples like this : 我有一个这样的元组列表:

list_months = [ ('A', 'January'),
                ('A', 'January'),
                ('A', 'January'),  # Total 10 instances of ('A', 'January')
                ('A', 'March'),
                ('A', 'March'),
                ('A', 'March'),
                ('A', 'February'),
                ('A', 'February'),
                .............
                ('B', 'January'),
                ('B', 'January'),  # Total 15 instances of ('B', 'January')
                ('B', 'July'),
                ('B', 'July'),
                ............. ]

This was obtained via a Pandas dataframe using : 这是通过使用以下命令通过Pandas数据框获得的:

for index, val in b['Incident Reported Date Time'].iteritems():
    list_months.append((index, str(val)))

I want to be able to generate a MatPlotLib graph such that : 我希望能够生成MatPlotLib图,例如:
X-Axis -> Months X轴->月
Y-Axis -> Volume Y轴->体积
Multiple Colored Lines -> Each representing 'A', 'B', 'C', etc. 多色线->每条代表“ A”,“ B”,“ C”等


在此处输入图片说明

For example in this picture the Red line would represent 'A' and the blue line 'B'. 例如,在此图片中,红线代表“ A”,蓝线代表“ B”。
Since there are 10 of ('A', 'January') it shows the red line on 10 at the Y-Axis for the month of January and there are 15 of ('B', January') so it shows the blue line at 15 at the Y-Axis for the month of January. 由于('A','January')有10个,因此在一月的Y轴上的10上显示红线,而('B',January')则有15个,因此显示蓝线一月份Y轴的15点。

How can I generate this and the legend dynamically in matplotlib for Python3 ? 如何在matplotlib for Python3中动态生成此图例和图例?
The graph should be line graphs like in the example image. 该图应为线形图,如示例图像中所示。

It will be easier to plot if you leave the data in a DataFrame. 如果将数据保留在DataFrame中,则绘制起来会更容易。 I made a sample DataFrame using the example data from your post: 我使用您帖子中的示例数据制作了一个示例DataFrame:

list_months = [ ('A', 'January'),
                ('A', 'January'),
                ('A', 'January'),  # Total 10 instances of ('A', 'January')
                ('A', 'March'),
                ('A', 'March'),
                ('A', 'March'),
                ('A', 'February'),
                ('A', 'February'),
                ('B', 'January'),
                ('B', 'January'),  # Total 15 instances of ('B', 'January')
                ('B', 'July'),
                ('B', 'July')
            ]
d = pandas.DataFrame(list_months, columns=["Item", "Month"]).fillna(0)

>>> d
   Item     Month
0     A   January
1     A   January
2     A   January
3     A     March
4     A     March
5     A     March
6     A  February
7     A  February
8     B   January
9     B   January
10    B      July
11    B      July

Then you can get your plot easily: 然后,您可以轻松获得情节:

>>> d.groupby(['Month', 'Item']).size().unstack().loc[['January', 'February', 'March', 'July']].fillna(0).plot(kind='line')

在此处输入图片说明

(The .loc['January', 'February', ...] bit is only necessarily to get the months in temporal order rather than alphabetical order. In the long run, you're probably better off storing this information via a datetime object or a number representing the month.) .loc['January', 'February', ...]位只需.loc['January', 'February', ...]时间顺序而不是字母顺序获得月份。从长远来看,最好通过日期时间存储此信息对象或代表月份的数字。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM