2019年12月20日
一.安装
1.官网
https://matplotlib.org/
2.命令(大概用了40分钟)
pip install matplotlib

3.mac端下载SimHeiFont中文字体 .ttf文件(解决中文显示问题)
https://www.fontpalace.com/font-download/SimHei/
3.1查找字体安装路径
import matplotlib
matplotlib.matplotlib_fname()
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/mpl-data/matplotlibrc'
类推下找到font目录我的是如下
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/mpl-data/fonts/ttf

将下载的ttf文件 复制进去即可。
3.2 用如下命令roload下就生效了
>>> from matplotlib.font_manager import _rebuild
>>> _rebuild() #reload一下

二.图表
1.绘制折线图
# coding=utf-8
import matplotlib.pyplot as plt
# 设置中文字体
plt.rcParams['font.family'] = ['SimHei']
x = [5, 4, 2, 1] # x轴坐标数据
y = [7, 8, 9, 10] # y轴坐标数据
# 绘制线段
plt.plot(x, y, 'b', label='线1', linewidth=2)
plt.title('绘制折线图') # 添加图表标题
plt.ylabel('y轴') # 添加y轴标题
plt.xlabel('x轴') # 添加x轴标题
plt.legend() # 设置图例
# 以分辨率 72 来保存图片
plt.savefig('折线图', dpi=72)
plt.show() # 显示图形

2.柱状图

# coding=utf-8
import matplotlib.pyplot as plt
# 设置中文字体
plt.rcParams['font.family'] = ['SimHei']
x1 = [1, 3, 5, 7, 9] # x1轴坐标数据
y1 = [5, 2, 7, 8, 2] # y1轴坐标数据
x2 = [2, 4, 6, 8, 10] # x2轴坐标数据
y2 = [8, 6, 2, 5, 6] # y2轴坐标数据
# 绘制柱状图
plt.bar(x1, y1, label='柱状图1')
plt.bar(x2, y2, label='柱状图2')
plt.title('绘制柱状图') # 添加图表标题
plt.ylabel('y轴') # 添加y轴标题
plt.xlabel('x轴') # 添加x轴标题
plt.legend() # 设置图例
plt.show() # 显示图形
3.绘制饼状图

# coding=utf-8
import matplotlib.pyplot as plt
# 设置中文字体
plt.rcParams['font.family'] = ['SimHei']
# 各种活动标题列表
activies = ['工作', '睡', '吃', '玩']
# 各种活动所占时间列表
slices = [8, 7, 3, 6]
# 各种活动在饼状图中的颜色列表
cols = ['c', 'm', 'r', 'b']
plt.pie(slices, labels=activies, colors=cols,
shadow=True, explode=(0, 0.1, 0, 0), autopct='%.1f%%')
plt.title('绘制饼状图')
plt.show() # 显示图形
4.绘制散点图 (用于科学计算)

# coding=utf-8
import matplotlib.pyplot as plt
import numpy as np
# 设置中文字体
plt.rcParams['font.family'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
n = 1024
x = np.random.normal(0, 1, n)
y = np.random.normal(0, 1, n)
plt.scatter(x, y)
plt.title('绘制散点图')
plt.show() # 显示图形
5.绘制子图标 (大图表里面有多个字图表)


# coding=utf-8
import matplotlib.pyplot as plt
import numpy as np
# 设置中文字体
plt.rcParams['font.family'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
def drowsubbar():
"""绘制柱状图"""
x1 = [1, 3, 5, 7, 9] # x1轴坐标数据
y1 = [5, 2, 7, 8, 2] # y1轴坐标数据
x2 = [2, 4, 6, 8, 10] # x2轴坐标数据
y2 = [8, 6, 2, 5, 6] # y2轴坐标数据
# 绘制柱状图
plt.bar(x1, y1, label='柱状图1')
plt.bar(x2, y2, label='柱状图2')
plt.title('绘制柱状图') # 添加图表标题
plt.ylabel('y轴') # 添加y轴标题
plt.xlabel('x轴') # 添加x轴标题
def drowsubpie():
"""绘制饼状图"""
# 各种活动标题列表
activies = ['工作', '睡', '吃', '玩']
# 各种活动所占时间列表
slices = [8, 7, 3, 6]
# 各种活动在饼状图中的颜色列表
cols = ['c', 'm', 'r', 'b']
plt.pie(slices, labels=activies, colors=cols,
shadow=True, explode=(0, 0.1, 0, 0), autopct='%.1f%%')
plt.title('绘制饼状图')
def drowsubline():
"""绘制折线图"""
x = [5, 4, 2, 1] # x轴坐标数据
y = [7, 8, 9, 10] # y轴坐标数据
# 绘制线段
plt.plot(x, y, 'b', label='线1', linewidth=2)
plt.title('绘制折线图') # 添加图表标题
plt.ylabel('y轴') # 添加y轴标题
plt.xlabel('x轴') # 添加x轴标题
plt.legend() # 设置图例
def drowssubscatter():
"""绘制散点图"""
n = 1024
x = np.random.normal(0, 1, n)
y = np.random.normal(0, 1, n)
plt.scatter(x, y)
plt.title('绘制散点图')
plt.subplot(2, 2, 1) # 替换(221)
drowsubbar()
plt.subplot(2, 2, 2) # 替换(222)
drowsubpie()
plt.subplot(2, 2, 3) # 替换(223)
drowsubline()
plt.subplot(2, 2, 4) # 替换(224)
drowssubscatter()
plt.tight_layout() # 调整布局
plt.show() # 显示图形
如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。
网友评论