Reputation: 51
Suppose I have a list of lists, such as:
['010', '01', '7']
['010', '02', '6']
['010', '03', '24']
['010', '04', '6']
['015', '02', '8']
['015', '03', '27']
['015', '04', '27']
['020', '01', '130']
['020', '02', '140']
['020', '03', '202']
['020', '04', '3']
['021', '01', '3']
['021', '02', '4']
['021', '03', '22']
['021', '04', '1']
['025', '01', '13']
['025', '02', '52']
['025', '03', '82']
The first element represents an ID, second element the month and third element the total amount. How can I plot the total amounts by month? I would like to have an individual line for each ID.
I tried something similar to the below:
for i in output:
plt.plot(int(i[1]). int(i[2]))
plt.show()
Is returning a blank line. Additionally, I am not sure how to handle legends as well.
Upvotes: 1
Views: 4095
Reputation: 39042
First you need to group the values based on the first column. This can be done using this suggestion for example. Then you need to convert your string values to floats. This can be done using map()
among other possible ways. [:, 1:]
specifies indexing which means all the rows :
and then second and third columns 1:
Below is a working answer assuming that your input is stored in a variable called data
.
EDIT: I took the suggestion of @RafaelC to convert the data type to float using astype(float)
import matplotlib.pyplot as plt
import numpy as np
import itertools, operator
# data = [[...], [...], [...]]
for k,g in itertools.groupby(data, operator.itemgetter(0)):
arr = np.array(list(g))[:, 1:].astype(float)
x = arr[:, 0]
y = arr[:, 1]
plt.plot(x, y, label=k)
plt.legend()
plt.show()
Upvotes: 1
Reputation: 149
this pic for plot try this code
@Output
import matplotlib.pyplot as plt
output = [['010', '01', '7'],
['010', '02', '6'],
['010', '03', '24'],
['010', '04', '6'],
['015', '02', '8'],
['015', '03', '27'],
['015', '04', '27'],
['020', '01', '130'],
['020', '02', '140'],
['020', '03', '202'],
['020', '04', '3'],
['021', '01', '3'],
['021', '02', '4'],
['021', '03', '22'],
['021', '04', '1'],
['025', '01', '13'],
['025', '02', '52'],
['025', '03', '82']]
listx = []
listy = []
for i in output:
listx.append((i[1]))
listy.append((i[2]))
plt.plot(listx,listy,'ro')
@Output(plt.show())
Upvotes: 2
Reputation: 153460
You can use pandas dataframes to label and plot your data:
import pandas as pd
l = [['010', '01', '7'],
['010', '02', '6'],
['010', '03', '24'],
['010', '04', '6'],
['015', '02', '8'],
['015', '03', '27'],
['015', '04', '27'],
['020', '01', '130'],
['020', '02', '140'],
['020', '03', '202'],
['020', '04', '3'],
['021', '01', '3'],
['021', '02', '4'],
['021', '03', '22'],
['021', '04', '1'],
['025', '01', '13'],
['025', '02', '52'],
['025', '03', '82']]
df = pd.DataFrame(l, columns=['ID','Month','Amount'])
df['Amount'] = df['Amount'].astype(int)
df.groupby(['Month','ID'])['Amount'].sum().unstack().plot()
Output:
Upvotes: 2
Reputation: 59274
You may use pandas
. I assumed you list of lists is named l
import pandas as pd
fig, ax = plt.subplots()
arr = np.array(l).astype(np.int)
for id_, g in pd.DataFrame(arr).groupby(0):
ax.plot(g[1], g[2], label=id_)
ax.legend()
Upvotes: 2