简体   繁体   中英

How do I change the y axis on facetgrid python to percentage?

Python matplotlib sns facetgrid y axis question

Hello!

I've been learning about python for data analysis and I'm stuck on something I can't find an answer to.

I want to change the y axis on my sns facegrid chart to a percentage. Below is the code and image...

g = sns.FacetGrid(data, col = 'Survived')
g.map(plt.hist, 'Age', bins=20)

Here is the output:

这是输出

You could use density=True , see docs .

Then change the y-label to percentages, like this example .

Which results in;

import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col='smoker', col_order=['Yes','No'])
g.map(plt.hist, 'total_bill', density=True, bins=20, color='m')

from matplotlib.ticker import FuncFormatter
def to_percent(y, position):
    s = str(100*y)
    return s + '%'
formatter = FuncFormatter(to_percent)
plt.gca().yaxis.set_major_formatter(formatter)
plt.show()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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