【学习笔记】Python 使用 matplotlib 画图

本文将介绍如何使用 Python 的 matplotlib 库画图,记录一些常用的画图 demo 代码

安装

# 建议先切换到虚拟环境中
pip install matplotlib

中文显示

新版的 matplotlib 已经支持字体回退功能,因此可以直接设置字体为 Times New RomanSimSun(宋体)。这样英文会以 Times New Roman 显示,中文会以 宋体 显示

import matplotlib.pyplot as plt

plt.rcParams['font.family'] = ['Times New Roman','SimSun']

折线图、点线图

plot 即可以绘制折线图,也可以绘制点线图,通过 marker 参数设置点的样式,markersize 设置点的大小

import matplotlib.pyplot as plt

# 设置中文字体为 Times New Roman 和 宋体
plt.rcParams['font.family'] = ['Times New Roman','SimSun']

# 生成数据
x = range(0, 6)
y = [i**3 for i in x]

# 绘制点线图
plt.plot(x, y, marker='o', markersize=6, label='y=x^3')

# 添加坐标轴标签
plt.xlabel('x')
plt.ylabel('y')

# 配置坐标轴范
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值