シンプルな軸のパディング

PythonPythonBeginner
今すぐ練習

This tutorial is from open-source community. Access the source code

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

この実験では、Matplotlibのadd_floating_axis関数を使ってグラフに浮動軸を追加する方法を学びます。これにより、グラフに関する追加情報を表示できます。具体的には、目盛りラベルと軸ラベルのパディングを調整する方法、および浮動軸上の目盛りの位置を調整する方法を学びます。

VMのヒント

VMの起動が完了したら、左上隅をクリックしてノートブックタブに切り替え、Jupyter Notebookを使って練習しましょう。

時々、Jupyter Notebookが読み込み完了するまで数秒待つ必要がある場合があります。Jupyter Notebookの制限により、操作の検証を自動化することはできません。

学習中に問題がある場合は、Labbyにお問い合わせください。セッション後にフィードバックを提供してください。すぐに問題を解決いたします。

ライブラリのインポート

まず、必要なライブラリをインポートします。これには、matplotlib.pyplotnumpy、およびmpl_toolkits.axisartistが含まれます。

import matplotlib.pyplot as plt
import numpy as np
import mpl_toolkits.axisartist as axisartist

軸の設定関数を定義する

次に、setup_axes()関数を定義します。この関数は、グラフの極座標投影を設定します。この関数は、GridHelperCurveLinearを使用して矩形ボックス内に極座標投影を作成します。また、グラフの範囲を設定し、ax1オブジェクトを返します。

def setup_axes(fig, rect):
    ## Define the PolarAxes transform and the extreme finder
    tr = Affine2D().scale(np.pi/180., 1.) + PolarAxes.PolarTransform()
    extreme_finder = angle_helper.ExtremeFinderCycle(20, 20, lon_cycle=360, lat_cycle=None, lon_minmax=None, lat_minmax=(0, np.inf))

    ## Define the grid locators and formatters
    grid_locator1 = angle_helper.LocatorDMS(12)
    grid_locator2 = grid_finder.MaxNLocator(5)
    tick_formatter1 = angle_helper.FormatterDMS()

    ## Define the GridHelperCurveLinear
    grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, grid_locator2=grid_locator2, tick_formatter1=tick_formatter1)

    ## Create the axis object and set its limits
    ax1 = fig.add_subplot(rect, axes_class=axisartist.Axes, grid_helper=grid_helper)
    ax1.axis[:].set_visible(False)
    ax1.set_aspect(1.)
    ax1.set_xlim(-5, 12)
    ax1.set_ylim(-5, 10)

    return ax1

浮動軸を追加する関数を定義する

グラフに浮動軸を追加するadd_floating_axis関数を定義します。この関数は、引数としてax1オブジェクトを受け取り、axisオブジェクトを返します。

def add_floating_axis(ax1):
    ## Define the floating axis
    ax1.axis["lat"] = axis = ax1.new_floating_axis(0, 30)
    axis.label.set_text(r"$\theta = 30^{\circ}$")
    axis.label.set_visible(True)

    return axis

目盛りラベルにパディングを追加する

このステップでは、浮動軸上の目盛りラベルにパディングを追加します。これは、major_ticklabelsオブジェクトのpad属性を望ましいパディング値に設定することで行えます。

## Add Padding to Tick Labels
fig = plt.figure(figsize=(9, 3.))
fig.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99, wspace=0.01, hspace=0.01)

ax1 = setup_axes(fig, rect=121)
axis = add_floating_axis(ax1)

ax1 = setup_axes(fig, rect=122)
axis = add_floating_axis(ax1)
axis.major_ticklabels.set_pad(10)

plt.show()

軸ラベルのパディングを調整する

このステップでは、浮動軸上の軸ラベルのパディングを調整します。これは、labelオブジェクトのpad属性を望ましいパディング値に設定することで行えます。

## Adjust Axis Label Padding
fig = plt.figure(figsize=(9, 3.))
fig.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99, wspace=0.01, hspace=0.01)

ax1 = setup_axes(fig, rect=121)
axis = add_floating_axis(ax1)

ax1 = setup_axes(fig, rect=122)
axis = add_floating_axis(ax1)
axis.label.set_pad(20)

plt.show()

目盛りの位置を調整する

このステップでは、浮動軸上の目盛りの位置を調整します。これは、major_ticksオブジェクトのtick_out属性をTrueに設定することで行えます。

## Adjust Tick Position
fig = plt.figure(figsize=(9, 3.))
fig.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99, wspace=0.01, hspace=0.01)

ax1 = setup_axes(fig, rect=121)
axis = add_floating_axis(ax1)

ax1 = setup_axes(fig, rect=122)
axis = add_floating_axis(ax1)
axis.major_ticks.set_tick_out(True)

plt.show()

まとめ

この実験では、Matplotlibのadd_floating_axis関数を使ってグラフに浮動軸を追加する方法を学びました。また、目盛りラベルと軸ラベルのパディングを調整する方法、および浮動軸上の目盛りの位置を調整する方法も学びました。この実験が終わるまでに、グラフに関する追加情報を表示する浮動軸付きのカスタマイズされたグラフを作成できるようになるはずです。