Reputation: 1633
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()
Upvotes: 0
Views: 215
Reputation: 339170
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)
Upvotes: 1