3

I have a pandas DataFrame that looks something like this:

n=pd.DataFrame({1:[1,2,np.NAN,4],2:[5,6,5,8],'info':[5,6,8,7],'moreinfo':[1,2,5,8]}).set_index(['info','moreinfo'])

Which looks like this:

                       1    2
info    moreinfo        
5       1            1.0    5
6       2            2.0    6
8       5            NaN    5
7       8            4.0    8

How can I line plot this such that:

The column labels are used as the xlabels
The data is grouped and colored by index
The y-axis is the numerical data found within each column.

Below is an example of what I'm thinking the plot should look like:

enter image description here

2
  • 1
    Can you show me what your graph should look like. What does "The data is grouped and colored by index" two indexes, info and more info. Commented Jul 16, 2018 at 20:02
  • What did you try so far? Could you draw a small sketch of the desired output? How should NaN be treated? Commented Jul 16, 2018 at 20:02

1 Answer 1

1

Let's try:

ax = n.T.plot(marker='o')
ax.set_xticks(n.T.index.astype(int))

Output:

enter image description here

You might want to fill that NaN with zero.

ax = n.fillna(0).T.plot(marker='o')
ax.set_xticks(n.T.index.astype(int))

Output: enter image description here

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

1 Comment

I just thought to try this, and it worked nicely. I assume there must be some way to specify the x labels versus the data labels other than transforming the DataFrame. But in any case, this does work nicely. Thanks.

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.