绘制折线图(plt.plot)
设置图片大小和分辨率(plt.figure)
保存图片到本地(plt.savefig)
设置xy轴刻度和字符串(xticks、yticks)
设置标题、xy轴标签(title、xlable、ylable)
其他图像类型(散点图plt.scatter,条形图plt.bar,横向plt.barh,直方图plt.hist(bin.width组距、num_bins分多少组、))
from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.ticker as ticker
from matplotlib.ticker import MultipleLocator
# 设置多个刻度范围
plt.figure(figsize=(20, 20))
ran = []
x = np.arange(-10, 10, 0.01)
y = 1
y = x + 1/x+5*np.cos(x)
ran.extend(range(-10, 10, 1))
plt.xticks(ran, rotation=20, fontsize=10)
plt.yticks(range(-10, 10, 1), fontsize=10)
plt.xlim(-10, 10)
plt.ylim(-10, 10)
plt.grid(alpha=1) #画出格子
plt.plot(x, y, color='green', marker='o', linestyle='solid', linewidth=1, markersize=6)
plt.legend(["y = x + 1/x + 5cosx "], loc="best")
plt.savefig('D:\ef2d.png')
plt.show()
from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.ticker as ticker
from matplotlib.ticker import MultipleLocator
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y)
#Z = np.sqrt(X**2 + Y**2)
# Z = np.sin(R)
Z = X**2 - Y**2
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')
plt.savefig('D:\img3d.png') #保存图像
plt.show()
例子:比如说这道考研05年数二、数三的二重积分的题,刚开始我就不明白
为什么一个正方形,要分成两个区域(当然我知道要用极坐标),后来画个图看一下就明白了
代码:
from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.ticker as ticker
from matplotlib.ticker import MultipleLocator
import sympy as sym
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(0, 1, 0.01)
Y = np.arange(0, 1, 0.01)
X, Y = np.meshgrid(X, Y)
Z = abs(X**2 + Y**2 - 1)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')
plt.savefig('D:\img3d.png') #保存图像
plt.show()
如下图所示,被积函数因为绝对值符号所以分成两个区域方便计算,本质上就是求两个不同区域的体积,
两个区域分别是
D1 = x^2+y ^2<=1(x>=0,y>=0)
D2 = x^2+y ^2>1(x<=1,y<=1)