SciPy.org 004 Matplotlib数据可视化

Matplotlib是一个重量级数据可视化工具。包括以下方面:

 Gallery描述
1Line, bars and markers线条图,指柱图和标记图
2Images, contours and fields图片,等高线和场线图
3Subplots, axes and figures子图,轴和标识
4Statistics统计学相关图
5Pie and polar charts饼图和极坐标图
6Text, labels and annotations文字,标识和注释
7Pyplot 
8Color颜色能量图
9Shapes and collections形状和符号图
10Style sheets样式表
11Axes Grid轴坐标网格图
12Axis Artist艺术化轴图
13Showcase个性化展示图形
14Animation动画
15Event handling事件交互句柄
16Front Page 
17Miscellaneous其它
183D plotting三维图
19Our Favorite Recipes推荐实例
20Scales缩放
21Specialty Plots特殊应用
22Ticks and spines点线图
23Units功能单元介绍
24Embedding Matplotlib in graphical user interfaces第三方应用
25Userdemo实例
26WidgetsGUI控件

 

分形学有一个非常重要的图,叫做曼德布洛特集合(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/

是详细的例子。代码也比较精简,方便重现。基础学习比较好,系统性强。

多谢,美。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值