Matplotlib 简单教程 3:坐标轴\边框设置

1、关闭坐标轴\边框

👏 python 的 matplotlib 库,关闭坐标轴\边框,如下:

1.1、面向对象:OO-style

👏 关闭全部边框\坐标轴,如下:

ini 复制代码
# 导入库
import matplotlib.pyplot as plt

# 创建图像窗体,子图对象
fig, ax = plt.subplots(figsize=(3,3))

# 绘制折线图
ax.plot([1, 2, 3, 4], [8, 4, 2, 3])

# 关闭所有边框\坐标轴
ax.axis('off')

# 显示图形:
plt.show()

👏 关闭部分边框\坐标轴,如下:

scss 复制代码
# 导入库
import matplotlib.pyplot as plt

# 创建图像窗体,子图对象
fig, ax = plt.subplots(figsize=(3,3))

# 绘制折线图
ax.plot([1, 2, 3, 4], [8, 4, 2, 3])

# 关闭最上面,右边坐边框
# top\bottom\left\right
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

# 显示图形:
plt.show()

1.2、pyplot 函数

👏 关闭全部边框\坐标轴,如下:

ini 复制代码
# 导入库
import matplotlib.pyplot as plt

# 创建图像窗体
plt.figure(figsize=(3, 3))

# 绘制折线图
plt.plot([1, 2, 3, 4], [8, 4, 2, 3])

# 关闭所有坐标轴\边框
plt.axis('off') 

# 显示图形:
plt.show()                  

👏 关闭部分边框\坐标轴,如下:

scss 复制代码
# 导入库
import matplotlib.pyplot as plt

# 创建图像窗体
plt.figure(figsize=(3, 3))

# 绘制折线图
plt.plot([1, 2, 3, 4], [8, 4, 2, 3])

# 关闭左边,下边坐标轴\边框
# top\bottom\left\right
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['right'].set_visible(False)

# plt.gca()获取当前坐标轴对象

# 显示图形:
plt.show()

2、坐标轴\边框颜色

👏 python 的 matplotlib 库,设置坐标轴\边框颜色,如下:

  • 将坐标轴\边框颜色设置为白色,同样可以起到隐藏效果

2.1、面向对象:OO-style

scss 复制代码
# 导入库
import matplotlib.pyplot as plt

# 创建图像窗体,子图对象
fig, ax = plt.subplots(figsize=(3,3))

# 绘制折线图
ax.plot([1, 2, 3, 4], [8, 4, 2, 3])

# 设置边框颜色
# top\bottom\left\right
ax.spines['bottom'].set_color('red')
ax.spines['top'].set_color('white')
ax.spines['right'].set_color('white')

# 显示图形:
plt.show()

2.1、面向对象:OO-style

scss 复制代码
# 导入库
import matplotlib.pyplot as plt

# 创建图像窗体
plt.figure(figsize=(3, 3))

# 绘制折线图
plt.plot([1, 2, 3, 4], [8, 4, 2, 3])

# 设置边框颜色
# top\bottom\left\right
plt.gca().spines['bottom'].set_color('red')
plt.gca().spines['top'].set_color('white')
plt.gca().spines['right'].set_color('white')

# 显示图形:
plt.show()

3、坐标轴\边框粗细

👏 python 的 matplotlib 库,设置坐标轴\边框粗细,如下:

3.1、面向对象:OO-style

scss 复制代码
# 导入库
import matplotlib.pyplot as plt

# 创建图像窗体,子图对象
fig, ax = plt.subplots(figsize=(3,3))

# 绘制折线图
ax.plot([1, 2, 3, 4], [8, 4, 2, 3])

# 设置边框粗细
# top\bottom\left\right
ax.spines['top'].set_linewidth(1)
ax.spines['bottom'].set_linewidth(3)
ax.spines['left'].set_linewidth(3)
ax.spines['right'].set_linewidth(4)

# 显示图形:
plt.show()

3.2、pyplot 函数

scss 复制代码
# 导入库
import matplotlib.pyplot as plt

# 创建图像窗体
plt.figure(figsize=(3, 3))

# 绘制折线图
plt.plot([1, 2, 3, 4], [8, 4, 2, 3])

# 设置边框粗细
# top\bottom\left\right
plt.gca().spines['top'].set_linewidth(1)
plt.gca().spines['bottom'].set_linewidth(3)
plt.gca().spines['left'].set_linewidth(3)
plt.gca().spines['right'].set_linewidth(4)

# 显示图形:
plt.show()

4、坐标轴\边框位置

