0

I have a data frame with three columns. I need to draw a graph with multiple lines. In that graphic, each line represents one id and the x-axis represents the month column and the y-axis represent the qtd column.

+-----+-----+----+
|month| id  |qtd |
+-----+-----+----+
| 1   |  1  | 1  |
| 1   |  2  | 3  |
| 1   |  3  | 6  |
| 2   |  4  | 2  |
| 2   |  5  | 3  |
| 3   |  6  | 4  |
| 3   |  7  | 5  |
| 3   |  8  | 4  |
+-----+-----+----+

DataFrame code:

l=[(1,1,1),(1,2,3),(1,3,6),(2,4,2),(2,5,3),(3,6,4),(3,7,5),(3,8,4)]
names=["month","id","qtd"]
db=sqlContext.createDataFrame(l,names)
db.show()

Desire result:

plot with x=qtd, y=month and one line for each id line

In that example I have 3 different id lines, so the graphic will be plotted with three lines, but in the real data frame, the number of ids would be unknown.

1 Answer 1

1

If you have a Pandas dataframe you can just group by id and plot each line like so:

fig, ax = plt.subplots()

for key, grp in df.groupby('id'):
    ax = grp.plot(ax=ax, x='qtd', y='month', label=key)

fig.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.