matplotlib/Pandas中的水平框图 [英] Horizontal box plots in matplotlib/Pandas
问题描述
matplotlib
提供功能 bar
和 barh
做垂直和水平条形图.
matplotlib
offers the function bar
and barh
to do vertical and horizontal bar plots.
matplotlib
还提供了功能 boxplot
进行垂直箱形图绘制.
matplotlib
also offers the function boxplot
to do vertical box plots.
熊猫提供但是matplotlib或Pandas中有什么方法可以得到水平箱形图?
But is there any way in matplotlib or Pandas to get a horizontal box plot?
推荐答案
matplotlib的boxplot(..., vert=False)
绘制水平箱形图.
关键字参数vert=False
也可以传递给DataFrame.boxplot
:
matplotlib's boxplot(..., vert=False)
makes horizontal box plots.
The keyword parameter vert=False
can also be passed to 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:
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:
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()
这篇关于matplotlib/Pandas中的水平框图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!