Seaborn Box and Whisker 图,X 和 Y 标签,让它变得大胆?
Seaborn Box and Whisker plot, X and Y labels, making it bold?
sns.boxplot(x=df['NAME'], y=df['QTY'], showmeans=True, meanprops={"marker":"o",
"markerfacecolor":"red",
"markeredgecolor":"black",
"markersize":"2"})
以上是在 Seaborn 中创建箱线图的代码行。
只是想知道是否有人知道如何使 X 轴标签 (NAME) 和 Y 轴标签 (QTY) 在图表上显示为“粗体”。我不想添加带有 X 和 Y 标签的新代码行,因为这两个值可能会不时更改。必须有一种方法可以在该行某处插入 'fontweight = 'bold'。
此外,如果有办法将 X 轴值(所有名称)旋转 90 度。它们都显示为水平。
非常感谢
您可以使用 ax = sns.boxplot(...)
来捕获创建情节的子情节。然后 ax.get_xlabel()
returns 现有的 x-label 可用于再次设置它并更新其属性。 sns.boxplot()
命令本身无法包含所有这些内容。
同样,可以通过 ax.tick_params(axis='x', labelrotation=...)
更改刻度标签的方向。请注意,plt.tightlayout()
会将标签很好地融入周围的绘图中。
下面是一些使用标准泰坦尼克号数据集的示例代码:
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('titanic')
ax = sns.boxplot(x=df['class'], y=df['age'], showmeans=True,
meanprops={"marker": "o", "markerfacecolor": "red", "markeredgecolor": "black", "markersize": "2"})
ax.set_xlabel(ax.get_xlabel(), fontdict={'weight': 'bold'})
ax.set_ylabel(ax.get_ylabel(), fontdict={'weight': 'bold'})
ax.tick_params(axis='x', labelrotation=45)
plt.tight_layout()
plt.show()
sns.boxplot(x=df['NAME'], y=df['QTY'], showmeans=True, meanprops={"marker":"o",
"markerfacecolor":"red",
"markeredgecolor":"black",
"markersize":"2"})
以上是在 Seaborn 中创建箱线图的代码行。
只是想知道是否有人知道如何使 X 轴标签 (NAME) 和 Y 轴标签 (QTY) 在图表上显示为“粗体”。我不想添加带有 X 和 Y 标签的新代码行,因为这两个值可能会不时更改。必须有一种方法可以在该行某处插入 'fontweight = 'bold'。
此外,如果有办法将 X 轴值(所有名称)旋转 90 度。它们都显示为水平。
非常感谢
您可以使用 ax = sns.boxplot(...)
来捕获创建情节的子情节。然后 ax.get_xlabel()
returns 现有的 x-label 可用于再次设置它并更新其属性。 sns.boxplot()
命令本身无法包含所有这些内容。
同样,可以通过 ax.tick_params(axis='x', labelrotation=...)
更改刻度标签的方向。请注意,plt.tightlayout()
会将标签很好地融入周围的绘图中。
下面是一些使用标准泰坦尼克号数据集的示例代码:
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('titanic')
ax = sns.boxplot(x=df['class'], y=df['age'], showmeans=True,
meanprops={"marker": "o", "markerfacecolor": "red", "markeredgecolor": "black", "markersize": "2"})
ax.set_xlabel(ax.get_xlabel(), fontdict={'weight': 'bold'})
ax.set_ylabel(ax.get_ylabel(), fontdict={'weight': 'bold'})
ax.tick_params(axis='x', labelrotation=45)
plt.tight_layout()
plt.show()