数据可视化第二版-03部分-08章-分布
总结
本系列博客为基于《数据可视化第二版》一书的教学资源博客。本文主要是第8章,分布可视化的案例相关。
可视化视角-分布
代码实现
安装依赖
pip install scikit-learn -i https://pypi.tuna.tsinghua.edu.cn/simple pip install seaborn -i https://pypi.tuna.tsinghua.edu.cn/simple
直方图
直方图依赖
from sklearn import datasets import matplotlib.pyplot as plt import numpy as np import pandas as pd from numpy.random import randn import matplotlib as mpl import seaborn as sns from scipy.stats.kde import gaussian_kde from scipy.stats import norm from numpy import linspace, hstack from pylab import plot, show, hist
直方图在计算机视觉中的应用
参考:https://www.dandelioncloud.cn/article/details/1564611965912051714
https://www.shuzhiduo.com/A/GBJrYeBWz0/
import numpy as np from PIL import Image import matplotlib.pyplot as plt import matplotlib.cm as cm def histeq(image_array,image_bins=256): # 将图像矩阵转化成直方图数据,返回元组(频数,直方图区间坐标) image_array2,bins = np.histogram(image_array.flatten(),image_bins) # 计算直方图的累积函数 cdf = image_array2.cumsum() # 将累积函数转化到区间[0,255] cdf = (255.0/cdf[-1])*cdf # 原图像矩阵利用累积函数进行转化,插值过程 image2_array = np.interp(image_array.flatten(),bins[:-1],cdf) # 返回均衡化后的图像矩阵和累积函数 return image2_array.reshape(image_array.shape),cdf image = Image.open("pika1.jpg").convert("L") image_array = np.array(image) plt.subplot(2,2,1) plt.hist(image_array.flatten(),256) plt.subplot(2,2,2) plt.imshow(image,cmap=cm.gray) plt.axis("off") a = histeq(image_array) # 利用刚定义的直方图均衡化函数对图像进行均衡化处理 plt.subplot(2,2,3) plt.hist(a[0].flatten(),256) plt.subplot(2,2,4) plt.imshow(Image.fromarray(a[0]),cmap=cm.gray) plt.axis("off") plt.show()
pika1.jpg
输出为:
直方图案例1
# 直方图 df = datasets.load_iris() plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 plt.figure(figsize=(10, 8)) # 设置画布大小 plt.hist(df.data[:, 0], # 选择鸢尾花数据集的第一个特征 bins=20, # 设置分组数量 alpha=0.5, # 颜色透明度 color="r", # 直方图矩形填充颜色 edgecolor="black", # 直方图矩形边框颜色 range=(4, 8.5)) # 设置直方图边界 plt.xlabel(df.feature_names[0]) # x标签 plt.ylabel("频数密度") # y标签 plt.title("鸢尾花数据集特征分布直方图") plt.show()
直方图示例2
# 直方图示例 data = np.random.randn(1000) plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 plt.rcParams['axes.unicode_minus'] = False plt.figure(figsize=(10, 8)) # 设置画布大小 plt.hist(data, bins=15, # 设置分组数量 alpha=0.5, # 颜色透明度 color="blue", # 直方图矩形填充颜色 edgecolor="black") # 直方图矩形边框颜色 plt.xlabel("") # x标签 plt.ylabel("频数密度") # y标签 plt.title("直方图示例") plt.show()
直方图与趋势线
# 直方图与趋势线 plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 plt.rcParams['axes.unicode_minus'] = False sample1 = norm.rvs(loc=-1.0, scale=1, size=320) sample2 = norm.rvs(loc=2.0, scale=0.6, size=320) sample = hstack([sample1, sample2]) probDensityFun = gaussian_kde(sample) x = linspace(-5, 5, 200) plot(x, probDensityFun(x)) hist(sample, density=True, alpha=0.5, color="purple") plt.title("直方图与趋势线") show()
直方图与趋势线2
# 直方图与趋势线2 data = randn(250) sns.set_palette("hls") plt.rcParams['axes.unicode_minus'] = False mpl.rc("figure", figsize=(10, 6)) sns.displot(data, bins=10, kde=True, rug=True, color='b') plt.title("直方图与趋势线2") plt.show()
分组直方图
import os os.chdir(os.path.dirname(__file__)) iris = pd.read_csv("鸢尾花.csv") plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 plt.figure(figsize=(8, 6)) # 设置画布大小 sns.histplot(data=iris, x="Sepal.Length", hue="Species", alpha=0.5) plt.title("分组直方图") plt.xlabel("萼片长度") plt.show()
变形
# 变形 # 创建数据集 df = pd.DataFrame({ 'var1': np.random.normal(size=1000), 'var2': np.random.normal(loc=2, size=1000) * -1 }) # 画布大小 plt.rcParams["figure.figsize"] = 10, 6 plt.rcParams['axes.unicode_minus'] = False # 画变量1的频率分布直方图 sns.histplot(x=df.var1, stat="density", bins=20) # 画变量2的频率分布直方图 n_bins = 20 # 获得变量2的分组 heights, bins = np.histogram(df.var2, density=True, bins=n_bins) # 给变量2的高度乘以1 heights *= -1 bin_width = np.diff(bins)[0] bin_pos = (bins[:-1] + bin_width / 2) * -1 plt.bar(bin_pos, heights, width=bin_width, edgecolor='black') plt.title("变形") plt.show()
密度图
密度图1
# 密度图1 import matplotlib.pyplot as plt import pandas as pd import seaborn as sns import os os.chdir(os.path.dirname(__file__)) iris = pd.read_csv("鸢尾花.csv") plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 plt.figure(figsize=(8, 6)) # 设置画布大小 sns.kdeplot(data=iris, x="Sepal.Length", hue="Species", alpha=0.5, fill="Species") plt.title("密度图1") plt.show()
密度图2-堆积密度图
# 密度图2 import matplotlib.pyplot as plt import pandas as pd import seaborn as sns import os os.chdir(os.path.dirname(__file__)) iris = pd.read_csv("鸢尾花.csv") plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 sns.kdeplot(data=iris.iloc[:, [1, 2, 5]], x="Sepal.Width", hue="Species", common_norm=False, multiple="fill", alpha=1) plt.title("花萼长度关于花萼宽度的堆积密度图") plt.xlabel("花萼宽度") plt.show()
密度图3-二维密度图
# 密度图3 import matplotlib.pyplot as plt import pandas as pd import seaborn as sns import os os.chdir(os.path.dirname(__file__)) iris = pd.read_csv("鸢尾花.csv") plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 sns.kdeplot(x=iris.iloc[:, 1], y=iris.iloc[:, 2], cmap="Reds", fill=True, bw_adjust=.5) plt.xlabel("花萼长度") plt.ylabel("花萼宽度") plt.title("密度图3") plt.show()
密度图4-边际密度图
# 密度图4 import matplotlib.pyplot as plt import pandas as pd import seaborn as sns iris = pd.read_csv("鸢尾花.csv") plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 plt.rcParams['axes.unicode_minus'] = False sns.jointplot(x=iris["Petal.Length"], y=iris["Petal.Width"], kind='kde', cmap="Reds", fill=True) plt.title("密度图4") plt.show()
密度图5-镜像密度图
# 密度图5 import numpy as np from numpy import linspace import pandas as pd import seaborn as sns import matplotlib.pyplot as plt from scipy.stats import gaussian_kde # 创建数据 df = pd.DataFrame({ 'var1': np.random.normal(size=1000), 'var2': np.random.normal(loc=2, size=1000) * -1 }) plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 plt.rcParams['axes.unicode_minus'] = False # 画变量1的核密度图 sns.kdeplot(data=df, x="var1", fill=True, alpha=1) # 画变量2的密度图 kde = gaussian_kde(df.var2) x_range = linspace(min(df.var2), max(df.var2), len(df.var2)) sns.lineplot(x=x_range * -1, y=kde(x_range) * -1, color='orange') plt.fill_between(x_range * -1, kde(x_range) * -1, color='orange') plt.xlabel("数值") plt.axhline(y=0, linestyle='-', linewidth=1, color='black') plt.title("密度图5") # show the graph plt.show()
密度图6-横向密度图
# 密度图6 import matplotlib.pyplot as plt import pandas as pd import seaborn as sns import warnings warnings.filterwarnings("ignore") iris = pd.read_csv("鸢尾花.csv") plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 plt.figure(figsize=(8, 6)) # 设置画布大小 sns.kdeplot(data=iris, x="Sepal.Length", alpha=0.5, fill="red", vertical=True) plt.title("密度图6") plt.show()
箱线图
箱线图1
# 箱线图1 import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import os os.chdir(os.path.dirname(__file__)) iris = pd.read_csv("鸢尾花.csv") df = iris plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 sns.boxplot(x=df["Species"], y=df["Petal.Width"]) plt.xlabel("种类") plt.ylabel("花瓣宽度") plt.title("箱线图1", fontsize=10) plt.show()
箱线图2-带数据点的盒须图
# 箱线图2 import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import os os.chdir(os.path.dirname(__file__)) iris = pd.read_csv("鸢尾花.csv") df = iris plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 sns.boxplot(x=df["Species"], y=df["Petal.Width"]) sns.stripplot(x="Species", y="Petal.Width", data=df, jitter=0.6, color="pink") plt.xlabel("种类") plt.ylabel("花瓣宽度") plt.title("带数据点的盒须图", fontsize=10) plt.show()
箱线图3-横向合须图
# 箱线图3 import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import os os.chdir(os.path.dirname(__file__)) iris = pd.read_csv("鸢尾花.csv") df = iris sns.boxplot(y=df["Species"], x=df["Petal.Width"], ) plt.xlabel("种类") plt.ylabel("花瓣宽度") plt.title("横向盒须图", fontsize=10) plt.show()
箱线图4-分组合须图
# 箱线图4 import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import os os.chdir(os.path.dirname(__file__)) plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 iris = pd.read_csv("鸢尾花2.csv") df = iris sns.boxplot(x=df["属性"], y=df["指标值"], hue=(df["种类"]), ) plt.xlabel("属性") plt.ylabel("") plt.title("分组盒须图", fontsize=10) plt.show()
小提琴图
小提琴图-
# 小提琴图1 import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import os os.chdir(os.path.dirname(__file__)) iris = pd.read_csv("鸢尾花.csv") df = iris plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 sns.violinplot(x=df["Species"], y=df["Petal.Width"]) plt.xlabel("种类") plt.ylabel("花瓣宽度") plt.title("小提琴图", fontsize=10) plt.show()
小提琴图-学生成绩与性别以及父母婚姻状况的关系
# 小提琴图2 import seaborn as sns import matplotlib.pyplot as plt import pandas as pd import warnings import os os.chdir(os.path.dirname(__file__)) warnings.filterwarnings("ignore") score = pd.read_csv("student/student-mat.csv", sep=";") plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 df = score sns.violinplot(y=df["G3"], x=df["Pstatus"], hue=(df["sex"]), split=True) plt.xlabel("父母婚姻状况") plt.ylabel("学生分数") plt.title("学生成绩与性别以及父母婚姻状况的关系", fontsize=10) plt.show()
小提琴图-多个小提提琴图
# 小提琴图3 import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import os os.chdir(os.path.dirname(__file__)) iris = pd.read_csv("鸢尾花.csv") df = iris plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 fig = plt.figure(figsize=(14, 14)) fig.suptitle("多个小提琴图") plt.subplot(2, 2, 1) sns.violinplot(x=df["Species"], y=df["Petal.Width"], inner="box") plt.subplot(2, 2, 2) sns.violinplot(x=df["Species"], y=df["Petal.Width"], inner="point") plt.subplot(2, 2, 3) sns.violinplot(x=df["Species"], y=df["Petal.Width"], inner="stick") plt.subplot(2, 2, 4) sns.violinplot(x=df["Species"], y=df["Petal.Width"], inner="quartile") plt.show()
小提琴图-带数据点的小提琴图
# 小提琴图4 import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import os os.chdir(os.path.dirname(__file__)) iris = pd.read_csv("鸢尾花.csv") df = iris plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 sns.violinplot(x=df["Species"], y=df["Petal.Width"]) sns.stripplot(x="Species", y="Petal.Width", data=df, jitter=0.2, color="pink") plt.xlabel("种类") plt.ylabel("花瓣宽度") plt.title("带数据点的小提琴图", fontsize=10) plt.show()
小提琴图-横向小提琴图
# 小提琴图5 import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import os os.chdir(os.path.dirname(__file__)) iris = pd.read_csv("鸢尾花.csv") df = iris sns.violinplot(y=df["Species"], x=df["Sepal.Length"], ) plt.xlabel("种类") plt.ylabel("花瓣宽度") plt.title("横向小提琴图", fontsize=10) plt.show()
嵴线图
嵴线图-
# 脊线图1 import pandas as pd import matplotlib.pyplot as plt from matplotlib import cm # 色谱 import joypy import os os.chdir(os.path.dirname(__file__)) tm1 = pd.read_csv("北京pm2.5数据.csv", sep=",") plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 plt.rcParams['axes.unicode_minus'] = False tm2 = tm1.iloc[:, [2, 7]] tm2 = tm2.dropna() fig, axs = joypy.joyplot(tm2, by="month", fill=True, legend=True, alpha=.8, range_style='own', xlabelsize=22, ylabelsize=22, grid='both', linewidth=.8, linecolor='k', figsize=(8, 6), colormap=(cm.Spectral_r)) plt.title("Ridgeline plot1") plt.show()
嵴线图-
# 脊线图2 import pandas as pd import matplotlib.pyplot as plt import joypy import os os.chdir(os.path.dirname(__file__)) tm1 = pd.read_csv("北京pm2.5数据.csv", sep=",") plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 plt.rcParams['axes.unicode_minus'] = False tm2 = tm1.iloc[:, [2, 7]] tm2 = tm2.dropna() fig, axs = joypy.joyplot(tm2, by="month", fill=True, legend=True, alpha=.8, hist=True, bins=40, range_style='own', xlabelsize=22, ylabelsize=22, linewidth=.8, linecolor='k', figsize=(8, 6)) plt.title("Ridgeline plot2") plt.show()
嵴线图-
# 脊线图3 import pandas as pd import matplotlib.pyplot as plt import joypy import os os.chdir(os.path.dirname(__file__)) tm1 = pd.read_csv("鸢尾花.csv", sep=",") plt.rcParams['font.sans-serif'] = ['SimHei'] # 添加中文字体 plt.rcParams['axes.unicode_minus'] = False tm2 = tm1.dropna() fig, axs = joypy.joyplot(tm2, by="Species", column="Sepal.Length", fill=True, legend=True, alpha=.8, range_style='own', xlabelsize=22, ylabelsize=22, grid='both', linewidth=.8, linecolor='k', figsize=(8, 6)) plt.title("Ridgeline plot3") plt.show()