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

在这里插入图片描述


相关内容

热门资讯

安卓如何操控苹果系统,揭秘跨平... 你知道吗?在这个科技飞速发展的时代,安卓和苹果两大操作系统之间的较量可是从未停歇。虽然它们各自有着忠...
安卓系统账户同步数据,畅享无缝... 你有没有遇到过这种情况:手机里存了那么多宝贝照片、重要文件,结果换了个新手机,却发现那些宝贝全都不翼...
安卓系统不停推送广告,安卓系统... 你有没有发现,最近你的安卓手机是不是越来越“热情”了?没错,就是那个不停在你屏幕上跳来跳去的广告!今...
airpods可以和安卓系统,... 你有没有想过,那些炫酷的AirPods竟然也能和安卓手机完美搭配?没错,就是那个我们平时只听说和iP...
安卓系统实体键盘不对,创新与挑... 你是不是也遇到了这个问题?安卓手机的实体键盘突然不对劲了,按下去没反应,或者反应迟钝,简直让人抓狂!...
汽车导航改装安卓系统,安卓系统... 你有没有想过,你的汽车导航系统是不是已经out了?现在,让我来给你揭秘如何给你的爱车来一次科技大变身...
安卓系统如何限制下载,安卓系统... 你有没有发现,手机里的安卓系统越来越智能了?不过,这也意味着有时候我们不小心就会下载一些不想要的软件...
安卓系统调成日语,概要の副標題... 你有没有想过,你的安卓手机竟然可以变成一个日式小天地呢?没错,就是那种动漫里常见的日语界面,是不是听...
男生耳机推荐安卓系统,男生耳机... 耳机可是现代生活中不可或缺的小玩意儿,尤其是对于喜欢听音乐的男生来说,一副好耳机简直就是灵魂的伴侣。...
安卓同版本升级系统,功能优化与... 你知道吗?最近手机界可是热闹非凡呢!各大品牌纷纷推出了安卓同版本升级系统,让我们的手机焕然一新。今天...
安卓更换别的手机系统,轻松切换... 你有没有想过,你的安卓手机用久了,是不是有点审美疲劳了呢?或者,你最近是不是对其他手机系统产生了浓厚...
安卓系统单机神雕侠侣,指尖重温 你有没有想过,在手机上也能体验一把江湖恩怨、侠骨柔肠?没错,就是那个让人心驰神往的《神雕侠侣》!今天...
安卓系统键盘语言切换,安卓系统... 你有没有发现,手机上的安卓系统键盘语言切换功能,简直就像是个神奇的魔法棒,轻轻一点,就能让文字飞舞在...
oppok1安卓系统,性能与体... 你有没有发现,最近手机圈里又掀起了一股热潮?没错,就是OPPO K1这款新机!这款手机不仅外观时尚,...
安卓系统环境的搭建,从零开始构... 想要在电脑上体验安卓系统的魅力,是不是已经跃跃欲试了呢?别急,今天就来手把手教你如何搭建一个属于自己...
【MySQL】锁 锁 文章目录锁全局锁表级锁表锁元数据锁(MDL)意向锁AUTO-INC锁...
【内网安全】 隧道搭建穿透上线... 文章目录内网穿透-Ngrok-入门-上线1、服务端配置:2、客户端连接服务端ÿ...
GCN的几种模型复现笔记 引言 本篇笔记紧接上文,主要是上一篇看写了快2w字,再去接入代码感觉有点...
数据分页展示逻辑 import java.util.Arrays;import java.util.List;impo...
Redis为什么选择单线程?R... 目录专栏导读一、Redis版本迭代二、Redis4.0之前为什么一直采用单线程?三、R...