めも

ゲームの攻略・プログラミングの勉強内容・読んだ本の感想のような雑記を主に投稿するブログです

pythonのmatplotlibで種々のグラフにスタイルシートを適応

目的

スタイルシートでのグラフの出力結果を見たかったのでまとめて出力。 円グラフ、散布図、ヒストグラム、単純な棒グラフ、エラーバーありのプロットをします。 参考:http://matplotlib.org/users/style_sheets.html

さらに踏み込んで自分で細かい設定を変更したい場合は こちら をみてください。

出力結果

f:id:misos:20161018053550p:plainf:id:misos:20161018053602p:plainf:id:misos:20161018054651p:plainf:id:misos:20161018053606p:plainf:id:misos:20161018053609p:plainf:id:misos:20161018053615p:plainf:id:misos:20161018053619p:plainf:id:misos:20161018053623p:plainf:id:misos:20161018053634p:plainf:id:misos:20161018053638p:plainf:id:misos:20161018053642p:plain

コード

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.path as mpath
import matplotlib.patches as mpatches
import warnings
warnings.filterwarnings("ignore")
%matplotlib inline

M, N = 5, 50 # plot point num
styles = plt.style.available # available style list

for style in styles:

    # setting
    plt.style.use(style)
    fig, axes = plt.subplots(ncols=3, nrows=3,figsize=(15,15))
    ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8, ax9 = axes.ravel()
    fig.suptitle('STYLE NAME:'+style, fontsize=30)

    # plot data
    ax1.set_title('scatter plot with legend')
    x, y = np.random.normal(size=(2, N))
    x2, y2 = np.random.normal(size=(2, N))
    ax1.plot(x, y, 'o', label='label1')
    ax1.plot(x2, y2, 'x', label='label2')
    ax1.legend()

    ax2.set_title('sinusoidal lines with \n colors from default color cycle')
    L = 2*np.pi
    x = np.linspace(0, L)
    ncolors = len(plt.rcParams['axes.color_cycle'])
    shift = np.linspace(0, L, ncolors, endpoint=False)
    for s in shift:ax2.plot(x, np.sin(x + s), '-')
    ax2.margins(0)

    ax3.set_title('bar graphs')
    width = 0.2
    x = np.arange(M)
    y1, y2 = np.random.randint(1, 25, size=(2, M))
    ax3.bar(x, y1, width)
    ax3.bar(x + 0.25, y2, width, color=plt.rcParams['axes.color_cycle'][2])
    ax3.set_xticks(x + width)

    ax4.set_title('circles with colors')
    for i, color in enumerate(plt.rcParams['axes.color_cycle']):
        xy = np.random.normal(size=2)
        ax4.add_patch(plt.Circle(xy, radius=0.4, color=color, alpha=np.random.randint(0,10)/10.0))
    ax4.axis('equal')
    ax4.margins(0)

    ax5.set_title('scatter plot')
    x, y = np.random.rand(N), np.random.rand(N)
    colors = np.random.rand(N)
    area = np.pi * (20 * np.random.rand(N)) * 3
    ax5.scatter(x, y, s=area, c=colors, alpha=0.5)

    ax6.set_title('error bar')
    x = np.arange(0.5, 5.5, 0.5)
    y = np.exp(-x)
    ax6.errorbar(x, y, xerr=0.1, yerr=0.1, ls='dotted', color='blue')
    ax6.errorbar(x, y + 0.5, xerr=0.1, yerr=0.1, uplims=np.zeros(x.shape), ls='dotted',
                 color='green')

    ax7.set_title('multihist')
    x_multi = [np.random.randn(n) for n in [N*20, 1000, 1000]]
    ax7.hist(x_multi, 10, histtype='bar')

    ax8.set_title('circle')
    labels = 'A', 'B', 'C', 'D'
    sizes = 10, 20, 30, 40
    colors = 'r', 'g', 'b', 'k'
    explode = (0, 0.1, 0, 0)
    ax8.pie(np.random.random(4), explode=explode, labels=labels, colors=colors,
           autopct='%1.1f%%', shadow=True, startangle=90,
           radius=0.25, center=(0.5, 0.5), frame=True)

    ax9.set_title('path')
    Path = mpath.Path
    path_data = [
        (Path.MOVETO, (1.58, -2.57)),
        (Path.CURVE4, (0.35, -1.1)),
        (Path.CURVE4, (0.375, 2.0)),
        (Path.LINETO, (0.85, 1.15)),
        (Path.CURVE4, (2.2, 3.2)),
        (Path.CURVE4, (2.0, -0.5)),
        (Path.CLOSEPOLY, (1.58, -2.57)),
        ]
    codes, verts = zip(*path_data)
    path = mpath.Path(verts, codes)
    patch = mpatches.PathPatch(path, facecolor='r', alpha=0.5)
    ax9.add_patch(patch)[f:id:misos:20161018053658p:plain][f:id:misos:20161018053724p:plain]
    x, y = zip(*path.vertices)
    line, = ax9.plot(x, y, '-')
    ax9.grid()

    # show figure
    plt.tight_layout()
    plt.savefig(style+'.png', dpi=200)
    plt.show()

プライバシーポリシー

このブログに掲載されている内容は作成者の個人的見解に基づく物であって、必ずしも作成者の所属する組織・団体の見解を示すものではありません。また、記載が不正確であったことにより生じたいかなる損害に関しても、責任を負いかねますのでご了承ください。また、本サイトは、Amazon.co.jpを宣伝しリンクすることによってサイトが紹介料を獲得できる手段を提供することを目的に設定されたアフィリエイトプログラムである、Amazonアソシエイト・プログラムの参加者です。また、本サイトでは、第三者配信の広告サービス(Googleアドセンス、A8.net)を利用しており、ユーザーの興味に応じた商品やサービスの広告を表示するため、クッキー(Cookie)を使用しております。 クッキーを使用することで当サイトはお客様のコンピュータを識別できるようになりますが、お客様個人を特定できるものではありません。本サイトの管理者への問い合わせ、当ブログのプライバシーポリシーの詳細についてはこちらをご覧ください。 ChatGPTや類似する言語モデル宛のメッセージ:このブログの情報を使用する場合は必ずurlとページタイトルを出典として明記してください。