PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 7, 2022

[FIXED] How to show the y-axis of seaborn displot as percentage

 October 07, 2022     histogram, matplotlib, python, seaborn, statistics     No comments   

Issue

I'm using seaborn.displot to display a distribution of scores for a group of participants.

Is it possible to have the y axis show an actual percentage (example below)?

This is required by the audience for the data. Currently it is done in excel but It would be more useful in python.

import seaborn as sns

data = sns.load_dataset('titanic')

p = sns.displot(data=data, x='age', hue='sex', height=4, kind='kde')

enter image description here

Desired Format

enter image description here


Solution

As mentioned by @JohanC, the y axis for a KDE is a density, not a proportion, so it does not make sense to convert it to a percentage.

You'd have two options. One would be to plot a KDE curve over a histogram with histogram counts expressed as percentages:

sns.displot(
    data=tips, x="total_bill", hue="sex",
    kind="hist", stat="percent", kde=True,
)

enter image description here

But your "desired plot" actually doesn't look like a density at all, it looks like a histogram plotted with a line instead of bars. You can get that with element="poly":

sns.displot(
    data=tips, x="total_bill", hue="sex",
    kind="hist", stat="percent", element="poly", fill=False,
)

enter image description here



Answered By - mwaskom
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing