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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:54:37  来源:igfitidea点击:

Horizontal box plots in matplotlib/Pandas

pythonmatplotlibpandas

提问by Amelio Vazquez-Reina

Bar plots:

条形图:

matplotliboffers the function barand barhto do verticaland horizontalbar plots.

matplotlib提供功能barbarh垂直水平条形图。

Box plots:

箱线图:

matplotlibalso offers the function boxplotto 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=Falsecan 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()

enter image description here

在此处输入图片说明

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

enter image description here

在此处输入图片说明

回答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 时,它们真的很容易,并且您可以使用它们来制作强大的图。