防止熊猫d .plot()对y轴进行排序

时间:2021-08-14 12:47:38

Its very irritating that the pandas df.plot sorts the y-axis automatically in descending order.

这只熊猫真烦人。绘制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. I tried in google but I could not find the solution.

我尝试查看文档,而plot()没有阻止排序的选项。我在谷歌上试过了,但没有找到解决办法。

Example file ( tab delimited ):

示例文件(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()

防止熊猫d .plot()对y轴进行排序

1 个解决方案

#1


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

防止熊猫d .plot()对y轴进行排序

or manually reverting the 'Y-axis' data:

或手动还原“y轴”数据:

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

Result:

结果:

防止熊猫d .plot()对y轴进行排序

#1


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

防止熊猫d .plot()对y轴进行排序

or manually reverting the 'Y-axis' data:

或手动还原“y轴”数据:

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

Result:

结果:

防止熊猫d .plot()对y轴进行排序