網(wǎng)站首頁 編程語言 正文
本文速覽
1、繪圖數(shù)據(jù)準(zhǔn)備
依舊使用鳶尾花iris數(shù)據(jù)集,詳細(xì)介紹見之前文章。
#導(dǎo)入本帖要用到的庫,聲明如下: import matplotlib.pyplot as plt import numpy as np import pandas as pd import palettable from pandas import Series,DataFrame from sklearn import datasets import seaborn as sns import palettable #導(dǎo)入鳶尾花iris數(shù)據(jù)集(方法一) #該方法更有助于理解數(shù)據(jù)集 iris=datasets.load_iris() x, y =iris.data,iris.target y_1 = np.array(['setosa' if i==0 else 'versicolor' if i==1 else 'virginica' for i in y]) pd_iris = pd.DataFrame(np.hstack((x, y_1.reshape(150,1))),columns=['sepal length(cm)','sepal width(cm)','petal length(cm)','petal width(cm)','class']) #astype修改pd_iris中數(shù)據(jù)類型object為float64 pd_iris['sepal length(cm)']=pd_iris['sepal length(cm)'].astype('float64') pd_iris['sepal width(cm)']=pd_iris['sepal width(cm)'].astype('float64') pd_iris['petal length(cm)']=pd_iris['petal length(cm)'].astype('float64') pd_iris['petal width(cm)']=pd_iris['petal width(cm)'].astype('float64') #導(dǎo)入鳶尾花iris數(shù)據(jù)集(方法二) #該方法有時候會卡巴斯基,所以棄而不用 #import seaborn as sns #iris_sns = sns.load_dataset("iris")
數(shù)據(jù)集簡單查看
2、seaborn.regplot
seaborn.regplot(x, y, data=None, x_estimator=None, x_bins=None, x_ci='ci', scatter=True, fit_reg=True, ci=95, n_boot=1000, units=None, seed=None, order=1, logistic=False, lowess=False, robust=False, logx=False, x_partial=None, y_partial=None, truncate=True, dropna=True, x_jitter=None, y_jitter=None, label=None, color=None, marker='o', scatter_kws=None, line_kws=None, ax=None)
regplot默認(rèn)參數(shù)線型回歸圖
plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2)#設(shè)置主題,文本大小 g=sns.regplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, color='#000000',#設(shè)置marker及線的顏色 marker='*',#設(shè)置marker形狀 )
分別設(shè)置點和擬合線屬性
plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2) g=sns.regplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, color='#000000', marker='*', scatter_kws={'s': 60,'color':'g',},#設(shè)置散點屬性,參考plt.scatter line_kws={'linestyle':'--','color':'r'}#設(shè)置線屬性,參考 plt.plot
置信區(qū)間(confidence interval)設(shè)置
注意擬合線周圍陰影面積變化?
plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2) g=sns.regplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, color='#000000', marker='*', ci=60,#置信區(qū)間設(shè)置,默認(rèn)為95%置信區(qū)間,越大線周圍陰影部分面積越大 )
擬合線延伸與坐標(biāo)軸相交?
# extend the regression line to the axis limits plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2) g=sns.regplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, color='#000000', marker='*', truncate=False,#讓擬合線與軸相交 )
擬合離散變量曲線
plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2) x_discrete=[0 if i=='setosa' else 1 if i=='versicolor' else 2 for i in pd_iris['class']]# g=sns.regplot(x=x_discrete, y='sepal width(cm)', data=pd_iris,#x此時為離散變量 color='#000000', marker='*', )
多項式回歸( polynomial regression)擬合曲線
plt.figure(dpi=110) sns.set(style="whitegrid",font_scale=1.2) g=sns.regplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, marker='*', order=4,#默認(rèn)為1,越大越彎曲 scatter_kws={'s': 60,'color':'#016392',},#設(shè)置散點屬性,參考plt.scatter line_kws={'linestyle':'--','color':'#c72e29'}#設(shè)置線屬性,參考 plt.plot )
3、seaborn.lmplot
seaborn.lmplot(x, y, data, hue=None, col=None, row=None, palette=None, col_wrap=None, height=5, aspect=1, markers='o', sharex=True, sharey=True, hue_order=None, col_order=None, row_order=None, legend=True, legend_out=True, x_estimator=None, x_bins=None, x_ci='ci', scatter=True, fit_reg=True, ci=95, n_boot=1000, units=None, seed=None, order=1, logistic=False, lowess=False, robust=False, logx=False, x_partial=None, y_partial=None, truncate=True, x_jitter=None, y_jitter=None, scatter_kws=None, line_kws=None, size=None)
seaborn.lmplot結(jié)合seaborn.regplot()和FacetGrid,比seaborn.regplot()更靈活,可繪制更個性化的圖形。
按變量分類擬合回歸線
plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2) g=sns.lmplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, hue='class', ) g.fig.set_size_inches(10,8)
散點marker設(shè)置
plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2) g=sns.lmplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, hue='class', markers=['+','^','o'], #設(shè)置散點marker ) g.fig.set_size_inches(10,8)
散點調(diào)色盤
plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2) g=sns.lmplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, hue='class', markers=['+','^','*'], scatter_kws={'s':180}, palette=["#01a2d9", "#31A354", "#c72e29"],#調(diào)色盤 ) g.fig.set_size_inches(10,8)
擬合線屬性設(shè)置
plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2) g=sns.lmplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, hue='class', markers=['+','^','*'], scatter_kws={'s':180}, line_kws={'linestyle':'--'},#擬合線屬性設(shè)置 palette=["#01a2d9", "#31A354", "#c72e29"], ) g.fig.set_size_inches(10,8)
繪制分面圖?
plt.figure(dpi=100) sns.set(style="whitegrid",font_scale=1.2) g=sns.lmplot(x='sepal length(cm)', y='sepal width(cm)', data=pd_iris, col='class',#按class繪制分面圖 markers='*', scatter_kws={'s':150,'color':'#01a2d9'}, line_kws={'linestyle':'--','color':'#c72e29'},#直線屬性設(shè)置 ) g.fig.set_size_inches(10,8)
原文鏈接:https://blog.csdn.net/qq_21478261/article/details/108174767
相關(guān)推薦
- 2022-08-11 boost字符串處理函數(shù)format的用法_C 語言
- 2022-09-22 Mybaits一級緩存和二級緩存分別是什么,區(qū)別是什么?
- 2022-03-31 C#算法之羅馬數(shù)字轉(zhuǎn)整數(shù)_C#教程
- 2022-04-02 ?Python錯誤與異常處理_python
- 2023-02-02 C++實現(xiàn)延遲的方法詳解_C 語言
- 2024-07-14 springboot通過CompletableFuture開啟多線程執(zhí)行任務(wù)
- 2023-02-17 docker快速部署zabbix的方法_docker
- 2022-11-16 生產(chǎn)redisson延時隊列不消費問題排查解決_Redis
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支