如何在我的 seaborn barplot 中的 first/last 条周围添加 space?
How can I add space around the first/last bars in my seaborn barplot?
我正在使用 seaborn 构建一个条形图,并且已经解决了大部分问题。但是我 运行 对情节中的第一项和最后一项存在疑问。在随附的屏幕截图中,当我希望边缘和条之间有 space 时,您可以看到第一个和最后一个条位于图形的边缘。
这是创建图表的代码
fig, ax = plt.subplots(figsize=(17,10))
# variables for table
examiner_figure_title = (f'Examiner Production - {yesterday}')
# hide axes
fig.patch.set_visible(False)
fig.suptitle(examiner_figure_title, fontsize=24)
# variables for bar graph
x = yesterday_df['Production']
y = yesterday_df['Examiner']
ax = sns.barplot(x=x, y=y, hue=y, orient='h', ax=ax, palette=sns.color_palette())
ax.set(yticklabels=[])
ax.tick_params(left=False)
ax.bar_label
# change_width(ax, .75)
change_height(ax, 1)
ax.legend(bbox_to_anchor=(1, .5))
plt.show()
我试过改变我的身材尺寸,认为这可能是原因。这并没有影响酒吧的位置。
我正在使用自定义高度函数来创建条形图而不是细线。如果我不应用条形的自定义大小,线条不会与图形边缘相对,但您无法真正看到线条,这就是我使用自定义设置的原因。也许我需要在函数中添加一些东西? (附加视图的自定义高度和宽度)
def change_width(ax, new_value):
for patch in ax.patches:
current_width = patch.get_width()
diff = current_width - new_value
patch.set_width(new_value)
patch.set_x(patch.get_x() + diff * .5)
def change_height(ax, new_value):
for patch in ax.patches:
current_height = patch.get_height()
diff = current_height - new_value
patch.set_height(new_value)
patch.set_y(patch.get_y() + diff * .5)
任何人都可以对此提供一些见解吗?
要在更改矩形后重新计算限制,您可以使用:
ax.relim()
ax.autoscale_view()
ax.margins(y=0.01) # for a smaller padding, as the default is rather wide
但您可能只想设置 dodge=False
。否则,每个 hue-values 为每个 y-positions.
保留一个位置
一些其他备注:
- 默认的
sns.color_palette()
只有10种不同的颜色,导致图例混乱;你可以使用例如turbo
或 tab20
更多颜色
- 要添加条形标签,您可以遍历
ax.containers
,例如: for bars_group in ax.containers: ax.bar_label(bars_group)
(每个色调值有一个“组”)
- 如果为图例设置
bbox_to_anchor=
,则还需要设置loc=
;默认使用loc='best',可以根据plot内容改变
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
fig, ax = plt.subplots(figsize=(17, 10))
fig.patch.set_visible(False)
x = [1, 4, 7, 9, 11, 12, 14, 16, 19, 20, 23, 24, 27, 28, 31, 32, 35.5, 40, 39, 40]
y = ['Hydrogen', 'Helium', 'Lithium', 'Berylium', 'Boron', 'Carbon', 'Nitrogen', 'Oxygen', 'Fluorine', 'Neon', 'Sodium', 'Magnesium', 'Aluminium', 'Silicon', 'Phosphorus', 'Sulphur', 'Chlorine', 'Argon', 'Potassium', 'Calcium']
ax = sns.barplot(x=x, y=y, hue=y, orient='h', dodge=False, ax=ax, palette='turbo')
ax.tick_params(left=False, labelleft=False)
for bars_group in ax.containers:
ax.bar_label(bars_group, padding=3, fontsize=15)
ax.legend(bbox_to_anchor=(1, .5), loc='center left')
# ax.margins(x=0.15) # optionally more space for the text
sns.despine()
plt.tight_layout()
plt.show()
我正在使用 seaborn 构建一个条形图,并且已经解决了大部分问题。但是我 运行 对情节中的第一项和最后一项存在疑问。在随附的屏幕截图中,当我希望边缘和条之间有 space 时,您可以看到第一个和最后一个条位于图形的边缘。
这是创建图表的代码
fig, ax = plt.subplots(figsize=(17,10))
# variables for table
examiner_figure_title = (f'Examiner Production - {yesterday}')
# hide axes
fig.patch.set_visible(False)
fig.suptitle(examiner_figure_title, fontsize=24)
# variables for bar graph
x = yesterday_df['Production']
y = yesterday_df['Examiner']
ax = sns.barplot(x=x, y=y, hue=y, orient='h', ax=ax, palette=sns.color_palette())
ax.set(yticklabels=[])
ax.tick_params(left=False)
ax.bar_label
# change_width(ax, .75)
change_height(ax, 1)
ax.legend(bbox_to_anchor=(1, .5))
plt.show()
我试过改变我的身材尺寸,认为这可能是原因。这并没有影响酒吧的位置。
我正在使用自定义高度函数来创建条形图而不是细线。如果我不应用条形的自定义大小,线条不会与图形边缘相对,但您无法真正看到线条,这就是我使用自定义设置的原因。也许我需要在函数中添加一些东西? (附加视图的自定义高度和宽度)
def change_width(ax, new_value):
for patch in ax.patches:
current_width = patch.get_width()
diff = current_width - new_value
patch.set_width(new_value)
patch.set_x(patch.get_x() + diff * .5)
def change_height(ax, new_value):
for patch in ax.patches:
current_height = patch.get_height()
diff = current_height - new_value
patch.set_height(new_value)
patch.set_y(patch.get_y() + diff * .5)
任何人都可以对此提供一些见解吗?
要在更改矩形后重新计算限制,您可以使用:
ax.relim()
ax.autoscale_view()
ax.margins(y=0.01) # for a smaller padding, as the default is rather wide
但您可能只想设置 dodge=False
。否则,每个 hue-values 为每个 y-positions.
一些其他备注:
- 默认的
sns.color_palette()
只有10种不同的颜色,导致图例混乱;你可以使用例如turbo
或tab20
更多颜色 - 要添加条形标签,您可以遍历
ax.containers
,例如:for bars_group in ax.containers: ax.bar_label(bars_group)
(每个色调值有一个“组”) - 如果为图例设置
bbox_to_anchor=
,则还需要设置loc=
;默认使用loc='best',可以根据plot内容改变
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
fig, ax = plt.subplots(figsize=(17, 10))
fig.patch.set_visible(False)
x = [1, 4, 7, 9, 11, 12, 14, 16, 19, 20, 23, 24, 27, 28, 31, 32, 35.5, 40, 39, 40]
y = ['Hydrogen', 'Helium', 'Lithium', 'Berylium', 'Boron', 'Carbon', 'Nitrogen', 'Oxygen', 'Fluorine', 'Neon', 'Sodium', 'Magnesium', 'Aluminium', 'Silicon', 'Phosphorus', 'Sulphur', 'Chlorine', 'Argon', 'Potassium', 'Calcium']
ax = sns.barplot(x=x, y=y, hue=y, orient='h', dodge=False, ax=ax, palette='turbo')
ax.tick_params(left=False, labelleft=False)
for bars_group in ax.containers:
ax.bar_label(bars_group, padding=3, fontsize=15)
ax.legend(bbox_to_anchor=(1, .5), loc='center left')
# ax.margins(x=0.15) # optionally more space for the text
sns.despine()
plt.tight_layout()
plt.show()