当前位置:网站首页>Seaborn draws 11 histograms

Seaborn draws 11 histograms

2022-07-05 16:21:00 Junhong's road of data analysis

This article introduces how to use seaborn To draw various histogram

  • Basic histogram

  • Horizontal histogram

  • Title Setting

  • be based on DataFrame mapping

  • hue Parameter setting

  • Color treatment

  • Multidimensional processing

One I like very much Seaborn Drawn graphics :

74a92568dd25a046c3a9216b9d43c870.jpeg

Import library

Seaborn yes matplotlib The advanced packaging of , therefore matplotlib You still need to import at the same time :

In [1]:

import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

sns.set_theme(style="whitegrid")
sns.set_style('darkgrid')

Import built-in data

It uses seaborn A built-in consumption tips Data sets :

In [2]:

tips = sns.load_dataset("tips")
tips.head()
5c2db2047bceba20ab1ab9d9952e3e8d.jpeg

Basic histogram

In [3]:

x = ["A","B","C"]
y = [1, 2, 3]

sns.barplot(x, y)
plt.show()
9a163fa1946eeb8e72768e7b7bde910c.jpeg

Draw a horizontal histogram :

#  Horizontal histogram 

x = ["A","B","C"]
y = [1, 2, 3]

sns.barplot(y, x)
plt.show()
c37dca9f22aa935ccf1741b533cc933b.jpeg

Set title

In [14]:

x = ["A","B","C"]
y = [1, 2, 3]

fig = sns.barplot(x, y)
fig.set_title('title of seaborn')

plt.show()
9b03be4248a9d405b3e48b5615647c16.jpeg

Appoint x-y-data

In [5]:

#  adopt DataFrame To specify the 

ax = sns.barplot(x="day", y="tip", data=tips)
plt.show()
c90a3d65b6705e584a90b75b4baa2268.jpeg

hue Parameters

Implemented grouped display data

In [6]:

ax = sns.barplot(x="day", 
        y="total_bill", 
        hue="sex", 
        data=tips)
3e61eed7fb5bef9e542d00d5d9c7c6ff.jpeg

Horizontal histogram

In [7]:

ax = sns.barplot(x="total_bill", 
                 y="day", 
                 data=tips)
53811f014ad535cc58431adfd562a4ec.jpeg

Custom order

In [8]:

ax = sns.barplot(x="total_bill", 
                 y="day", 
                 #  add to order Parameters , order of appointment 
                 order=["Sat","Fri","Sun","Thur"],  #  Customize 
                 data=tips)
c8ae8ebafd74641b9519a016a7ecd888.jpeg

Color treatment

Use a color

In [9]:

ax = sns.barplot(x="size", 
                 y="total_bill", 
                 data=tips,
                 color="salmon", 
                 saturation=.5)
3253936775d77cbc4d585414cf41b334.jpeg

Color gradient

In [10]:

ax = sns.barplot(x="size", 
                 y="tip", 
                 data=tips,
                 palette="Blues")
06c946a6e19ebf5ee1ed01e99fff7595.jpeg

Multidimensional grouping

In [11]:

g = sns.catplot(x="sex", 
                y="total_bill",
                hue="smoker", 
                col="time",
                data=tips, 
                kind="bar",
                height=4, 
                aspect=.7)
c7928e56b2af3e37491aa0c4595aad66.jpeg

True/False grouping

In [12]:

tips["weekend"] = tips["day"].isin(["Sat", "Sun"])
tips

Out[12]:

359657eeaf4d036310a6684f654c0901.jpeg

In [13]:

ax = sns.barplot(x="day", 
                 y="tip", 
                 hue="weekend",
                 data=tips, 
                 dodge=False)
f35ceee620ccac8558cabd3fdc0f815f.jpeg
- END -
原网站

版权声明
本文为[Junhong's road of data analysis]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051543320510.html