0

I have a pandas dataframe A,B, and posand 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.

1 Answer 1

1

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')

Output Scatter by ginven data

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.

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.