Matplotlib是一个重量级数据可视化工具。包括以下方面:
Gallery | 描述 | |
1 | Line, bars and markers | 线条图,指柱图和标记图 |
2 | Images, contours and fields | 图片,等高线和场线图 |
3 | Subplots, axes and figures | 子图,轴和标识 |
4 | Statistics | 统计学相关图 |
5 | Pie and polar charts | 饼图和极坐标图 |
6 | Text, labels and annotations | 文字,标识和注释 |
7 | Pyplot | |
8 | Color | 颜色能量图 |
9 | Shapes and collections | 形状和符号图 |
10 | Style sheets | 样式表 |
11 | Axes Grid | 轴坐标网格图 |
12 | Axis Artist | 艺术化轴图 |
13 | Showcase | 个性化展示图形 |
14 | Animation | 动画 |
15 | Event handling | 事件交互句柄 |
16 | Front Page | |
17 | Miscellaneous | 其它 |
18 | 3D plotting | 三维图 |
19 | Our Favorite Recipes | 推荐实例 |
20 | Scales | 缩放 |
21 | Specialty Plots | 特殊应用 |
22 | Ticks and spines | 点线图 |
23 | Units | 功能单元介绍 |
24 | Embedding Matplotlib in graphical user interfaces | 第三方应用 |
25 | Userdemo | 实例 |
26 | Widgets | GUI控件 |
分形学有一个非常重要的图,叫做曼德布洛特集合(Mandelbrot set),如下:
用Matplotlib好容易就可以画出来,代码如下:
"""
有趣的事情
没有结束
2020/4/17 16:55
"""
import numpy as np
def mandelbrot_set(xmin, xmax, ymin, ymax, xn, yn, maxiter, horizon=2.0):
X = np.linspace(xmin, xmax, xn)
Y = np.linspace(ymin, ymax, yn)
C = X + Y[:, None] * 1j
N = np.zeros_like(C, dtype=int)
Z = np.zeros_like(C)
for n in range(maxiter):
I = np.less(abs(Z), horizon)
N[I] = n
Z[I] = Z[I]**2 + C[I]
N[N == maxiter-1] = 0
return Z, N
if __name__ == '__main__':
import time
import matplotlib
from matplotlib import colors
import matplotlib.pyplot as plt
xmin, xmax, xn = -2.25, +0.75, 1500
ymin, ymax, yn = -1.25, +1.25, 1500
maxiter = 20
horizon = 2.0 ** 40
log_horizon = np.log(np.log(horizon))/np.log(2)
Z, N = mandelbrot_set(xmin, xmax, ymin, ymax, xn, yn, maxiter, horizon)
# Normalized recount as explained in:
# https://linas.org/art-gallery/escape/smooth.html
# https://www.ibm.com/developerworks/community/blogs/jfp/entry/My_Christmas_Gift
# This line will generate warnings for null values but it is faster to
# process them afterwards using the nan_to_num
with np.errstate(invalid='ignore'):
M = np.nan_to_num(N + 1 -
np.log(np.log(abs(Z)))/np.log(2) +
log_horizon)
dpi = 72
width = 10
height = 10*yn/xn
fig = plt.figure(figsize=(width, height), dpi=dpi)
ax = fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False, aspect=1)
# Shaded rendering
# light = colors.LightSource(azdeg=315, altdeg=10)
# M = light.shade(M, cmap=plt.cm.hot, vert_exag=1.5,
# norm=colors.PowerNorm(0.3), blend_mode='hsv')
plt.imshow(M, extent=[xmin, xmax, ymin, ymax], interpolation="bicubic")
ax.set_xticks([])
ax.set_yticks([])
# Some advertisement for matplotlib
# year = time.strftime("%Y")
# text = ("The Mandelbrot fractal set\n"
# "Rendered with matplotlib %s, %s - http://matplotlib.org"
# % (matplotlib.__version__, year))
# ax.text(xmin+.025, ymin+.025, text, color="white", fontsize=12, alpha=0.5)
plt.show()
修改其中的参数,会得到各种各样的变化,也是分形无穷无尽的特点。
有三个与Scipy.org相关比较好的中文网,网址如下:
NumPy中文网:https://www.numpy.org.cn/
matplotlib中文网:https://www.matplotlib.org.cn/
pandas中文网:https://www.pypandas.cn/
是详细的例子。代码也比较精简,方便重现。基础学习比较好,系统性强。
多谢,美。