【python绘图】matplotlib+seaborn+pyecharts学习过程中遇到的好看的绘图技巧(超实用!)(持续更新中!)
创始人
2025-05-28 07:06:23
0

目录

  • 一些必要的库
  • 一些写的还不错的博客
  • 按照图像类型
    • 扇形图——可视化样本占比
    • 散点图——绘制双/多变量分布
      • 1. 二维散点图
      • 2. seaborn的jointplot绘制
      • 3. seaborn的jointplot绘制(等高线牛逼版)
    • 组合点阵图
      • sns.pairplot
    • 叠加图Area Plot
  • 按照功能
    • 绘制混淆矩阵
    • 绘制ROC&PR曲线(无敌)

一直以来对matplotlib以及seaborn的学习都停留在复制与粘贴与调参,因此下定决心整理一套适合自己的绘图模板以及匹配特定的应用场景,便于自己的查找与更新
目的:抛弃繁杂的参数设置学习,直接看齐优秀的模板

一些必要的库

import matplotlib.pyplot as plt
import seaborn as sns

一些写的还不错的博客

解释plt.plot(),plt.scatter(),plt.legend参数
seaborn.set()

按照图像类型

扇形图——可视化样本占比


散点图——绘制双/多变量分布

1. 二维散点图

#画散点图,第一维的数据作为x轴和第二维的数据作为y轴
# iris.target_names = array(['setosa', 'versicolor', 'virginica'], dtype='

数据集为鸢尾花,效果如下:
在这里插入图片描述

seaborn版本

sns.set(style="darkgrid")# 添加背景
chart = sns.FacetGrid(iris_df, hue="species") .map(plt.scatter, "sepal length (cm)", "sepal width (cm)") .add_legend()
chart.fig.set_size_inches(12,6)

在这里插入图片描述


2. seaborn的jointplot绘制

sns.set(style="white", color_codes=True) 
sns.jointplot(x="sepal length (cm)", y="sepal width (cm)", data=iris_df, size=5)

在这里插入图片描述


3. seaborn的jointplot绘制(等高线牛逼版)

# 没加阴影
sns.set(style="white", color_codes=True) 
sns.jointplot(x='petal length (cm)', y='sepal width (cm)',data= iris_df, kind="kde", hue='species' # 按照鸢尾花的类别进行了颜色区分
)
plt.show()
sns.jointplot(x='petal length (cm)', y='sepal width (cm)',data= iris_df,              kind="kde",hue='species',joint_kws=dict(alpha =0.6,shade = True,),marginal_kws=dict(shade=True)
)

在这里插入图片描述
在这里插入图片描述

参考文章


组合点阵图

sns.pairplot

sns.pairplot(iris_df, hue="species", size=3)

在这里插入图片描述


叠加图Area Plot

