Reputation: 15
I have a pandas dataframe A
,B
, and pos
and want to plot A
versus B
but in such a way that a distinct graph is generated for each value of pos
.
Meaning a single figure but a graph for all pos = 1
, one for pos = 2
and so on
The dataframe is structured like this:
A B pos
1 6 2 1
2 2 10 2
3 1 3 4
4 8 1 1
5 6 1 1
How do I do that? Any help is appreciated.
Upvotes: 0
Views: 609
Reputation: 6337
I assume you are looking for a scatter plot. Other types can be defiened using the kind
keyword.
To genearate 3 figures based on the number in pos
, use
s.groupby('pos').plot(x='A', y='B', kind='scatter')
.
If you want to collect all three plots in one figure, then I think the best solution is to create a 'color'-column in the source to define the colors by group.
Data the looks like this:
1 6 2 1 red
2 2 10 2 blue
3 1 3 4 green
4 8 1 1 red
5 6 1 1 red
This is working:
s.plot(x='A', y='B', kind='scatter', color=s['color'].values)
And this is also working documented here:
s.plot(x='A', y='B', kind='scatter', c='color')
Comment Unfortunately this is not supported and raises an error.
s.plot(x='A', y='B', kind='scatter', color='color')
But in my option ths would be most straight forward.
Upvotes: 1