python - matplotlib/Pandas 中的水平箱线图

标签 python matplotlib pandas

条形图:

matplotlib 提供函数barbarh垂直水平条形图。

箱线图:

matplotlib 还提供函数boxplot垂直箱线图。

还有 Pandas报价 its own function 垂直箱线图。

但是在 matplotlib 或 Pandas 中是否有任何方法可以获得 水平 箱线图?

最佳答案

matplotlib 的 boxplot(..., 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

我从评论(下面)中看到,制作水平箱形图的动机是标签相当长。在这种情况下,另一种选择可能是旋转 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

关于python - matplotlib/Pandas 中的水平箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18500011/

相关文章:

python - 如何更改数据框中保存的数据的格式?

python - 在 pandas/Python 中不间断地计算连续值

python - 如何使用 BeautifulSoup 查找指向特定域的页面中的所有链接?

python - 使用 opencv-python 在 Windows 中禁用网络摄像头的自动对焦

python - 使用Flask,尝试让AJAX在更新mongo记录后更新span,但它打开一个新页面

python - MatPlotLib:同一个散点图上的多个数据集

python - 将注释框添加到 matplotlib 等高线/热图图

python - Plotly:单击按钮后如何显示图形?

python - matplotlib 中的自动标记大小

python - 基于联合条件的 Pandas 切片行