300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > matplotlib seaborn 绘制多图

matplotlib seaborn 绘制多图

时间:2023-07-31 08:20:06

相关推荐

matplotlib seaborn 绘制多图

matplotlib、seaborn 绘制多图-函数编写思路

对于这种不常用,但有时候做分析又会用的到,因为绘图用的少,总是会在生产中被卡壳。谨以此记录python 的漫漫路
对于这种不常用,但有时候做分析又会用的到,因为绘图用的少,总是会在生产中被卡壳。谨以此记录python 的漫漫路

源代码是在jupyter-lab上面写的,为了方便阅读,就全部放在了一起。

import numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport seaborn as sns### 查看分类数据的不同属性值的表现差异rn = np.random.randn(100,2)rn1 = np.random.randint(10,size=(100,2))# 造数据, 这个环节随便造, 当时是想到哪儿写到哪儿rndf = pd.DataFrame(data=rn,columns=['r1','r2'])rndf['flg'] = 'bad'rndf1 = pd.DataFrame(data=rn1,columns=['r1','r2'])rndf1['flg'] = 'god'rnd = pd.concat([rndf,rndf1])rnd['r3'] = np.random.randint(10,size=(200,1))rnd['r4'] = np.random.randint(low=-10, high=10, size=(200,1))rnd['r5'] = np.random.randint(low=10, high=100, size=(200,1))rnd['r6'] = np.random.randint(low=60, high=100, size=(200,1))rnd['r7'] = np.random.randint(low=-100, high=100 ,size=(200,1))# 重点编写函数def show_graph(df, lst):"""批量绘制box分布图形, 适用于分类标签的不同指标值的分布数据查看, 对于指标值很多的时候比较适用df : DataFrame数据lst : 需要绘制图形的字段"""sns.set()fig = plt.figure(figsize=(16,10)) # 这里的画布大小还是先设置好吧。for i in range(len(lst)):# 遍历每张子图ax = fig.add_subplot(2, 4, i + 1)sns.boxplot(x='flg', y=lst[i], hue='flg', data=rnd, ax=ax, meanline=True, showmeans=True, )# 单独设置了每个子图的标题, 为了方便查看, 也可以省略ax.set_title(lst[i]+'_index_contrast')# 设置子图之间的横纵边距, 不然会挤到一块, 影响阅读plt.subplots_adjust(wspace=0.3,hspace=0.3)# 函数调用cols = []# 提取需要绘图的字段, 遍历所有字段, 做下判断收入即可for ls in list(rnd.columns.unique()):if ls.startswith('r'):cols.append(ls)show_graph(df=rnd, lst=cols)plt.savefig('boxplots.png',dpi=1000)# 下图为保存在本地的图片

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。