Python matplotlib/Pandas 中的水平箱线图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18500011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Horizontal box plots in matplotlib/Pandas
提问by Amelio Vazquez-Reina
Bar plots:
条形图:
matplotlib
offers the function bar
and barh
to do verticaland horizontalbar plots.
matplotlib
提供功能bar
和barh
做垂直和水平条形图。
Box plots:
箱线图:
matplotlib
also offers the function boxplot
to do verticalbox plots.
matplotlib
还提供了boxplot
做垂直箱线图的功能。
And Pandasoffers its own functionfor verticalbox plots.
But is there any way in matplotlib or Pandas to get a horizontalbox plot?
但是在 matplotlib 或 Pandas 中有什么方法可以获得水平箱线图吗?
采纳答案by unutbu
matplotlib's boxplot(..., vert=False)
makes horizontal box plots.
The keyword parameter vert=False
can also be passed to DataFrame.boxplot
:
matplotlibboxplot(..., vert=False)
制作水平箱线图。关键字参数vert=False
也可以传递给DataFrame.boxplot
:
import matplotlib.pyplot as plt
import pandas as pd
x = [[1.2, 2.3, 3.0, 4.5],
[1.1, 2.2, 2.9, 5.0]]
df = pd.DataFrame(x, index=['Age of pregnant women', 'Age of pregnant men'])
df.T.boxplot(vert=False)
plt.subplots_adjust(left=0.25)
plt.show()
I see from the comment (below) that the motivation for making a horizontal box plot is that the labels are rather long. Another option in that case might be to rotate the xticklabels:
我从评论(下面)中看到,制作水平箱线图的动机是标签相当长。在这种情况下,另一种选择可能是旋转 xticklabels:
import matplotlib.pyplot as plt
import pandas as pd
x = [[1.2, 2.3, 3.0, 4.5],
[1.1, 2.2, 2.9, 5.0]]
df = pd.DataFrame(x, index=['Age of pregnant women', 'Age of pregnant men'])
df.T.boxplot()
plt.subplots_adjust(bottom=0.25)
plt.xticks(rotation=25)
plt.show()
回答by MasterOne Piece
vert=False stands # for "no vertical"
Use by='categorical_feature name' to make box for every level plt.tight_layout() # kills any overlapping plots (not always) Matplotlib and Pandas are really easy when you master them and you can do powerful plots using them.
使用 by='categorical_feature name' 为每个级别制作框 plt.tight_layout() # 消除任何重叠的图(并非总是如此)当您掌握 Matplotlib 和 Pandas 时,它们真的很容易,并且您可以使用它们来制作强大的图。