対数対数プロットの作成

MatplotlibMatplotlibBeginner
今すぐ練習

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

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

はじめに

このチュートリアルでは、Python の Matplotlib を使って対数対数プロットを作成する方法を学びます。対数対数プロットは、x 軸と y 軸の両方が対数スケールになっているグラフの一種です。これにより、複数桁にわたるデータをコンパクトで情報豊富な方法で視覚化することができます。

VM のヒント

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

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

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


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("Matplotlib")) -.-> matplotlib/BasicConceptsGroup(["Basic Concepts"]) matplotlib(("Matplotlib")) -.-> matplotlib/PlottingDataGroup(["Plotting Data"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) matplotlib(("Matplotlib")) -.-> matplotlib/AdvancedPlottingGroup(["Advanced Plotting"]) python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) python(("Python")) -.-> python/FileHandlingGroup(["File Handling"]) python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("Importing Matplotlib") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("Understanding Figures and Axes") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("Line Plots") python/DataStructuresGroup -.-> python/lists("Lists") python/DataStructuresGroup -.-> python/tuples("Tuples") matplotlib/AdvancedPlottingGroup -.-> matplotlib/log_scale("Logarithmic Scale") python/ModulesandPackagesGroup -.-> python/importing_modules("Importing Modules") python/FileHandlingGroup -.-> python/with_statement("Using with Statement") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("Data Visualization") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48557{{"対数対数プロットの作成"}} matplotlib/figures_axes -.-> lab-48557{{"対数対数プロットの作成"}} matplotlib/line_plots -.-> lab-48557{{"対数対数プロットの作成"}} python/lists -.-> lab-48557{{"対数対数プロットの作成"}} python/tuples -.-> lab-48557{{"対数対数プロットの作成"}} matplotlib/log_scale -.-> lab-48557{{"対数対数プロットの作成"}} python/importing_modules -.-> lab-48557{{"対数対数プロットの作成"}} python/with_statement -.-> lab-48557{{"対数対数プロットの作成"}} python/data_visualization -.-> lab-48557{{"対数対数プロットの作成"}} end

ライブラリのインポート

まず、必要なライブラリをインポートする必要があります。グラフを作成するために matplotlib.pyplot を使用します。

import matplotlib.pyplot as plt

調整可能なボックス付きの対数対数プロットの作成

次に、調整可能なボックス付きの対数対数プロットを作成します。これは、x 軸と y 軸の両方が対数スケールになり、プロットのアスペクト比が 1 に等しくなることを意味します。

fig, ax = plt.subplots()
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_xlim(1e1, 1e3)
ax.set_ylim(1e2, 1e3)
ax.set_aspect(1)
ax.set_title("Log-Log Plot with Adjustable Box")
plt.show()

調整可能なデータ制限付きの対数対数プロットの作成

次に、調整可能なデータ制限付きの対数対数プロットを作成します。これは、x 軸と y 軸の両方が対数スケールになり、プロットのアスペクト比がデータに合うように調整されることを意味します。

fig, ax = plt.subplots()
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_adjustable("datalim")
ax.plot([1, 3, 10], [1, 9, 100], "o-")
ax.set_xlim(1e-1, 1e2)
ax.set_ylim(1e-1, 1e3)
ax.set_aspect(1)
ax.set_title("Log-Log Plot with Adjustable Datalim")
plt.show()

まとめ

このチュートリアルでは、Python の Matplotlib を使って対数対数プロットを作成する方法を学びました。調整可能なボックス付きと調整可能なデータ制限付きの 2 種類の対数対数プロットを作成しました。これらのプロットは、複数桁にわたるデータを視覚化するのに役立ちます。