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 库操作,请参考:可视化-文档
相关推荐
Aevget10 小时前
可视化工具LightningChart JS v8.1 重磅更新:热力图与 3D 可视化能力双提升!
javascript·3d·信息可视化·数据可视化·lightningchart
Serendipity_Carl1 天前
数据可视化实战之链家
python·数据可视化·数据清洗
imbackneverdie1 天前
国自然申报技术路线图模板
图像处理·人工智能·信息可视化·数据可视化·学术·国自然·国家自然科学基金
hdsoft_huge2 天前
在天地图中使用不同格式高效加载 PostGIS 的方案
arcgis·postgresql·数据可视化
阿达_优阅达2 天前
Tableau 2025.3 发布!可视化扩展升级、Server 版 Agent、平台数据 API,让 AI 深度融入业务工作流
人工智能·ai·数据分析·数据可视化·仪表板·tableau·版本更新
希艾席帝恩2 天前
数字孪生如何重塑现代制造体系?
大数据·人工智能·数字孪生·数据可视化·数字化转型
Pyeako2 天前
Python数据可视化--matplotlib库
python·matplotlib·数据可视化·画图·pylab
数据科学项目实践3 天前
建模步骤 3 :数据探索(EDA) — 1、初步了解数据:常用函数
人工智能·python·机器学习·数据挖掘·数据分析·pandas·数据可视化
AI小云3 天前
【数据操作与可视化】Serborn绘图-类别散点图和热力图
python·数据可视化
数据科学项目实践5 天前
建模步骤 3 :数据探索(EDA) — 1、初步了解数据:自定义函数
大数据·人工智能·python·机器学习·matplotlib·数据可视化