iris_df.plot.area(y=['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width 
(cm)'],alpha=0.4,figsize=(12, 6));

在这里插入图片描述


按照功能

绘制混淆矩阵

其实就是热力图:

y_pred_grid_knn = knn_grid_search.predict(X_test)
y_pred_grid_logi = logistic_grid_search.predict(X_test)
y_pred_grid_nb = naive_bayes.predict(X_test)matrix_1 = confusion_matrix(y_test, y_pred_grid_knn) 
matrix_2 = confusion_matrix(y_test, y_pred_grid_logi) 
matrix_3 = confusion_matrix(y_test, y_pred_grid_nb) df_1 = pd.DataFrame(matrix_1,index = ['setosa','versicolor','virginica'], columns = ['setosa','versicolor','virginica'])df_2 = pd.DataFrame(matrix_2,index = ['setosa','versicolor','virginica'], columns = ['setosa','versicolor','virginica'])df_3 = pd.DataFrame(matrix_3,index = ['setosa','versicolor','virginica'], columns = ['setosa','versicolor','virginica'])
plt.figure(figsize=(20,5))
plt.subplots_adjust(hspace = .25)
plt.subplot(1,3,1)
plt.title('confusion_matrix(KNN)')
sns.heatmap(df_1, annot=True,cmap='Blues')
plt.subplot(1,3,2)
plt.title('confusion_matrix(logistic regression)')
sns.heatmap(df_2, annot=True,cmap='Greens')
plt.subplot(1,3,3)
plt.title('confusion_matrix(naive_bayes)')
sns.heatmap(df_3, annot=True,cmap='Reds')
plt.show()

在这里插入图片描述


绘制ROC&PR曲线(无敌)

在kaggle 偶然看到的,这也太好看了,用到了yellowbrick这个库

from yellowbrick.classifier import PrecisionRecallCurve, ROCAUC, ConfusionMatrix
from yellowbrick.style import set_palette
from yellowbrick.cluster import KElbowVisualizer
from yellowbrick.model_selection import LearningCurve, FeatureImportances
from yellowbrick.contrib.wrapper import wrap# --- LR Accuracy ---
LRAcc = accuracy_score(y_pred_grid_logi, y_test)
print('.:. Logistic Regression Accuracy:'+'\033[35m\033[1m {:.2f}%'.format(LRAcc*100)+' \033[0m.:.')# --- LR Classification Report ---
print('\033[35m\033[1m\n.: Classification Report'+'\033[0m')
print('*' * 25)
print(classification_report(y_test, y_pred_grid_logi))# --- Performance Evaluation ---
print('\033[35m\n\033[1m'+'.: Performance Evaluation'+'\033[0m')
print('*' * 26)
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize = (14, 12))#--- LR Confusion Matrix ---
logmatrix = ConfusionMatrix(logistic_grid_search, ax=ax1, cmap='RdPu', title='Logistic Regression Confusion Matrix')
logmatrix.fit(X_train, y_train)
logmatrix.score(X_test, y_test)
logmatrix.finalize()# --- LR ROC AUC ---
logrocauc = ROCAUC(logistic_grid_search, ax = ax2, title = 'Logistic Regression ROC AUC Plot')
logrocauc.fit(X_train, y_train)
logrocauc.score(X_test, y_test)
logrocauc.finalize()# --- LR Learning Curve ---
loglc = LearningCurve(logistic_grid_search, ax = ax3, title = 'Logistic Regression Learning Curve')
loglc.fit(X_train, y_train)
loglc.finalize()# --- LR Precision Recall Curve ---
logcurve = PrecisionRecallCurve(logistic_grid_search, ax = ax4, ap_score = True, iso_f1_curves = True, title = 'Logistic Regression Precision-Recall Curve')
logcurve.fit(X_train, y_train)
logcurve.score(X_test, y_test)
logcurve.finalize()plt.tight_layout();

在这里插入图片描述


相关内容

热门资讯

优酷安卓9.0系统版本,畅享流... 你有没有发现,最近你的优酷APP是不是有点不一样了?没错,就是那个我们每天离不开的追剧神器——优酷,...
安卓手机系统体验排名,揭秘最佳... 你有没有发现,现在手机市场上安卓手机的品牌和型号简直多到让人眼花缭乱?每个品牌都试图在系统体验上大显...
安卓操作系统技巧在哪,安卓操作... 你有没有发现,安卓手机用久了,总感觉有点慢吞吞的?别急,今天就来给你支几招,让你的安卓手机焕发第二春...
安卓手机哪个系统最快,揭秘最快... 你有没有想过,为什么你的安卓手机有时候会慢吞吞的,像是老牛拉破车一样?别急,今天就来给你揭秘安卓手机...
安卓非系统允许程序,探索安卓非... 你知道吗?在安卓手机的世界里,除了那些系统自带的程序,还有很多“外来客”在悄悄地占领着你的手机空间。...
qq飞车安卓系统和苹果系统,安... 你有没有发现,最近QQ飞车这款游戏在安卓系统和苹果系统上可是火得一塌糊涂啊!不管是走在街头,还是坐在...
安卓系统页面不显示时间,安卓系... 手机屏幕上那时间怎么突然消失了呢?是不是你也遇到了安卓系统页面不显示时间的问题?别急,今天就来给你详...
怎么修改安卓系统设备,揭秘安卓... 手机用久了是不是觉得卡得要命?别急,今天就来教你怎么修改安卓系统设备,让你的手机焕发第二春!一、清理...
安卓平板刷车载系统固件,体验智... 你有没有想过,你的安卓平板不仅能陪你追剧、玩游戏,还能变身成为车载系统的得力助手呢?没错,就是那种让...
安卓要不要系统更新系统,守护安... 亲爱的安卓用户们,你是不是也经常被手机弹出的系统更新通知搞得头都大了?是不是在犹豫,这更新到底要不要...
电视机安卓系统则,体验升级 你有没有发现,现在的电视机越来越智能了?尤其是那些搭载了安卓系统的电视机,简直就像是个小机器人,不仅...
安卓系统打开动画效果,打开动画... 你有没有发现,每次打开安卓手机,那瞬间闪现的动画效果,就像是一场视觉盛宴呢?今天,就让我带你一起探索...
安卓系统的诞生和发展,安卓系统... 你有没有想过,手机里的那个小小的操作系统,竟然能改变我们的生活呢?没错,我要说的就是安卓系统。它就像...
安卓系统电话通话录音,捕捉真实... 你有没有想过,在繁忙的生活中,有时候一个电话的录音就能帮你回忆起重要的信息或者关键时刻的对话内容呢?...
安卓64位系统官方下载,解锁全... 你有没有发现,最近你的安卓手机好像有点卡卡的呢?别急,别急,今天就来给你揭秘一下如何给你的安卓手机升...
安卓8系统可以吗,创新与变革的... 你有没有听说安卓8系统?最近这个话题在数码圈可是火得一塌糊涂呢!不少朋友都在问我:“安卓8系统可以吗...
安卓系统电量显示不正,揭秘原因... 手机电量显示不准确,是不是你也遇到了这样的烦恼?每次看着那忽上忽下的电量百分比,心里是不是直发慌?别...
安卓平板开票系统怎么用,轻松实... 你有没有想过,拥有一台安卓平板,不仅能随时随地办公学习,还能轻松搞定开票业务呢?没错,现在就让我来带...
安卓系统怎样下载尚德,安卓系统... 你有没有想过,想要在安卓系统上下载尚德,其实就像是在茫茫书海中找到一本宝藏呢?别急,让我来带你一步步...
安卓5系统自带相机软件,系统自... 你有没有发现,自从你升级到了安卓5系统,手机里的相机软件好像变得不一样了呢?没错,就是那个我们每天都...