Matplotlib 简单教程 4:坐标轴标题

25 阅读3分钟

1、坐标轴标题

1.1、面向对象:OO-style

👏

OO-style:参数 设置坐标轴标题格式

import warnings
warnings.filterwarnings("ignore") # 禁止显示不需要的警告

# (1)导入库
import matplotlib.pyplot as plt

# 替换sans-serif字体,正常显示中文(黑体)'SimHei''FangSong''Microsoft YaHei'
plt.rcParams['font.sans-serif']=['Microsoft YaHei']  
# 坐标显示负号
plt.rcParams['axes.unicode_minus'] = False  

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

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

# (4)设置 x 轴标签
ax.set_xlabel(
    "x 轴"
    , loc="left"            # left\right\center
    , font='Microsoft YaHei'# 标题字体
    , size=10               # 字体大小
    , color='gray'          # 字体颜色
    , fontweight='bold'     # 加粗
    , rotation=60           # 设置旋转角度
    )

# 设置 y 轴标签 
ax.set_ylabel(      
    "y 轴"
    , loc="center"          # bottom\center\top
    , font='Microsoft YaHei'# 标题字体
    , size=10               # 字体大小
    , color='gray'          # 字体颜色
    , fontweight='bold'     # 加粗
    , rotation=0            # 设置旋转角度
    )

plt.show()
image.png

👏

OO-style:参数字典 设置坐标轴标题格式

import warnings
warnings.filterwarnings("ignore") # 禁止显示不需要的警告

# (1)导入库
import matplotlib.pyplot as plt

# 替换sans-serif字体,正常显示中文(黑体)'SimHei''FangSong''Microsoft YaHei'
plt.rcParams['font.sans-serif']=['Microsoft YaHei']  
# 坐标显示负号
plt.rcParams['axes.unicode_minus'] = False  

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

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

# (4)设置参数字典:
format_dir = {
    'fontproperties': 'Microsoft YaHei' # 字体种类
    , 'fontsize': 10            # 字体大小
    , 'color': 'Gray'           # 字体颜色
    , 'fontweight': 'bold'      # 字体加粗
    }

# (5)设置 x 轴标签
ax.set_xlabel(
    "x 轴"
    , loc="left"            # left\right\center
    , rotation=60           # 设置旋转角度
    , **format_dir
    )

# 设置 y 轴标签 
ax.set_ylabel(      
    "y 轴"
    , loc="center"          # bottom\center\top
    , rotation=0            # 设置旋转角度
    , **format_dir
    )

plt.show()
image.png

1.2、pyplot 函数

👏

pyplot 函数:参数 设置坐标轴标题格式

import warnings
warnings.filterwarnings("ignore")  # 禁止显示不需要的警告

import matplotlib.pyplot as plt

# 替换sans-serif字体,正常显示中文(黑体)'SimHei''FangSong''Microsoft YaHei'
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
# 坐标显示负号
plt.rcParams['axes.unicode_minus'] = False

# (1)创建一个绘图窗体,并设置大小为 (4, 2)
plt.figure(figsize=(42))

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

# (3)设置 x 轴标签
plt.xlabel(
    "x 轴"
    , loc="left"             # left\right\center
    , font='Microsoft YaHei' # 标题字体
    , size=10                # 字体大小
    , color='gray'           # 字体颜色
    , fontweight='bold'      # 加粗
    , rotation=60           # 设置旋转角度
)

# (4)设置 y 轴标签
plt.ylabel(
    "y 轴"
    , loc="center"           # bottom\center\top
    , font='Microsoft YaHei' # 标题字体
    , size=10                # 字体大小
    , color='gray'           # 字体颜色
    , fontweight='bold'      # 加粗
    , rotation=0             # 设置旋转角度
)

plt.show()
image.png

👏

pyplot 函数:参数字典 设置坐标轴标题格式

import warnings
warnings.filterwarnings("ignore")  # 禁止显示不需要的警告

import matplotlib.pyplot as plt

# 替换sans-serif字体,正常显示中文(黑体)'SimHei''FangSong''Microsoft YaHei'
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
# 坐标显示负号
plt.rcParams['axes.unicode_minus'] = False

# (1)创建一个绘图窗体,并设置大小为 (4, 2)
plt.figure(figsize=(42))

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

# (3)设置参数字典:
format_dir = {
    'fontproperties': 'Microsoft YaHei' # 字体种类
    , 'fontsize': 10            # 字体大小
    , 'color': 'Gray'           # 字体颜色
    , 'fontweight': 'bold'      # 字体加粗
    }

# (4)设置 x 轴标签
plt.xlabel(
    "x 轴"
    , loc="left"             # left\right\center
    , rotation=60           # 设置旋转角度
    , **format_dir
)

# (5)设置 y 轴标签
plt.ylabel(
    "y 轴"
    , loc="center"           # bottom\center\top
    , rotation=0           # 设置旋转角度
    , **format_dir
)

plt.show()
image.png

👏

总结:以上介绍了 python matplotlib 库,坐标轴标题设置。

Python 端到端的机器学习
AI入门:详细介绍机器学习建模过程,步骤细节;以及人工智能的分阶段学习线路图。
🚀 点击查看
SQL + Pandas 练习题
SQL 练习题目,使用 Pandas 库实现,使用 Sqlalchemy 库查看 SQL 代码血缘关系。
🚀 点击查看
Python 数据可视化
介绍了有关 Matplotlib,Seaborn,Plotly 几个 Python 绘图库的简单使用。
🚀 点击查看