简体   繁体   English

防止熊猫df.plot()对y轴排序

[英]Prevent pandas df.plot() from sorting the y-axis

Its very irritating that the pandas df.plot sorts the y-axis automatically in descending order. 大熊猫df.plot对y轴自动以降序排序非常令人讨厌。

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

I do not want to sort anything in my data frame and plot as it is. 我不想对数据框中的任何内容进行排序并按原样绘制。 Is there any way to prevent automatic sorting ? 有什么方法可以防止自动分类吗?

I tried to look at documentation and the plot() does not have any option to prevent sorting. 我试图查看文档,plot()没有任何防止排序的选项。 I tried in google but I could not find the solution. 我在Google中尝试过,但找不到解决方案。

Example file ( tab delimited ): 示例文件(以制表符分隔):

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

Code snippet: 程式码片段:

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()

在此处输入图片说明

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()

在此处输入图片说明

or manually reverting the 'Y-axis' data: 或手动还原“ Y轴”数据:

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

Result: 结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM