本文介绍了箱形图平均分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将箱形图均匀地分布在x轴上。以下代码生成了一个图,其中两个箱形图紧密并在两侧都有足够的空间。在生成中,我希望代码不考虑盒图的数量而均匀地分布盒图(在此示例中,只有两个,但通常会有很多)。
I would like to spread my boxplots equally over the x-axis. The following code generates a figure where the two boxplots are close with plenty of space on either side. In generate I want the code to spread the boxplots out evenly regardless of the number of boxplots (in this example there are only two, but in general there will be many).
import matplotlib.pyplot as plt
statistic_dict = {0.40000000000000002: [0.36003616645322273, 0.40526649416305677, 0.46522159350924536], 0.20000000000000001: [0.11932912803730165, 0.23235825966896217, 0.12380728472472625]}
def draw_boxplot(y_values, x_values, edge_color, fill_color):
bp = plt.boxplot(y_values, patch_artist=True, positions=x_values)
for element in ['boxes', 'whiskers', 'fliers', 'medians', 'caps']:
plt.setp(bp[element], color=edge_color)
plt.xlabel("x label ")
plt.ylabel("y label ")
plt.title("Title")
for patch in bp['boxes']:
patch.set(facecolor=fill_color)
y_values = statistic_dict.values()
x_values = statistic_dict.keys()
draw_boxplot(y_values, x_values, "skyblue", "white")
plt.savefig('fileName.png', bbox_inches='tight')
plt.close()
推荐答案
箱图在创建后不会自动缩放。您可以手动执行。
The boxplot is not autoscaled after its creation. You may do that manually. Also adding some custom margin afterwards is possible.
plt.gca().autoscale()
plt.gca().margins(x=0.2)
这篇关于箱形图平均分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!