3.3.2 各种图形绘制
首先我们需要下载几个csv 文件:
链接: https://pan.baidu.com/s/12CkTweXPT-El4z2M93HltQ?pwd=vaks
提取码: vaks
下载完成之后,把该文件和我们的代码放到同一个文件夹下,这一操作我们在之前的博客中已经反复说到,这里就不再进行演示
3.3.2.1 调色板
参数palette(调色板),用于调整颜色,系统默认提供了六种选择:deep,muted,bright,pastel,dark,colorblind
参数palette调色板,可以有更多的颜色选择,Matplotlib为我们提供了多达178种,这足够绘图用,可以通过代码print(plt.colormaps())
查看选择
import numpy as np import matplotlib.pyplot as plt import seaborn as sns print(plt.colormaps())
3.3.2.2 线形图
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd # 设置样式 sns.set(style = 'dark', context = 'notebook', font = 'STKaiti') plt.figure(figsize = (9, 6)) # fmri 这一核磁共振数据 fmri = pd.read_csv('./fmri.csv') ax = sns.lineplot(x = 'timepoint',y = 'signal', hue = 'event', # 根据 event 进行分类绘制 style = 'event', # 根据 event 属性分类指定样式 # 如图自动分配成了实现和虚线,●和× data = fmri, palette = 'deep', # 画板、颜色 markers = True, markersize = 10) plt.xlabel('时间节点',fontsize = 30)
3.3.2.3 散点图
import pandas as pd import matplotlib.pyplot as plt import seaborn as sns data = pd.read_csv('./tips.csv') # 小费 plt.figure(figsize = (9, 6)) sns.set(style = 'darkgrid', context = 'talk') # 散点图 fig = sns.scatterplot(x = 'total_bill', y = 'tip', hue = 'time', data = data, palette = 'autumn', s = 100)
3.3.2.4 柱状图
import pandas as pd import seaborn as sns import matplotlib.pyplot as plt plt.figure(figsize = (9, 6)) sns.set(style = 'whitegrid') tips = pd.read_csv('./tips.csv') # 小费 ax = sns.barplot(x = "day", y = "total_bill", data = tips,hue = 'sex', palette = 'colorblind', capsize = 0.2)