首页 > 解决方案 > How can I decrease the width of a boxplot rectangle?

问题描述

I've made a boxplot:

Boxplot

I would like to decrease the width of the blue/orange rectangle to get a width like the blue/orange rectangles in this boxplot:

enter image description here

My goal is it to decrease the total width of the boxplot so that it takes less space in a document / I can have text flow beside the plot picture.

Code so far:

ax_v = sns.boxplot(x="hasTopic", y="sentiment_sum", data=videos, orient="v")
ax_v.set(xticklabels=["ohne", "mit"])
ax_v.set(ylim=(-4, 4))
plt.xlabel('Themenbezug Videos', fontsize=14)
plt.ylabel('Sentiment', fontsize=14)
plt.show()

How can I do that in Seaborn?

标签: seabornboxplotspacing

解决方案


plt.figure(figsize=(3.4, 4))  # <- this line
ax_v = sns.boxplot(x="hasTopic", y="sentiment_sum", data=videos, width=0.5, orient="v")
ax_v.set(xticklabels=["ohne", "mit"])
ax_v.set(ylim=(-4, 4))
plt.xlabel('Themenbezug Videos', fontsize=14)
plt.ylabel('Sentiment', fontsize=14)
plt.show()

解决了。

要使用自定义颜色为每个矩形着色,请使用:

my_pal = {0: "#3399ff", 1: "#f1c40f"}  # x="hasTopic" has value 0 or 1
plt.figure(figsize=(3.4, 4), dpi=800)
ax_c = sns.violinplot(x="hasTopic", y="sentiment_sum", palette=my_pal, width=0.7, orient="v", data=channels)
ax_c.yaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
#ax_v = sns.swarmplot(x="hasTopic", y="sentiment_sum", data=comments, color=".25")
ax_c.set(xticklabels=["ohne", "mit"])
ax_c.set(ylim=(-4, 4))
plt.xlabel('Themenbezug Channels', fontsize=14)
plt.ylabel('Sentiment', fontsize=14)
plt.tight_layout()
plt.show()

推荐阅读