0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Matplotlib: 今更マンデルブロ集合を描いてみるーiPad mini (A20 Pro)のPythonista 3で実行ー(2026.11.23)

0
Last updated at Posted at 2026-11-23

同様の記事を2026.11.22、noteにアップしましたが、プログラミングの備忘録としてQiitaにあげさせてもらいます。

概要

なんで、今更これをやろうと思ったかというと。。。
最近、iPad mini (A20 Pro)を購入したのだが、どの程度の処理能力なのかを自分なりに確認したかったため、ある程度処理時間が必要な、マンデルブロ集合の描画を思い立った。
MacBook Airではターミナルからの実行、iPad mini, iPad Air, iPnone 16 PlusはPythonista 3での実行である。処理時間の比較結果は、以下のとおり。A20 ProはM3より速い!

処理時間の比較(単位:秒)

Device MacBook Air 13" (M4) iPad mini (A20 Pro) iPad Air 13" (M3) iPhone 16 Plus (A18)
Calculation 1.507 1.294 2.037 1.796
2D drawing 0.175 0.200 0.268 0.267
3D drawing 1.327 4.729 7.246 6.539
Total time 3.009 6.223 9.551 8.601

気になったこと

上記作業のうち、プログラム作成は、MacBookで行っており、iPadとはユニバーサルコントロールで接続して、コードをコピーしている。
iPhoneについても、MacのiPhoneミラーリングで接続し、コードをiPhoneにコピーしている。ここで、MacBookをクラムシェルで使っていると、iPhoneに接続できないことが判明。結局、MacBookの画面を開いておかないと、iPhoneミラーリングは使えなかった。
また、私の M3 iPad Air 13"は、A18チップの iPhone 16 Plusより処理速度が遅かったが、何かおかしい?シングルコア性能の差ということであろうか。

作例

fig_1.jpg

fig_2.jpg

プログラミング説明

以下にプログラミングについて記しておく。

  • 計算は、複素数は使わず、実数演算にて行なっている。
  • 描画したい領域を含む正方形領域を指定し、これを500x500の微小領域に分割し、各点での収束回数を記憶し、2次元描画ではこれに応じた色で塗り分けを行ない、3次元描画では収束回数に応じた高さを描画している。
  • 収束計算回数の上限は100回としている。これを大きくすると加速度的に計算時間が増加する。
  • 収束回数は対数をとって図化している。数値が離れすぎると図が面白くないため。
  • 2次元描画では、当初は簡単のためcontourfを使ってみたが、形状がぼやけるため、pcolormeshで塗り分けを行うようにした。
  • 3次元描画では、surfaceを用いているが、勝手に平滑化しないよう、パラメータを設定している。Geminiによるパラメータの解説を以下に示す。

各オプションの役割

• antialiased=False:面と面の境界線(エッジ)のぼかし処理を無効にし、各ポリゴンをくっきりさせます。デフォルトでは True になっていることが多く、隙間が透けたりぼやけたりする原因になります。
• shade=False:3D空間上の光源計算によるグラデーション(滑らかな陰影)を無効にし、Z値(カラーマップ)本来の色をベタ塗りで表示します。
• rstride=1, cstride=1:matplotlibはデータ点数が多い場合、自動的にメッシュを間引いて描画します。これを 1 に設定することで、元の配列のすべての頂点をそのまま使って描画します。
• edgecolor='black'(任意):各メッシュの枠線を描画します。色の変化が少ないデータでも、カクカクとしたグリッド構造を明確にしたい場合に有効です。

コード

import matplotlib.pyplot as plt
import numpy as np
import time


def fig2(xmin,xmax,ymin,ymax,zmin,zmax,X,Y,Z):
    iw,ih=20,18
    fig = plt.figure(figsize=(iw,ih))
    ax = fig.add_subplot(111, projection='3d')
    ax.set_xlim3d(xmin, xmax)
    ax.set_ylim3d(ymin, ymax)
    ax.set_zlim3d(zmin, zmax)
    ax.set_xlabel('x-axis')
    ax.set_ylabel('y-axis')
    ax.set_zlabel('z-axis')
    ax.set_aspect('auto')

    ax.plot_surface(
        X,Y,Z, 
        rstride=1,
        cstride=1,
        cmap='Pastel2',
        shade=False,
        antialiased=False,
        #facecolor='#ffffff',
        edgecolor='#000000',
        linewidth=0.01)

    plt.tight_layout()
    plt.savefig('fig_2.jpg',dpi=300)
    #plt.show()


def fig1(xmin,xmax,ymin,ymax,X,Y,Z):
    iw=6
    ih=iw/(xmax-xmin)*(ymax-ymin)
    plt.figure(figsize=(iw,ih),facecolor='w')

    plt.xlim([xmin,xmax])
    plt.ylim([ymin,ymax])
    plt.gca().set_aspect('equal',adjustable='box')
    plt.pcolormesh(X,Y,Z,cmap='jet')

    fnameF='fig_1.jpg'
    plt.savefig(fnameF, dpi=300, bbox_inches="tight", pad_inches=0.1)
    #plt.show()


def main():
    start=time.perf_counter()
    x0,y0,rr=-0.75,0,1.5
    nn=500
    nx,ny=nn+1,nn+1
    nmax=100
    
    xmin,xmax=x0-rr,x0+rr
    ymin,ymax=y0-rr,y0+rr
    
    a=np.linspace(xmin,xmax,nx+1)
    b=np.linspace(ymin,ymax,ny+1)
    v=np.zeros((len(b),len(a)))+nmax
    for i in range(len(b)):
        for j in range(len(a)):
            x1,y1=0,0
            for k in range(nmax):
                x2=x1*x1-y1*y1+a[j]
                y2=2*x1*y1+b[i]
                if x2*x2+y2*y2>4.0: break
                x1=x2
                y1=y2
            if x2*x2+y2*y2>4.0: v[i,j]=k+1
    X,Y=np.meshgrid(a,b)
    Z=np.log10(v)/np.log10(nmax)*(xmax-xmin)*0.5
    zmin,zmax=np.min(Z),np.max(Z)
    e1=time.perf_counter()

    fig1(xmin,xmax,ymin,ymax,X,Y,Z)
    e2=time.perf_counter()

    fig2(xmin,xmax,ymin,ymax,zmin,zmax,X,Y,Z)
    e3=time.perf_counter()

    print(f'{e1-start:.3f} sec') # time for calculation
    print(f'{e2-e1:.3f} sec')    # time for 2D drawing
    print(f'{e3-e2:.3f} sec')    # time for 3D drawibf
    print(f'{e3-start:.3f} sec') # total time


#---------------
# Execute
#---------------
if __name__ == '__main__': main()

以 上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?