条形图:
matplotlib
提供函数bar
和 barh
做垂直和水平条形图。
箱线图:
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()
我从评论(下面)中看到,制作水平箱形图的动机是标签相当长。在这种情况下,另一种选择可能是旋转 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()
关于python - matplotlib/Pandas 中的水平箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18500011/