2017-02-20 156 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這是我得到的圖表。我所需要的是讓垂直線水平。這些線條由兩個具有我應該在上面的形狀的數據框生成。 –