2017-02-20 72 views
0

(使用Python 3.5) 我有熊猫据帧表与colums:大熊猫与Matplotlib开关轴

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

不是我画出这个

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

它把价格X和数量Y上-轴。 我该如何切换,以便价格在Y轴上?

回答

1

您可以使用lreshapeSeries.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

+0

之前,我有overlaping线 - 我想要的东西 - 与此解决方案,它吸引他们依次 –

+0

你能解释一下吗?因为看来我不明白你需要什么。 – jezrael

+0

https://www.dropbox.com/s/c36una20ex0eh5l/Screenshot%202017-02-20%2012.33.11.png?dl=0这是我得到的图表。我所需要的是让垂直线水平。这些线条由两个具有我应该在上面的形状的数据框生成。 –