简体   繁体   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'

New on this and I could use some help.在这方面是新的,我可以使用一些帮助。 I am using the code below.我正在使用下面的代码。 The 3 lines at the end that supposed to format the x axis to currency and add gridlines doesn't seem to work but they are not returning an error neither.最后应该将 x 轴格式化为货币并添加网格线的 3 行似乎不起作用,但它们也没有返回错误。 I would also like to change or hide the titles on the top of each subplot.我还想更改或隐藏每个子图顶部的标题。 Is it possible to hide the y axis?是否可以隐藏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()

在此处输入图像描述

I changed the approach and I came to the desired outcome by plotting every element separately.我改变了方法,通过分别绘制每个元素来达到预期的结果。

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.

相关问题 如何旋转 seaborn 条形图 x 轴刻度标签 - How to rotate seaborn barplot x-axis tick labels 如何使用python在x轴或y轴标签之间添加更多刻度? - How can I add more ticks between x-axis or y-axis labels using python? 如何更改 seaborn 小提琴 plot 的 x 轴标签而不返回并更改数据? - How can I change the labels of the x-axis of a seaborn violin plot without going back and changing the data? X-Tick 标签不会旋转! [如何使用子图和图形在 Seaborn 中旋转 X-Tick 标签] - X-Tick Labels Are Not Getting Rotated! [How to Rotate X-Tick Labels in Seaborn Using Subplots & Figure] 如何在 Matplotlib 和 Seaborn 中的 x 轴刻度标记(不是刻度标签)和 x 轴之间添加填充 - How do you add padding between the x-axis tick marks (not tick labels) and the x-axis in Matplotlib and Seaborn 如何在 seaborn FacetGrid 中格式化 y 轴或 x 轴标签 - How to format the y- or x-axis labels in a seaborn FacetGrid 使用 seaborn displot 我无法指定色调来分隔 kde plot 的三个类别 - Using seaborn displot I am unable to specify the hue to separate three categories of a kde plot 如何使用 displot 在 python 中制作 seaborn plot ,其中我们计算一个字段中的唯一值而不是总行数? - How can I make a seaborn plot in python with displot where we count unique values in one field rather than the total number of rows? 如何在 python 条形图中添加 x 轴刻度标签 - How to add x-axis tick labels in python bar chart 如何在python中使用matplotlib设置x轴刻度标记标签? - How to set x-axis tick mark labels using matplotlib in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM