Matplotlib | 手把手教你绘制官网神图

本文详细介绍了使用PythonMatplotlib进行高级绘图的过程,包括导入模块、设置图布和极坐标系、调整坐标轴、绘制曲线和散点、添加图例等步骤,并提供了代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大家好,我是 👉【Python当打之年(点击跳转)】

本期 手把手教你绘制官网神图 Matplotlib系列和Pyecharts系列都会不间断更新,希望对大家有所帮助,如有疑问或者需要改进的地方可以联系小编。

Matplotlib高阶绘图系列:
Matplotlib | 高阶绘图案例【3】
Matplotlib | 高阶绘图案例【2】
Matplotlib | 高阶绘图案例【1】


示例图(部分调整):

在这里插入图片描述

🏳️‍🌈 1. 导入模块

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from matplotlib.patheffects import withStroke
from matplotlib.ticker import AutoMinorLocator, MultipleLocator

🏳️‍🌈 2. 绘图

2.1 绘制图布,设置极坐标系

# 绘图
fig = plt.figure(figsize=(8,8),facecolor='#ECEFF1')
ax = fig.add_axes([0.2, 0.17, 0.68, 0.7])
# 设置x、y轴范围
ax.set_xlim(0, 4)
ax.set_ylim(0, 4)

在这里插入图片描述

2.2 设置主、次刻度

# 设置坐标轴主刻度
ax.xaxis.set_major_locator(MultipleLocator(1))
ax.yaxis.set_major_locator(MultipleLocator(1))

# 设置坐标轴次刻度
ax.xaxis.set_minor_locator(AutoMinorLocator(4))
ax.yaxis.set_minor_locator(AutoMinorLocator(4))

在这里插入图片描述

2.3 设置次刻度文本格式

# 设置坐标轴次刻度文本格式
ax.xaxis.set_minor_formatter("{x:.2f}")
ax.yaxis.set_minor_formatter("{x:.2f}%")

在这里插入图片描述

2.4 坐标轴标签替换、修改刻度线

# 坐标轴标签替换
ax.set_yticks(np.linspace(0, 4, 5)) 
ax.set_yticklabels(list('ABCDE'))

# 设置刻度线样式
ax.tick_params(which='major', width=1, length=10, labelsize=14)
ax.tick_params(which='minor', width=1.0, length=5, labelsize=10, labelcolor='0.25')

在这里插入图片描述

2.5 绘制曲线、栅格

# 数据
X = np.linspace(0.5, 3.5, 100)
Y1 = 3+np.cos(X)
Y2 = 1+np.cos(1+X/0.75)/2
Y3 = np.random.uniform(Y1, Y2, len(X))
ax.plot(X, Y1, c='C0', lw=2.5, label="Blue signal", zorder=10)
ax.plot(X, Y2, c='C1', lw=2.5, label="Orange signal")

# 设置栅格
ax.grid(linestyle="--", linewidth=0.5, color='.25', zorder=-10)

在这里插入图片描述

2.6 添加散点

ax.scatter(X[::3], Y3[::3],marker = 'o',color="w",edgecolors='black')

在这里插入图片描述

2.7 设置标题、标签、图例

# 设置标题
ax.set_title("Anatomy of a figure", fontsize=20, verticalalignment='bottom')

# 设置标签
ax.set_xlabel("x Axis label", fontsize=14)
ax.set_ylabel("y Axis label", fontsize=14)

# 设置图例
ax.legend(loc="upper right", fontsize=14)
royal_blue = [0, 20/256, 112/256]
bbox=dict(boxstyle="round", fc="w", ec="w")

在这里插入图片描述

2.8 绘制标注

# 绘制标注
def annotate(x, y, text):
    c = Circle((x, y), radius=0.1, clip_on=False, zorder=10, linewidth=2.5,
               edgecolor=royal_blue + [0.6], facecolor='none',
               path_effects=[withStroke(linewidth=7, foreground='white')])
    ax.add_artist(c)
    ax.text(x, y-0.25, text, style='italic', fontfamily='Courier New', ha='center', va='top', weight='bold',color=royal_blue,bbox=bbox)

annotate(3.8, -0.13, "Minor tick label\nax.xaxis.set_minor_formatter")
annotate(-0.03, 1.0, "Major tick\nax.yaxis.set_major_locator")
annotate(0.00, 3.75, "Minor tick\nax.yaxis.set_minor_locator")
annotate(-0.15, 3.00, "Major tick label\nax.yaxis.set_major_formatter")
annotate(1.68, -0.39, "xlabel\nax.set_xlabel")
annotate(-0.49, 1.67, "ylabel\nax.set_ylabel")
annotate(1.62, 4.15, "Title\nax.set_title")
annotate(1.75, 2.80, "Line\nax.plot")
annotate(2.25, 1.54, "Markers\nax.scatter")
annotate(3.00, 3.00, "Grid\nax.grid")
annotate(3.60, 3.58, "Legend\nax.legend")
annotate(2.5, 0.55, "Axes\nfig.subplots")
annotate(4.6, 4.5, "Figure\nplt.figure")
annotate(0.65, 0.01, "x Axis\nax.xaxis")
annotate(0, 0.36, "y Axis\nax.yaxis")
annotate(4.0, 0.7, "Spine\nax.spines")

在这里插入图片描述

2.9 设置边框

# 设置边框
fig.patch.set(linewidth=4, edgecolor='0.5')

在这里插入图片描述

🏳️‍🌈 3. 可视化项目源码+数据

点击跳转:【全部可视化项目源码+数据】


以上就是本期为大家整理的全部内容了,赶快练习起来吧,原创不易,喜欢的朋友可以点赞、收藏也可以分享注明出处)让更多人知道。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python当打之年

您的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值