簡體   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