matplotlib 基础使用

matplotlib 基础使用


import matplotlib.pyplot as plt #引入库

fig = plt.figure()#建立空白图

fig = plt.figure(figsize=(8,6),dpi=100)#创建一个800*600像素、100dpi(每英寸100点)分辨率的图形

ax1 = plt.subplot(231) #建立一个包含多个子图的图(2表示多少行,3表示多少列,1表示位置索引(从1开始,所有4表示第二行的第一个))
plt.subplot(2, 2, 4)

plt.show()#显示图

ax1.axis([-1, 1, -1, 1])#x0,x1,y0,y1(就是开始和结束的值)
plt.axis([-1, 1, -1, 1])#参数为一个list组

plt.title('PIE') #标题
plt.xlabel("x axis caption") #x轴描述
plt.ylabel("y axis caption") #Y轴描述

plt.savefig("matplot_sample.jpg")#保存图到本地

plt.grid(True) # 显示网格;

plt.plot(t, s, 'r--o', label = 'sinx')#右上角的那个label的内容
plt.legend()# 显示右上角的那个label,即上面的label = 'sinx'
ax.legend(loc=2); # upper left corner位置可以设置
ax.legend(loc=0) # let matplotlib decide the optimal location
ax.legend(loc=1) # upper right corner
ax.legend(loc=2) # upper left corner
ax.legend(loc=3) # lower left corner
ax.legend(loc=4) # lower right corner

线与描点风格
linewidth 或是 lw 参数改变线宽。 linestyle 或是 ls 参数改变线的风格。
ax.plot(x, x+13, color="purple", lw=1, ls='-', marker='o', markersize=2)


#刻度上的样式是可以更改的
for label in axis.get_ticklabels():
label.set_color('red') # 设置每个刻度标签的颜色;
label.set_rotation(45) # 旋转45度;
label.set_fontsize(16) # 设置字体大小;


twinx() 共享x轴实例

双坐标轴
twinx 与 twiny 函数能设置双坐标轴:

对数刻度
set_xscale 与 set_yscale 设置刻度,参数选择 “log” :


图里有图的实现

import matplotlib.pyplot as plt
from pylab import *
x = linspace(0, 5, 10)
y = x ** 2

fig = plt.figure()

axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # left, bottom, width, height (range 0 to 1)
axes2 = fig.add_axes([0.2, 0.5, 0.4, 0.3]) # inset axes

axes.plot(x, y, 'r')

axes.set_xlabel('x')
axes.set_ylabel('y')
axes.set_title('title')

# insert
axes2.plot(y, x, 'g')
axes2.set_xlabel('y')
axes2.set_ylabel('x')
axes2.set_title('insert title');

plt.show()



画散点图

x = [1, 2, 3, 4, 5]
y = [2.3, 3.4, 1.2, 6.6, 7.0]

plt.scatter(x, y, color='r', marker='+') #
plt.show()


color为散点的颜色标志,常用color的表示如下:
'b' 蓝色
'g' 绿色
'r' 红色
'c' 青色
'm' 品红色
'y' 黄色
'k' 黑色
'w' 白色
有四种表示颜色的方式:
用全名
16进制,如:#FF00FF
灰度强度,如:‘0.7’

marker为散点的标记,标记风格有多种:
'-' 实线样式
'--' 短横线样式
'-.' 点划线样式
':' 虚线样式
'.' 点标记
',' 像素标记
'o' 圆标记
'v' 倒三角标记
'^' 正三角标记
'<' 左三角标记
'>' 右三角标记
'1' 下箭头标记
'2' 上箭头标记
'3' 左箭头标记
'4' 右箭头标记
's' 正方形标记
'p' 五边形标记
'*' 星形标记
'h' 六边形标记 1
'H' 六边形标记 2
'+' 加号标记
'x' X 标记
'D' 菱形标记
'd' 窄菱形标记
'|' 竖直线标记
'_' 水平线标记


折线图

plt.plot(x, y, color='r', linestyle='--')
plt.show()


参数linestyle,控制的是线型的格式:
- 实线
-- 短线
-. 短点相间线
: 虚点线


扇形图

y = [2.3, 3.4, 1.2, 6.6, 7.0]
plt.figure()
plt.pie(y)
plt.title('PIE')
plt.show()



柱状图bar

x = [1, 2, 3, 4, 5]
y = [2.3, 3.4, 1.2, 6.6, 7.0]
plt.figure()
plt.bar(x, y)
plt.title("bar")
plt.show()



等高线

delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z = Y**2 + X**2
plt.figure(figsize=(12, 6))
plt.subplot(121)
plt.contour(X, Y, Z)
plt.colorbar()
plt.title("contour")



本地图片
img=mpimg.imread('marvin.jpg')
plt.subplot(122)
plt.imshow(img)
plt.title("imshow")
plt.show()
#plt.savefig("matplot_sample.jpg")


填充区域
axes[3].fill_between(x, x**2, x**3, color="green", alpha=0.5);

步图(梯级图)
axes[1].step(n, n**2, lw=2)


更多参考(2维图,3维图)
[url]https://matplotlib.org/gallery.html[/url]


[url]https://blog.csdn.net/lilianforever/article/details/48786795[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jie310600

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值