matplotlib 简单用法详解(转)

原文链接:https://blog.csdn.net/cheng9981/article/details/60583273/

 

import matplotlib.pyplot as plt
import numpy as np

# ipython 画图

# 简单线图 

# 生成测试数据 
x = np.linspace(-1, 1, 50)
y = np.cos(x)

# figure创建一个绘图对象 figsize 图片大小
plt.figure(figsize=(10, 5))

# 画图 
plt.plot(x, y)  # 默认

# 所有绘图对象
plt.show()

# 画多个子图

# 生成测试数据 
x = np.linspace(-3, 3, 50)
y = np.cos(x)
y1 = 2 * x + 1
y2 = np.sin(x)

# figure创建一个绘图对象 figsize 图片大小
plt.figure(figsize=(10, 5))

plt.subplot(221)  # 第一行的左图
plt.plot(x, y)

plt.subplot(222)  # 第一行的右图
plt.plot(x, y1)

plt.subplot(212)  # 第二整行
plt.plot(x, y2)

# 所有绘图对象
plt.show()

# 显示标题和X、Y轴的文字和范围

# 生成测试数据 
x = np.linspace(-1, 1, 50)
y = np.cos(x)

# figure创建一个绘图对象 figsize 图片大小
plt.figure(figsize=(10, 5))

# 画图 
plt.plot(x, y)  # 默认

plt.xlabel("x label")  # X轴的文字
plt.ylabel("y label")  # Y轴的文字
plt.title("this is a title")  # 图表的标题

plt.xlim(-1.2, 1.2)  # 设置Y轴的范围
plt.ylim(-1.2, 1.2)  # 设置Y轴的范围

# 所有绘图对象
plt.show()

# 设置X、Y轴座标转换成字符座标

# 生成测试数据 
x = np.linspace(-3, 3, 50)
y = np.cos(x)

# figure创建一个绘图对象 figsize 图片大小
plt.figure(figsize=(10, 5))

# 画图 
plt.plot(x, y)  # 默认

# 根据相应座标数 转换成字符座标
plt.xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi], ['$-\pi$', '$-\pi/2$', '$0$', '$\pi/2$', '$\pi$'])
plt.yticks([-1, 0, 1], ['-one', 'zero', '+one'])

# 所有绘图对象
plt.show()

# 在同一figure对象中画多条线图

# 生成测试数据 
x = np.linspace(-1, 1, 50)
y = np.cos(x)
y2 = np.sin(x)
# figure创建一个绘图对象 figsize 图片大小
plt.figure(figsize=(10, 5))

# 画图 
plt.plot(x, y)  # 默认
plt.plot(x, y2)  # 默认
# 所有绘图对象
plt.show()

# 带有图示的线图 

# 生成测试数据 
x = np.linspace(-1, 1, 50)
y = np.cos(x)
y2 = np.sin(x)
# label 在图示中显示 color 线的颜色 linewidth线的宽度 linestyle线的样式
plt.plot(x, y, label='$cos(x)$', color='red', linewidth=1.0, linestyle='--')  # 前后添加"$"符号 表示数学公式
plt.plot(x, y2, label='$sin(x)$')

# 显示图例 plt.plot()中label和颜色及线型
# legend 的位置参数 best/upper right/upper left/lower left/lower right/right/center left
# /center right/lower center/upper center/center
plt.legend(loc='best')

# 所有绘图对象
plt.show()

# 设置X、Y轴的显示隐藏和移动 字体背景的透明度

# 生成测试数据 
x = np.linspace(-1, 1, 50)
y = np.cos(x)
y2 = np.sin(x)
# label 在图示中显示 color 线的颜色 linewidth线的宽度 linestyle线的样式
plt.plot(x, y, label='$cos(x)$', color='red', linewidth=1.0, linestyle='--')  # 前后添加"$"符号 表示数学公式
plt.plot(x, y2, label='$sin(x)$')

# 先把右边和上边的边界设置为不可见
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')

# 然后把下边界和左边界移动到0点
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))

# 设置座标轴字体的透明度
ax = plt.gca()
for label in ax.get_xticklabels() + ax.get_yticklabels():
    label.set_fontsize(12)
    label.set_bbox(dict(facecolor='blue', edgecolor='none', alpha=0.7))  # facecolor 前面颜色  edgecolor边框线 alpha透明度

# 所有绘图对象
plt.show()

# 给X、Y轴的座标添加标注

# 生成测试数据 
x = np.linspace(0, np.pi, 20)
y = np.sin(x)

# label 在图示中显示 color 线的颜色 linewidth线的宽度 linestyle线的样式
plt.plot(x, y, label='$cos(x)$', color='red', linewidth=1.0, linestyle='--')  # 前后添加"$"符号 表示数学公式

# 添加标注
x0 = np.pi / 2
y0 = np.sin(x0)

plt.scatter(x0, y0, color='black')  # 显示一个点
plt.plot([x0, x0], [0, y0], 'b--')  # 在点到x轴画出垂直线
# 标注方法1
plt.annotate('y = sin(x)' % y0, xy=(x0, y0), xycoords='data', xytext=(+30, -30), textcoords='offset points',
             arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=-0.2'))
# 标注方法2
plt.text(x0 + 0.1, y0, 'this is a sin(x)line')

# 所有绘图对象
plt.show()

# 全部设置完成
# 复杂线形图
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
c, s = np.cos(x), np.sin(x)

# 这里`b-`是`color="blue",linestyle="-"`的简写
# `lw`=`linewidth` 的简写
fig = plt.figure(figsize=(8, 6), dpi=80)
plt.plot(x, c, 'b-', lw=1.5, label='cosine')
plt.plot(x, s, 'r-', lw=1, label='sine')

# 显示图例 plt.plot()中label和颜色及线型 loc设置位置
plt.legend(loc='upper left')
# legend 的位置参数
'''
    best
    upper right
    upper left
    lower left
    lower right
    right
    center left
    center right
    lower center
    upper center
    center
'''

plt.xlim(-3.2, 3.2)  # 设置Y轴的范围 plt.xlim(x.min()*1.1, x.max()*1.1)
plt.ylim(-1.2, 1.2)  # 设置Y轴的范围 plt.ylim(c.min()*1.1, s.max()*1.1)

# 根据相应座标数 转换成字符座标
plt.xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi], ['$-\pi$', '$-\pi/2$', '$0$', '$\pi/2$', '$\pi$'])
# plt.yticks([-1,0,1],['-one','zero','+one'])

# 设置座标轴的位置
# 先把右边和上边的边界设置为不可见
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# 然后把下边界和左边界移动到0点
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))

# 添加标注
x0 = np.pi / 2
y0 = np.sin(x0)
plt.scatter(x0, y0, color='black')  # 显示一个点
plt.plot([x0, x0], [0, y0], 'b--')  # 在点到x轴画出垂直线
# 标注方法1
plt.annotate('y = sin(x)' % y0, xy=(x0, y0), xycoords='data', xytext=(+30, -30), textcoords='offset points',
             arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=-0.2'))
# 标注方法2
plt.text(x0 + 0.1, y0, 'this is a sin(x)line')

# 设置座标轴字体的透明度
ax = plt.gca()
for label in ax.get_xticklabels() + ax.get_yticklabels():
    label.set_fontsize(12)
    label.set_bbox(dict(facecolor='white', edgecolor='none', alpha=0.7))

plt.show()

show1:

 

show2:

show3:

show4:


show5:

show 6:

show7:

show8:

show 9:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章