Matplotlib における主目盛りと副目盛り

PythonPythonBeginner
今すぐ練習

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

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

はじめに

Matplotlibのグラフでは、目盛りは軸上のデータポイントの位置を示すために使用されます。主目盛りは、データポイントの値を表す大きな目盛りであり、副目盛りは、主目盛りの間に配置される小さな目盛りです。このチュートリアルでは、Matplotlibで主目盛りと副目盛りを使用する方法を示します。

VMのヒント

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

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

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


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) matplotlib(("Matplotlib")) -.-> matplotlib/BasicConceptsGroup(["Basic Concepts"]) matplotlib(("Matplotlib")) -.-> matplotlib/PlottingDataGroup(["Plotting Data"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("Importing Matplotlib") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("Understanding Figures and Axes") python/BasicConceptsGroup -.-> python/comments("Comments") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("Line Plots") python/DataStructuresGroup -.-> python/tuples("Tuples") python/DataStructuresGroup -.-> python/dictionaries("Dictionaries") python/ModulesandPackagesGroup -.-> python/importing_modules("Importing Modules") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("Numerical Computing") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("Data Visualization") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48816{{"Matplotlib における主目盛りと副目盛り"}} matplotlib/figures_axes -.-> lab-48816{{"Matplotlib における主目盛りと副目盛り"}} python/comments -.-> lab-48816{{"Matplotlib における主目盛りと副目盛り"}} matplotlib/line_plots -.-> lab-48816{{"Matplotlib における主目盛りと副目盛り"}} python/tuples -.-> lab-48816{{"Matplotlib における主目盛りと副目盛り"}} python/dictionaries -.-> lab-48816{{"Matplotlib における主目盛りと副目盛り"}} python/importing_modules -.-> lab-48816{{"Matplotlib における主目盛りと副目盛り"}} python/numerical_computing -.-> lab-48816{{"Matplotlib における主目盛りと副目盛り"}} python/data_visualization -.-> lab-48816{{"Matplotlib における主目盛りと副目盛り"}} end

必要なライブラリをインポートしてデータを作成する

import matplotlib.pyplot as plt
import numpy as np

## Create data
t = np.arange(0.0, 100.0, 0.1)
s = np.sin(0.1 * np.pi * t) * np.exp(-t * 0.01)

まず、必要なライブラリ、つまりMatplotlibとNumPyをインポートします。そして、描画するデータを作成します。この例では、numpy配列「t」を作成し、tを使って別のnumpy配列「s」を計算します。

データを描画する

fig, ax = plt.subplots()
ax.plot(t, s)

次に、グラフと軸のオブジェクトを作成し、軸上にデータを描画します。

主目盛りと副目盛りの設定

## Set the major locator
ax.xaxis.set_major_locator(MultipleLocator(20))
## Set the major formatter
ax.xaxis.set_major_formatter('{x:.0f}')
## Set the minor locator
ax.xaxis.set_minor_locator(MultipleLocator(5))

ここでは、主目盛りの位置を20の倍数で設定し、主目盛りを".0f"形式でラベル付けするための主目盛りのフォーマッタを設定し、副目盛りの位置を5の倍数で設定する副目盛りを設定します。

グラフを表示する

plt.show()

最後に、グラフを表示します。

主目盛りと副目盛りの自動目盛り選択

## Create data
t = np.arange(0.0, 100.0, 0.01)
s = np.sin(2 * np.pi * t) * np.exp(-t * 0.01)

## Plot the data
fig, ax = plt.subplots()
ax.plot(t, s)

## Set the minor locator
ax.xaxis.set_minor_locator(AutoMinorLocator())

## Set the tick parameters
ax.tick_params(which='both', width=2)
ax.tick_params(which='major', length=7)
ax.tick_params(which='minor', length=4, color='r')

## Display the plot
plt.show()

このステップでは、新しいデータを作成して描画します。その後、副目盛りの数を自動的に選択するように副目盛りを設定します。その後、主目盛りと副目盛りの両方に対して、目盛りの幅と長さ、およびそれらの色などの目盛りパラメータを設定します。最後に、グラフを表示します。

まとめ

このチュートリアルでは、Matplotlibで主目盛りと副目盛りを使用する方法を示しました。主目盛りと副目盛りの位置付けとフォーマッタを設定する方法、および副目盛りの数を自動的に選択する方法を見ました。また、目盛りの幅と長さ、およびそれらの色などの目盛りパラメータを設定する方法も見ました。