【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();

在这里插入图片描述


相关内容

热门资讯

智能安卓电视系统卡,智能安卓电... 你有没有遇到过这种情况?家里的智能安卓电视系统突然卡住了,屏幕上那个熟悉的界面就像被施了魔法一样,怎...
电脑虚拟安卓系统教程,教程全解... 你有没有想过,让你的电脑也能像手机一样,随时随地玩各种安卓应用?没错,这就是今天我要跟你分享的神奇魔...
qq飞车分安卓系统,QQ飞车安... 你有没有发现,最近QQ飞车这款游戏在安卓系统上可是火得一塌糊涂啊!不管是街头巷尾,还是朋友圈里,都能...
淘手游苹果系统安卓系统,苹果系... 你有没有发现,现在手机游戏越来越火了?不管是走在街头,还是坐在家里,总能看到大家拿着手机,眼睛一眨不...
安卓系统定位app华为,守护您... 你有没有发现,现在手机里的APP真是五花八门,各有各的用处。今天,咱们就来聊聊安卓系统里一个特别实用...
安卓系统显示矫准,打造清晰视觉... 你有没有发现,你的安卓手机屏幕有时候显示得有点歪歪扭扭的?别急,这可不是什么大问题,今天就来给你详细...
安卓系统服务有病毒,病毒生成背... 你知道吗?最近在安卓系统上,服务里竟然悄悄潜入了病毒!这可不是闹着玩的,得赶紧来聊聊这个事儿,让你了...
解决ios系统和安卓系统游戏,... 你是不是也和我一样,手机里装了各种游戏,却因为iOS和安卓系统不兼容而头疼不已?别急,今天就来给你支...
安卓系统浮窗app,便捷多任务... 你有没有发现,手机上的那些小窗口,就像魔法一样,让我们的使用体验瞬间升级?没错,说的就是安卓系统里的...
安卓手工刷谷歌系统,体验原生魅... 你有没有想过,你的安卓手机其实可以焕发第二春呢?没错,就是通过手工刷谷歌系统,让你的手机体验焕然一新...
调整安卓系统时间流速,揭秘安卓... 你有没有发现,时间有时候就像那调皮的小精灵,在我们不经意间溜走?有时候,我们希望时间能慢一些,让生活...
网易云游戏安卓系统,解锁全新游... 亲爱的游戏迷们,你是不是也和我一样,对手机游戏情有独钟?今天,我要和你聊聊一个特别酷的话题——网易云...
安卓系统那个优化最好,探索最佳... 你有没有发现,手机里的安卓系统就像是个调皮的小家伙,总是时不时地给你点小麻烦?不过别担心,今天咱们就...
安卓手机安装windous系统... 你有没有想过,你的安卓手机也能装上Windows系统?是的,你没听错,就是那个曾经陪伴我们无数个日夜...
华为手机适合安卓系统,安卓生态... 你有没有发现,最近华为手机在安卓系统圈子里可是风头无两呢?这不,我就来给你好好捋一捋,为什么华为手机...
安卓系统下载福建助学,安卓系统... 你有没有听说最近安卓系统上有个超级棒的福建助学项目?没错,就是那个能让你轻松下载各种学习资源的神器!...
i7安卓系统,引领智能科技新潮... 你有没有想过,手机和电脑的结合体是什么样的呢?想象一个既能流畅运行大型游戏,又能轻松处理日常办公的设...
安卓改鸿蒙系统安装,系统升级安... 你有没有想过给你的安卓手机换换口味呢?没错,就是那种焕然一新的感觉!今天,就让我来带你一起探索如何将...
安卓原生系统美化软件,个性化美... 你有没有发现,安卓手机用久了,界面总是有点单调乏味呢?别急,今天就来给你安利几款超好用的安卓原生系统...
安卓系统图案解锁方法,安卓系统... 手机解锁,这可是每天都要经历的小环节,是不是觉得有点儿单调呢?今天,就让我来带你一起探索一下安卓系统...