1

(Using python 3.5) I have pandas dataframe tables with colums:

Price| Quantity|Price| Quantity|Price|...
2    | 1       |4    | 5       | 13  |...

Not when I Plot this

import matplotlib.pyplot as plt
plt.plot(my_table, their_table)
plt.show()

It puts price on X and Quantity on Y-axis. How can I switch this around, so that price is on Y axis?

1 Answer 1

1

You can use lreshape with Series.plot:

print (df)
   Price  Quantity  Price  Quantity  Price  Quantity
0      2         1      4         5     13        10
1      8         7      2         3      6         8

#remove duplicates in columns adding numbers
L = ['Price', 'Quantity']
k = int(len(df.columns) / 2)
df.columns = ['{}{}'.format(x, y) for y in range(1, k+1) for x in L]
print (df)
   Price1  Quantity1  Price2  Quantity2  Price3  Quantity3
0       2          1       4          5      13         10
1       8          7       2          3       6          8

#filter columns
prices = [col for col in df.columns if col.startswith('Price')]
quantities = [col for col in df.columns if col.startswith('Quantity')]
print (prices)
['Price1', 'Price2', 'Price3']

print (quantities)
['Quantity1', 'Quantity2', 'Quantity3']

#reshape all values to 2 columns
df = pd.lreshape(df, {'Price':prices, 'Quantity':quantities})
print (df)
   Price  Quantity
0      2         1
1      8         7
2      4         5
3      2         3
4     13        10
5      6         8

df.set_index('Price')['Quantity'].plot()

graph

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

6 Comments

Before I had overlaping lines - what I wanted - with this solutions it draws them sequentially
Can you explain more? Because it seems I dont understand what do you need.
dropbox.com/s/c36una20ex0eh5l/… This is the graph I'm getting. All I need is to get the vertical lines to be horizontal. Thous lines are generated by the two dataframes that have the shape I should above.
plt.plot( my_table, my_table[PRICE, 'g--', their_table, their_table[PRICE], 'r--' ) Did it!
what is ` my_table` and what their_table > I cannot reproduced your code.
|

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.