Matplotlib,水平条形图(barh)上下颠倒 [英] Matplotlib, horizontal bar chart (barh) is upside-down
问题描述
TL'DR ,垂直条形图以常规方式显示-东西从左到右排列.但是,当将其转换为水平条形图(从 bar
到 barh
)时,一切都是颠倒的.即,对于分组的条形图,不仅分组的条形图的顺序是错误的,而且每个组的顺序也是错误的.
例如,来自
如果仔细观察,会发现条和图例的顺序相反-牛肉在图例的顶部显示,但在图形的底部显示.
作为最简单的演示,我将 kind ='bar',
更改为 kind ='barh',
从这张图
我们可以看到该顺序也是非常规的,因为人们希望该图是自上而下的,"AAA"在顶部,而不是底部.
如果您搜索"Excel上下颠倒",您会发现人们在各地都在用Excel抱怨.Microsoft Excel有一个修复程序,Matplotlib/Panda/Searborn/Ploty/etc是否有修复程序?
我认为组和子组的联合错误顺序归结为一个特征: y
轴向上增加,例如通常的情节.尝试反转此轴的 y
轴,如以下无熊猫示例:
将numpy导入为np导入matplotlib.pyplot作为pltx =范围(5)y = np.random.randn(5)#plot1:栏plt.figure()plt.bar(x,y)#plot2:barh,顺序错误plt.figure()plt.barh(x,y)#plot3:barh,顺序正确:y轴自上而下plt.figure()plt.barh(x,y)plt.gca().invert_yaxis()
专门针对大熊猫, pandas.DataFrame.plot
及其各种绘制子方法返回一个matplotlib轴对象,因此您可以直接反转其y轴:
ax = df.plot.barh()#或df.plot(),或类似的ax.invert_yaxis()
TL'DR, the vertical bar charts are shown in a conventional way -- things line up from left to right. However, when it is converted to horizontal bar chart (from bar
to barh
), everything is upside-down. I.e., for a grouped bar chart, not only the order of the grouped bar is wrong, the order of the each group is wrong as well.
For e.g., the graph from http://dwheelerau.com/2014/05/28/pandas-data-analysis-new-zealanders-and-their-sheep/
If you look closely, you will find that the the bar and legend are in reverse order -- Beef shows on top in legend but on bottom in the graph.
As the simplest demo, I changed kind='bar',
to kind='barh',
from this graph
https://plot.ly/pandas/bar-charts/#pandas-grouped-bar-chart
and the result looks like this:
https://plot.ly/7/~xpt/
I.e., the bars in the horizontal grouped bar chart is ordered upside-down.
How to fix it?
EDIT: @Ajean, it is actually not only the order of the grouped bar is wrong, the order of the each group is wrong as well. The graph from Simple customization of matplotlib/pandas bar chart (labels, ticks, etc.) shows it clearly:
We can see that the order is unconventional too, because people would expect the graph to be top-down, with "AAA" at the top, not the bottom.
If you search for "Excel upside-down", you will find people are complaining about this in Excel all over the places. The Microsoft Excel has a fix for it, do Matplotlib/Panda/Searborn/Ploty/etc has a fix for it?
I believe the joint wrong order of groups and subgroups boils down to a single feature: that the y
axis increases upwards, as in a usual plot. Try reversing the y
axis of your axes as in this pandas-less example:
import numpy as np
import matplotlib.pyplot as plt
x=range(5)
y=np.random.randn(5)
#plot1: bar
plt.figure()
plt.bar(x,y)
#plot2: barh, wrong order
plt.figure()
plt.barh(x,y)
#plot3: barh with correct order: top-down y axis
plt.figure()
plt.barh(x,y)
plt.gca().invert_yaxis()
Specifically for pandas, pandas.DataFrame.plot
and its various plotting submethods return a matplotlib axes object, so you can invert its y axis directly:
ax = df.plot.barh() # or df.plot(), or similar
ax.invert_yaxis()
这篇关于Matplotlib,水平条形图(barh)上下颠倒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!