防止pandas df.plot()对y轴进行排序

时间:2016-12-24 12:12:58

标签: python python-2.7 pandas matplotlib

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

1 个答案:

答案 0 :(得分: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轴”&#39;数据:

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

结果:

enter image description here