文章目录
说明
本文运行环境
- macOS 10.13.6
- python 3.7
- Matplotlib 3.0.2
Matplotlib
安装
conda install -c conda-forge matplotlib
引用
import numpy as np
import matplotlib.pyplot as plt
设置notebook
%matplotlib inline
绘制正弦函数
x = np.linspace(0,10,200)
y = np.sin(x)
plt.plot(x,y)
output:
在正弦函数上再绘制余弦函数
plt.plot(x,np.sin(x))
plt.plot(x,np.cos(x))
output:
设置绘图的样式为虚线
plt.plot(x,y,'--')
output:
设置绘图的样式为圆点
x = np.linspace(0,10,100)
y = np.sin(x)
plt.plot(x,y,'o')
output:
设置绘图的样式为六边形
x = np.linspace(0,10,100)
y = np.sin(x)
plt.plot(x,y,'p')
output:
将绘制图片存储
figure = plt.figure()
plt.plot(x,y,'--')
figure.savefig("/Users/No_notion/Desktop/sinx.png")
#output
一张图片存储到Destop上
同时打印多张图片
#subplot同时打印多张图片,传入三个参数
#第一个参数,代表几行
#第二个参数,代表几列
#第三个参数,代表位置的索引
plt.subplot(3,3,1)
plt.plot(x,np.sin(x),'--')
plt.subplot(3,3,2)
plt.plot(x,np.cos(x),'--')
plt.subplot(3,3,3)
plt.plot(x,np.cos(x),'--')
plt.subplot(3,3,4)
plt.plot(x,np.cos(x),'--')
plt.subplot(3,3,5)
plt.plot(x,np.cos(x),'--')
plt.subplot(3,3,6)
plt.plot(x,np.cos(x),'--')
output:
设置绘制的颜色为红色
x = np.linspace(0,10,50)
y = np.sin(x)
plt.plot(x,y,'o',color='red')
output:
颜色值支持十六进制字符串
设置标签
x = np.linspace(0,10,100)
plt.plot(x,np.sin(x),label='sinx(x)')
plt.plot(x,np.cos(x),'--',label='cos(x)')
plt.legend()
output:
|
将标签设置在右上角
x = np.linspace(0,10,100)
plt.plot(x,np.sin(x),label='sinx(x)')
plt.plot(x,np.cos(x),'--',label='cos(x)')
plt.legend(loc='upper right')
output:
标签位置列表
位置字符串 | 位置码 | 描述 |
---|---|---|
‘best’ | 0 | 自动 |
‘upper right’ | 1 | 右上 |
‘upper left’ | 2 | 左上 |
‘lower left’ | 3 | 左下 |
‘lower right’ | 4 | 右下 |
‘right’ | 5 | 右 |
‘center left’ | 6 | 中左 |
‘center right’ | 7 | 中右 |
‘lower center’ | 8 | 中下 |
‘upper center’ | 9 | 中上 |
‘center’ | 10 | 中心 |
设置标记点大小
plt.subplot(2,2,1)
plt.plot(x,np.sin(x),'-p',color='red',markersize=3)
plt.subplot(2,2,2)
plt.plot(x,np.cos(x),'-p',color='green',markersize=5)
plt.subplot(2,2,3)
plt.plot(x,np.cos(x),'-p',color='orange',markersize=8)
plt.subplot(2,2,4)
plt.plot(x,np.cos(x),'-p',color='pink',markersize=10)
output:
设置标记点边线颜色
plt.plot(x,np.sin(x),'-p',color='red',markersize=3,markeredgecolor='black')
output:
设置绘制线条的粗细
plt.plot(x,np.cos(x),'-p',color='red',linewidth=3)
output:
设置y轴的限制条件
plt.plot(x,np.cos(x),'-p',color='red',linewidth=3)
#第一个参数为最小值
#第二个参数为最大值
plt.ylim(-0.5,0.5)
output:
设置x轴的限制条件
plt.plot(x,np.cos(x),'-p',color='red',linewidth=3)
plt.ylim(-1.0,0.5)
plt.xlim(2,8)
output:
散点图
#设置x
x = np.random.randn(100)
#设置y
y = np.random.randn(100)
#设置color
colors = np.random.randn(100)
#设置sizes
sizes = np.random.rand(100)*1000
plt.scatter(x=x,y=y,s=sizes,c=colors)
output:
散点图其他属性
x = np.random.randn(100)
y = np.random.randn(100)
colors = np.random.randn(100)
sizes = np.random.rand(100)*1000
#设置alpha
alpha = 0.4
plt.scatter(x=x,y=y,s=sizes,c=colors,alpha = alpha)
#设置colorBar
plt.colorbar()
output:
设置绘制风格
plt.style.use('classic')
x = np.random.randn(100)
y = np.random.randn(100)
colors = np.random.randn(100)
sizes = np.random.rand(100)*1000
#设置alpha
alpha = 0.4
plt.scatter(x=x,y=y,s=sizes,c=colors,alpha = alpha)
#设置colorBar
plt.colorbar()
output:
相关链接
matplotlib Documentation
matplotlib tutorial from tutorialspoint