转
声明:内容全部来自《Python科学计算》(张若愚),仅供学习记录使用。如有兴趣,请尊重知识,购买正版。
Github 链接
-
# -*- coding: utf-8 -*-
-
"""
-
@author: xusworld
-
@file: FirstExample.py
-
@time: 2017-05-06 09:37
-
-
"""
-
-
import matplotlib.pyplot
as plt
-
import numpy
as np
-
x = np.linspace(
0,
10,
1000)
-
y = np.sin(x)
-
z = np.cos(x**
2)
-
-
plt.figure(figsize=(
8,
4))
-
-
plt.plot(x,y, label=
"$sin(x)$", color=
"red", linewidth=
2)
-
plt.plot(x,z,
"b--", label=
"$cos(x^2)$")
-
-
plt.xlabel(
"Times(s)")
-
plt.ylabel(
"Volt")
-
plt.title(
"PyPlot First Example")
-
plt.ylim(
-1.2,
1.2)
-
plt.legend()
-
-
plt.show()
plt.figure(figsize=(8,4))
plt.plot(x,y, label="$sin(x)$", color="red", linewidth=2)
plot传送门创建Figure对象之后,接下来调用plot()在当前的Figure对象中绘图。实际上plot()实在Axes(子图)对象上绘图,如果当前的Figure对象中没有Axes对象,将会位置
plt.plot(x,z,"b--", label="$cos(x^2)$")
plt.xlabel("Times(s)") plt.ylabel("Volt")
plt.title("PyPlot First Example")
plt.ylim(-1.2, 1.2)
plt.legend()
plt.show()
-
figure = plt.gcf()
-
axes = plt.gca()
-
print(figure)
-
print(axes)
-
-
Output
-
Figure(
640x480)
-
Axes(
0.125,
0.11;
0.775x0
.77)