2016-12-24 123 views
0

令人非常烦恼的是,熊猫df.plot以降序自动排序y轴。防止pandas df.plot()对y轴进行排序

df1[:10].plot(x="Term",y="P-value",kind='barh') 

我不想排序我的数据框中的任何东西,并且原样绘图。有什么办法可以防止自动排序吗?

我试着看看文档和plot()没有任何选项来防止排序。我在谷歌尝试,但我找不到解决方案。

实施例文件(制表符分隔):

Term p-value 
De novo pyrimidine deoxyribonu 0.043726 
Interleukin signaling pathway_ 0.075241 
Plasminogen activating cascade 0.099365 
De novo purine biosynthesis_Ho 0.115435 
Alzheimer disease-presenilin p 0.142836 
Salvage pyrimidine ribonucleot 0.171718 
Pyrimidine Metabolism_Homo sap 0.171718 
Heterotrimeric G-protein signa 0.189208 
p53 pathway_Homo sapiens_P0005 0.215917 
Heterotrimeric G-protein signa 0.246512 

代码段:

import matplotlib.pyplot as plt 
import pandas as pd 
df = pd.read_csv("test.txt",sep='\t') 
df.plot(x="Term",y="p-value",kind='barh') 
plt.show() 

enter image description here

回答

0
In [33]: df.plot.barh(x='Term',y="p-value") 
Out[33]: <matplotlib.axes._subplots.AxesSubplot at 0xba4fb00> 

In [34]: plt.gca().invert_yaxis() # <--- this is the trick 

In [35]: plt.tight_layout() 

enter image description here

或手动如果还原 'Y轴' 数据:

df.plot.barh(x=df.Term[::-1],y="p-value") 
plt.tight_layout() 

结果:

enter image description here

+0

感谢您的特技 – gthm