👏 python 的 matplotlib 库,设置坐标轴\边框位置,如下:

👏 设置坐标轴边框位置有三种种方式:

  • 1、("axes", 0.5):

    • "axes"表示相对于坐标轴定位。

    • 0.5是一个比例值,表示将右侧边框定位在相对于坐标轴的 0.5 位置处,即坐标轴范围的中间位置。

  • 2、("data", 0):

    • "data"表示相对于数据定位。

    • 0表示将右侧边框定位在数据值为 0 的位置处。

  • 3、("outward", 5):

    • "outward":表示向外移动边框。这个参数告诉 matplotlib 将边框向远离图表中心的方向移动。
    • -5:是一个数值,表示移动的距离。在这里,负数表示向坐标轴的负方向移动,也就是向左移动(对于垂直边框来说)。如果是正数,就会向坐标轴的正方向移动。

4.1、面向对象:OO-style

scss 复制代码
# 导入库
import matplotlib.pyplot as plt

# 创建图像窗体,子图对象
fig, ax = plt.subplots(figsize=(3,3))

# 绘制折线图
ax.plot([1, 2, 3, 4], [8, 4, 2, 3])

# 设置边框位置
# top\bottom\left\right
ax.spines["right"].set_position(("outward", -5))
ax.spines["left"].set_position(("data", 2))
ax.spines["top"].set_position(("axes", 0.1))

# 显示图形:
plt.show()

4.2、pyplot 函数

scss 复制代码
# 导入库
import matplotlib.pyplot as plt

# 创建图像窗体
plt.figure(figsize=(3, 3))

# 绘制折线图
plt.plot([1, 2, 3, 4], [8, 4, 2, 3])

# 设置边框位置
# top\bottom\left\right
plt.gca().spines["right"].set_position(("outward", -5))
plt.gca().spines["left"].set_position(("data", 2))
plt.gca().spines["top"].set_position(("axes", 0.1))

# 显示图形:
plt.show()

👏 plt.gca():获取当前坐标轴对象,可以先将坐标轴对象保存到变量中,再进行操作。如下

scss 复制代码
# 导入库
import matplotlib.pyplot as plt

# 创建图像窗体
plt.figure(figsize=(3, 3))

# 绘制折线图
plt.plot([1, 2, 3, 4], [8, 4, 2, 3])

# 设置边框位置
# top\bottom\left\right
plt_ax = plt.gca()

plt_ax.spines["right"].set_position(("outward", -5))
plt_ax.spines["left"].set_position(("data", 2))
plt_ax.spines["top"].set_position(("axes", 0.1))

# 显示图形:
plt.show()
  • 总结 :以上介绍了 python matplotlib 库,设置坐标轴\边框颜色、粗细、位置等操作。更多关于 matplotlib 库操作,请参考:可视化-文档
相关推荐
SL_staff3 小时前
BI不该只服务高管:JVS如何让仓库管理员自主配置库存预警
spring boot·数据分析·数据可视化
Q264336502310 小时前
【有源码】基于Hadoop+Spark的低能见度易雾站点识别与可视化分析系统-面向气象保障的低能见度监测数据挖掘与风险画像系统
大数据·hadoop·数据挖掘·spark·毕业设计·课程设计·数据可视化
天理智能科技19 小时前
物流仿真项目投入成本怎么算?制造企业收益测算思路
数据可视化
龙亘川20 小时前
AI 协同赋能城市治理:支撑政协数字化履职的技术路径探析
大数据·人工智能·智慧城市·开源软件·数据可视化
rememberme0011 天前
Windows Server 服务器监控部署文档(Prometheus + Grafana)
运维开发·数据可视化
龙亘川1 天前
城市运管服平台下综合办公数字化建设实践与思考
大数据·人工智能·智慧城市·开源软件·数据可视化
龙亘川1 天前
设备管理业务建模:从设备信息台账到维护计划执行闭环
大数据·人工智能·智慧城市·开源软件·数据可视化
数字新视界1 天前
国产信创动环监控系统的未来趋势分析
物联网·3d·数字孪生·三维可视化·数据可视化
慧都小妮子1 天前
Stimulsoft 2026.4 发布:Markdown 组件、桑基图与 .NET 11 支持
.net·数据可视化·stimulsoft·版本更新·报表控件·桑基图·.net 11
ASS-ASH2 天前
从力学与机械原理角度深度剖析:尊界V800刹车踏板支架断裂事件的技术逻辑
人工智能·python·数据可视化·机械工程·汽车安全·物理仿真·制动系统