In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
In [2]:
penguins = sns.load_dataset('penguins',data_home='.')
In [3]:
penguins.head(2)
Out[3]:
In [4]:
pic = sns.histplot(penguins, x="flipper_length_mm")
pic.set_title('flipper_length_mm')
Out[4]:
In [5]:
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[0])
pic = sns.histplot(penguins, x="body_mass_g", ax=ax[1])
In [6]:
ax=plt.axes((0.1, 0.1, 0.8, 0.7), facecolor='green')
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax)
In [7]:
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[0])
pic.set_title('x')
pic = sns.histplot(penguins, y="flipper_length_mm", ax=ax[1])
pic.set_title('y')
Out[7]:
In [8]:
fig, ax =plt.subplots(1,5,constrained_layout=True, figsize=(15, 3))
_ = sns.histplot(penguins, x="flipper_length_mm", stat="count", ax=ax[0]) # count, 也是默认值
_ = sns.histplot(penguins, x="flipper_length_mm", stat="frequency", ax=ax[1]) # frequency
_ = sns.histplot(penguins, x="flipper_length_mm", stat="probability", ax=ax[2])# probability
_ = sns.histplot(penguins, x="flipper_length_mm", stat="percent", ax=ax[3]) # percent
_ = sns.histplot(penguins, x="flipper_length_mm", stat="density", ax=ax[4]) # density
In [9]:
fig, ax =plt.subplots(1,3,constrained_layout=True, figsize=(12, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[0], bins=5)
pic.set_title('bins=5')
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[1], bins=10)
pic.set_title('bins=10')
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[2], bins=[150, 175, 200, 225, 250])
pic.set_title('bins=[150, 175, 200, 225, 250]')
Out[9]:
In [10]:
fig, ax =plt.subplots(2,4,constrained_layout=True, figsize=(15, 6))
pic = sns.histplot(penguins, x="flipper_length_mm", bins="auto", ax=ax[0][0]) # count, 也是默认值
pic.set_title('auto')
pic = sns.histplot(penguins, x="flipper_length_mm", bins="fd", ax=ax[0][1]) # frequency
pic.set_title('fd')
pic = sns.histplot(penguins, x="flipper_length_mm", bins="doane", ax=ax[0][2])# probability
pic.set_title('doane')
pic = sns.histplot(penguins, x="flipper_length_mm", bins="scott", ax=ax[0][3]) # percent
pic.set_title('scott')
pic = sns.histplot(penguins, x="flipper_length_mm", bins="stone", ax=ax[1][0]) # count, 也是默认值
pic.set_title('stone')
pic = sns.histplot(penguins, x="flipper_length_mm", bins="rice", ax=ax[1][1]) # frequency
pic.set_title('rice')
pic = sns.histplot(penguins, x="flipper_length_mm", bins="sturges", ax=ax[1][2])# probability
pic.set_title('sturges')
pic = sns.histplot(penguins, x="flipper_length_mm", bins="sqrt", ax=ax[1][3]) # percent
pic.set_title('sqrt')
Out[10]:
In [11]:
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
_ = sns.histplot(penguins, x="flipper_length_mm", ax=ax[0], binwidth=1)
_ = sns.histplot(penguins, x="flipper_length_mm", ax=ax[1], binwidth=3)
In [12]:
sns.histplot(penguins, x="flipper_length_mm", cumulative=True)
Out[12]:
In [13]:
sns.histplot(penguins, x="flipper_length_mm", multiple="layer")
Out[13]:
In [14]:
_ = sns.histplot(penguins, x="flipper_length_mm", hue="species")
In [15]:
fig, ax =plt.subplots(1,3,constrained_layout=True, figsize=(12, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", hue="species", ax=ax[0], element="bars")
pic.set_title('element="bars"')
pic = sns.histplot(penguins, x="flipper_length_mm", hue="species", ax=ax[1], element="step")
pic.set_title('element="step')
pic = sns.histplot(penguins, x="flipper_length_mm", hue="species", ax=ax[2], element="poly")
pic.set_title('element="poly"')
Out[15]:
In [16]:
fig, ax =plt.subplots(1,4,constrained_layout=True, figsize=(16, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", hue="species", ax=ax[0], multiple="layer")
pic.set_title('multiple="layer"')
pic = sns.histplot(penguins, x="flipper_length_mm", hue="species", ax=ax[1], multiple="dodge")
pic.set_title('multiple="dodge')
pic = sns.histplot(penguins, x="flipper_length_mm", hue="species", ax=ax[2], multiple="stack")
pic.set_title('multiple="stack"')
pic = sns.histplot(penguins, x="flipper_length_mm", hue="species", ax=ax[3], multiple="fill")
pic.set_title('multiple="fill"')
Out[16]:
In [17]:
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[0], kde=False) # 默认值,不生成核密度曲线
pic.set_title('kde=False')
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[1], kde=True) # 值为True,显示核密度曲线
pic.set_title('kde=True')
Out[17]:
In [18]:
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[0], color="#FFC0CB") # 可以使16进制颜色
pic.set_title('color="#FFC0CB"')
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[1], color="orange") # 也可以是 英文颜色字符串
pic.set_title('color="orange"')
Out[18]:
In [19]:
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[0], color="#FFC0CB") # 可以使16进制颜色
pic.set_title('color="#FFC0CB"')
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[1], color="orange") # 也可以是 英文颜色字符串
pic.set_title('color="orange"')
Out[19]:
In [20]:
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", hue='sex', ax=ax[0], fill=False)
pic.set_title('fill=False')
pic = sns.histplot(penguins, x="flipper_length_mm", hue='sex', ax=ax[1], fill=True)
pic.set_title('fill=True')
Out[20]:
In [21]:
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", hue="sex", ax=ax[0], legend=False)
pic.set_title('legend=False')
pic = sns.histplot(penguins, x="flipper_length_mm", hue="sex", ax=ax[1], legend=True)
pic.set_title('legend=True')
Out[21]:
In [22]:
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[0], shrink=0.5)
pic.set_title('shrink=0.5')
# pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[1], bins=10)
pic = sns.histplot(penguins, x="flipper_length_mm", ax=ax[1], shrink=0.8)
pic.set_title('shrink=0.8')
Out[22]:
In [47]:
_ = sns.histplot(penguins, x="flipper_length_mm", edgecolor="red")
In [23]:
tips = sns.load_dataset("tips")
In [24]:
tips.head(2)
Out[24]:
In [25]:
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.barplot(x="total_bill", y="day", data=tips, ax=ax[0])
pic.set_title('x="total_bill", y="day"')
pic = sns.barplot(x="day", y="total_bill", data=tips, ax=ax[1])
pic.set_title('x="day", y="total_bill"')
Out[25]:
In [26]:
fig, ax =plt.subplots(1,3,constrained_layout=True, figsize=(12, 3))
pic = sns.barplot(x="day", y="total_bill", data=tips, ax=ax[0], estimator=np.mean)
pic.set_title('estimator=np.mean')
pic = sns.barplot(x="day", y="total_bill", data=tips, ax=ax[1], estimator=np.std)
pic.set_title('np.std')
pic = sns.barplot(x="day", y="total_bill", data=tips, ax=ax[2], estimator=len) # 内置方法len统计总次数
pic.set_title('len')
Out[26]:
In [27]:
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.barplot(x="day", y="total_bill", data=tips, ax=ax[0])
pic.set_title('default order')
pic = sns.barplot(x="day", y="total_bill", data=tips, order=['Sun', 'Sat', 'Thur', 'Fri'], ax=ax[1])
pic.set_title('"Sun", "Sat", "Thur", "Fri"')
Out[27]:
In [28]:
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.barplot(x="day", y="total_bill", data=tips, ax=ax[0])
pic.set_title('no hue')
pic = sns.barplot(x="day", y="total_bill", data=tips, ax=ax[1], hue="sex")
pic.set_title('hue="sex"')
Out[28]:
In [29]:
ax = sns.barplot(x="day", y="total_bill", data=tips, color="red")
In [30]:
ax = sns.barplot(x="day", y="total_bill", data=tips, color="red",saturation=0.5 )
In [31]:
ax = sns.barplot(x="day", y="total_bill", data=tips, errcolor="red")
In [32]:
ax = sns.barplot(x="day", y="total_bill", data=tips, errwidth=8)
In [33]:
ax = sns.barplot(x="day", y="total_bill", data=tips, capsize=.2)
In [34]:
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[0])
pic.set_title('x="day", y="total_bill"')
pic = sns.stripplot(x="total_bill", y="day", data=tips, ax=ax[1])
pic.set_title('x="total_bill", y="day"')
Out[34]:
In [35]:
fig, ax =plt.subplots(1,3,constrained_layout=True, figsize=(12, 3))
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[0], )
pic.set_title('no jitter')
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[1], jitter=0.05)
pic.set_title('jitter=0.05')
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[2], jitter=0.3)
pic.set_title('jitter=0.3')
Out[35]:
In [36]:
sns.stripplot(x="day", y="total_bill", data=tips, color='red')
Out[36]:
In [37]:
fig, ax =plt.subplots(1,3,constrained_layout=True, figsize=(12, 3))
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[0], )
pic.set_title('no jitter')
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[1], size=2)
pic.set_title('size=2')
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[2], size=10)
pic.set_title('size=10')
Out[37]:
In [38]:
fig, ax =plt.subplots(1,4,constrained_layout=True, figsize=(16, 3))
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[0], )
pic.set_title('no linewidth')
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[1], linewidth=1)
pic.set_title('linewidth=1')
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[2], linewidth=2)
pic.set_title('linewidth=2')
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[3], linewidth=1, edgecolor="yellow")
pic.set_title('linewidth=1, edgecolor="red"')
Out[38]:
In [39]:
fig, ax =plt.subplots(1,2,constrained_layout=True, figsize=(8, 3))
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[0], hue="smoker")
pic.set_title('hue="smoker"')
pic = sns.stripplot(x="day", y="total_bill", data=tips, ax=ax[1], hue="smoker", dodge=True)
pic.set_title('hue="smoker", dodge=True')
Out[39]: