Matplotlib绘图基础入门

Jupyter Notebook
Matplotlib绘图基础入门
Matplotlib入门
#导入工具包加粗样式
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(20);
print(x);
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
是x的平方
y = x**2; #表示是x的平方
print(y);
[  0   1   4   9  16  25  36  49  64  81 100 121 144 169 196 225 256 289
 324 361]
plt.plot(x,y);
plt.show();

python中以面向对象的思维绘制图像
第一步,创建Figure对象。类似一张画布,使用者可以在画布上创建其他对象
fig=plt.figure();
fig=plt.figure();
<Figure size 432x288 with 0 Axes>
相应
可以通过对象调用相应方法,实现在画布上画图(添加对象)的操作
ax = fig.add_axes([0.1,0.1,0.8,0.8]);
D:\anocoda\lib\site-packages\matplotlib\figure.py:98: MatplotlibDeprecationWarning: 
Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
  "Adding an axes using the same arguments as a previous axes "
##
## 通过fig.add_axes()方法我们在画布上创建了一个Axes对象,
##由于创建的Axes对象是一个容器,因此这个容器还能包含其他东西。定义变量以获得数据,
## 然后通过调用Axes对象的plot方法绘图。如下图所示
fig = plt.figure();
ax = fig.add_axes([0.1,0.1,0.8,0.8]);
x = np.linspace(0,2*np.pi,100);
print(x);
ax.plot(x,np.sin(x));
[0.         0.06346652 0.12693304 0.19039955 0.25386607 0.31733259
 0.38079911 0.44426563 0.50773215 0.57119866 0.63466518 0.6981317
 0.76159822 0.82506474 0.88853126 0.95199777 1.01546429 1.07893081
 1.14239733 1.20586385 1.26933037 1.33279688 1.3962634  1.45972992
 1.52319644 1.58666296 1.65012947 1.71359599 1.77706251 1.84052903
 1.90399555 1.96746207 2.03092858 2.0943951  2.15786162 2.22132814
 2.28479466 2.34826118 2.41172769 2.47519421 2.53866073 2.60212725
 2.66559377 2.72906028 2.7925268  2.85599332 2.91945984 2.98292636
 3.04639288 3.10985939 3.17332591 3.23679243 3.30025895 3.36372547
 3.42719199 3.4906585  3.55412502 3.61759154 3.68105806 3.74452458
 3.8079911  3.87145761 3.93492413 3.99839065 4.06185717 4.12532369
 4.1887902  4.25225672 4.31572324 4.37918976 4.44265628 4.5061228
 4.56958931 4.63305583 4.69652235 4.75998887 4.82345539 4.88692191
 4.95038842 5.01385494 5.07732146 5.14078798 5.2042545  5.26772102
 5.33118753 5.39465405 5.45812057 5.52158709 5.58505361 5.64852012
 5.71198664 5.77545316 5.83891968 5.9023862  5.96585272 6.02931923
 6.09278575 6.15625227 6.21971879 6.28318531]

## 存储图片到本地电脑
fig.savefig("d:/test.png");
设置坐标系
1.坐标网络 当绘制程序使用MATLAB风格时,一般通过plt操作各种绘图相关的方法和属性,而“面向对象”风格通常使用figure对象调用方法创建的对象(如下图的ax对象),但使用二者添加网格的方法是一样的,都是plot()方法。

# 添加网格 
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(20);
y = x**2;
plt.plot(x,y);
plt.grid(color='gray');
plt.show();

2.坐标轴
    可以根据需求自行调整坐标轴的范围和轴标题
;
fig = plt.figure();
ax = fig.add_axes([0.1,0.1,0.8,0.8]);
x = np.linspace(0,2*np.pi,100);
ax.plot(x,np.sin(x));
ax.grid(color='gray');
ax.set_xlabel("x_axis");
ax.set_ylabel("y_axis");
ax.set_xlim((-2,10));
ax.set_ylim((-1.5,1.2));

3.分区 使用plt.plot()绘图,画布上只有一个坐标系,若是想要有多个坐标系,则可以使用plt.suplots();如下图,创建了一个2*3的分区,同一行的分区,共用y轴的文本,同一列的分区,共用x轴的文本。从左上到右下,坐标分别为(0,1),(0,2),(0,3),(1,1),(1,2),(1,3).

,
fig,ax = plt.subplots(2,3,sharex = 'col',sharey = 'row');
for i in range(2):
    for j in range(3):
        ax[i,j].text(0.5,0.5,str((i,j)),fontsize=8,ha='center');

而plt.sublplot()能得到分区中的一个子图。和上图不同,该图的编号是从1开始的,不是从0开始。

;
for i in range(1,7):
    plt.subplot(2,3,i);
    plt.text(0.5,0.5,str((2,3,i)),fontsize=8,ha='center');


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值