3.3.2.5 箱式图
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd sns.set(style = 'ticks') tips = pd.read_csv('./tips.csv') ax = sns.boxplot(x = "day", y = "total_bill", data = tips, palette = 'colorblind')
3.3.2.6 直方图
import seaborn as sns import numpy as np import matplotlib.pyplot as plt sns.set(style = 'dark') x = np.random.randn(5000) sns.histplot(x, kde = True)
import seaborn as sns import numpy as np import matplotlib.pyplot as plt import pandas as pd sns.set(style = 'darkgrid') tips = pd.read_csv('./tips.csv') sns.histplot(x = 'total_bill', data = tips, kde = True)
3.3.2.7 分类散点图
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd sns.set(style = 'darkgrid') exercise = pd.read_csv('./exercise.csv') sns.catplot(x = "time", y = "pulse", hue = "kind", data = exercise)
3.3.2.8 热力图
import matplotlib.pyplot as plt import seaborn as sns import pandas as pd plt.figure(figsize = (12, 9)) flights = pd.read_csv('./flights.csv') # 飞行数据 # pivot() 实现了数据重塑,改变了DataFrame的形状 # month 作为行索引,year作为列索引,passengers作为数据 flights = flights.pivot("month", "year", "passengers") # 年,月,乘客 sns.heatmap(flights, annot = True, # 画上数值 fmt = 'd', # 数值为整数 cmap = 'RdBu_r', # 设置颜色 linewidths = 0.5) # 线宽为 0.5
我们最后来说一下数据重塑,在本题的基础上,我们查看一下我们的flights 数据:
咋们再来重新加载一下数据,看看原始的flights 数据:
import matplotlib.pyplot as plt import seaborn as sns import pandas as pd flights = pd.read_csv('./flights.csv') flights
不难看出,上述绘图过程中涉及到了数据重塑:代码:flights = flights.pivot("month", "year", "passengers")
实现了数据的重塑,使得month 作为行索引,yerr 作为列索引,passengers 作为数据。