繁体   English   中英

我在 Power BI 中使用 Python 脚本。 如何格式化多个 seaborn 'displot' 的 x 轴刻度标签和标题

[英]I am using Python script in Power BI. How can I format the x axis tick labels and titles for a multiple seaborn 'displot'

在这方面是新的,我可以使用一些帮助。 我正在使用下面的代码。 最后应该将 x 轴格式化为货币并添加网格线的 3 行似乎不起作用,但它们也没有返回错误。 我还想更改或隐藏每个子图顶部的标题。 是否可以隐藏y轴?

    import seaborn as sns
    import matplotlib.pyplot as plt
    import matplotlib.ticker as ticker
    
    colors =["#FF0B04","#228800","#ffbb33"]
    sns.set_palette(sns.color_palette(colors))
    hue_order = ['A', 'B', 'C']
    sns.set_context("notebook", font_scale=1.2, rc={"font.size":5,"axes.labelsize":12})
    
    g = sns.JointGrid()
    
    sns.displot(data=dataset, x="Profit",col= 'ClassificationChar', bins=50,hue= 'ClassificationChar', fill=True, stat="count", legend=False )
    
    ax = g.ax_joint
    
    ax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: '£{:,.0f}K'.format(x/1000)))
    ax.grid(b=True, which='major')
    ax.grid(b=True, which='minor')
    
    
    plt.tight_layout()
    plt.show()

在此处输入图像描述

我改变了方法,通过分别绘制每个元素来达到预期的结果。

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

colors =["#FF0B04","#228800","#ffbb33"]
sns.set_palette(colors)

sns.set_context("notebook", font_scale=1.2, rc={"font.size":5, "axes.labelsize":12})
sns.set_style("ticks", {"xtick.major.size":8,"ytick.major.size":8})
current_palette = sns.color_palette(colors)

x, y, z= dataset["OverallTotalScore%"], dataset["Profit"], dataset["ClassificationChar"]


f, (ax1, ax2, ax3) = plt.subplots (1,3, figsize=(16, 6), sharey= True)
sns.despine(left=False, bottom=False, offset=0)

sns.histplot(x=y, hue=z, hue_order='A', stat="count", alpha = 1, palette = ['#228800'], kde=False, fill=True, legend=False, bins=50, ax=ax1)
sns.histplot(x=y, hue=z, hue_order='B', stat="count", alpha = 1, palette = ['#ffbb33'], kde=False, fill=True, legend=False, bins=50, ax=ax2)
sns.histplot(x=y, hue=z, hue_order='C', stat="count", alpha = 1, palette = ['#FF0B04'], kde=False, fill=True, legend=False, bins=50, ax=ax3)

ax1.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: '£{:,.0f}K'.format(x/1000)))
ax2.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: '£{:,.0f}K'.format(x/1000)))
ax3.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: '£{:,.0f}K'.format(x/1000)))

ax1.grid(b=True, which='major')
ax1.grid(b=True, which='minor')
ax2.grid(b=True, which='major')
ax2.grid(b=True, which='minor')
ax3.grid(b=True, which='major')
ax3.grid(b=True, which='minor')

ax1.legend().set_title('Class A')
ax2.legend().set_title('Class B')
ax3.legend().set_title('Class C')

plt.style.use("classic")
plt.tight_layout()
plt.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM