首页 > 解决方案 > 将 x 轴格式化为 %,将 y 轴格式化为 £

问题描述

我在 Power BI 中使用了这个 python 代码:

colors = ["#FF0B04", "#ffbf00", "#228800"]
sns.set_palette(sns.color_palette(colors))
g = sns.JointGrid(data=dataset, x="Score", y="Profit", hue= 'Score Bands')
g.plot(sns.scatterplot, sns.histplot,legend=False)
plt.tight_layout()
plt.show()

创建情节: 在此处输入图像描述

任何人都可以帮助将 x 轴格式化为 % 并将 y 轴格式化为 £ 吗?

标签: pythonmatplotlibseaborn

解决方案


使用来自官方网站的示例数据,我将 seaborn 与 ax 相关联,然后使用 formatter 以 unicode 指定百分比显示和欧元货币符号。

import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

tips = sns.load_dataset("tips")
colors = ["#FF0B04", "#ffbf00", "#228800"]
sns.set_palette(sns.color_palette(colors))
g = sns.JointGrid(data=tips, x="total_bill", y="tip", hue= 'time')
g.plot(sns.scatterplot, sns.histplot,legend=False)
ax = g.ax_joint

ax.xaxis.set_major_formatter(ticker.PercentFormatter(xmax=5))
ax.yaxis.set_major_formatter(ticker.FormatStrFormatter("\u20ac%d"))

plt.tight_layout()
plt.show()

在此处输入图像描述


推荐阅读