Two Y Axes with twinx

CodeUtilityのオンラインMatplotlib IDEでTwo Y Axes with twinxを表示、実行、編集して試せます。

🚀 565,316 合計実行 (12,021 今月)
出力

Pythonのコーディングチャレンジで練習

このエディタで実践的な問題を解こう。サンプルテストを実行し、提出してAIコードレビューを受けられます。

すべてのPythonチャレンジ
簡単

Rectangle Perimeter

Compute the perimeter of a rectangle from its sides.

学べること 算術戻り値
BeginnerLearning-pathVariables
簡単

Days in a Month

Return how many days a month has, including leap years.

学べること 暦の規則補助関数
BeginnerConditionsFunctionsLearning-path
簡単

2つのベクトルの内積

2つのベクトルを要素ごとに掛けて合計します。すべてのニューロンの中心となる計算です。

学べること ベクトル内積
ニューラルネットワークai機械学習
簡単

Distance Between Points

Compute the straight-line distance between two points.

学べること 三平方の定理平方根
BeginnerFunctionsLearning-pathmath
簡単

Average of Three

Average three whole numbers.

学べること 算術除算
BeginnerLearning-pathVariables
簡単

ToDo リスト

タスクのリストを管理します。追加、完了、削除を行い、残っているタスクを報告します。

学べること 型付きコレクションレコード
BeginnerSimulationStrings
簡単

素数判定

数が素数かどうかを判定してください。

学べること 割り切れるかの判定ループの最適化
BeginnerLoopsNumbersStudent-beginner
難しい

Word Break

Decide whether text can be split into dictionary words.

学べること 接頭辞の DP単語への分割
AdvancedDynamic-programmingHackathonStrings
簡単

Count Pairs With a Sum

Count pairs of positions whose values add up to a target.

学べること インデックスの組二重カウントの回避
ArraysBeginnerLearning-pathNested-loops
簡単

欠けている数

0からnまでの範囲で1つだけ欠けている数を見つけてください。

学べること 等差数列和の差の工夫
ArraysBeginnerStudent-beginnermath
中級

K-th Smallest Element

Find the k-th smallest value without fully sorting.

学べること クイックセレクト分割
AdvancedCompetitive-programmingSorting
簡単

Min and Max Together

Find the smallest and largest value in one pass.

学べること 1 回の走査複数の戻り値
ArraysBeginnerFunctionsLearning-path

📊 この Matplotlib オンライン実行ツールについて

CodeUtility Matplotlib Executor は、Matplotlib のプロット命令を含む Python コードを ブラウザ上でそのまま作成・実行できるツールです。セットアップやローカルインストールは不要です。 Matplotlib があらかじめ入った実際の Python 環境で動作し、可視化をすぐに生成できます。

このツールは最新の Matplotlib を使用し、データ分析や科学計算でよく使われる NumPy や Pandas といった一般的な Python ライブラリにも対応しています。

2D チャート、散布図、ヒストグラム、カスタム可視化などを作成できます。コードが正常に実行されると 図は自動的にキャプチャされ、ポップアップウィンドウ に表示されます。

Jupyter Notebook やローカルの Python 環境がなくても、Python の可視化コードをすぐに試したい 学習者、データサイエンティスト、エンジニアに最適です。

単なるコードランナーではありません

同じ実行環境で、複数ファイル対応エディター、分離された実行ターミナル、ランタイム別ライブラリ、永続ワークスペース、共有、リアルタイム共同作業を利用できます。

この実行環境の使い方

ランタイムを選択し、ファイルを開くか作成してコードを記述し、[実行]を選択します。出力はターミナルに表示されます。サインインしたユーザーは、ワークスペースにファイルを保存し、ライブラリをインストールして、共有を通じて共同作業できます。

  1. バージョンを選択して現在のファイルを編集するか、ワークスペース内の別のファイルを開きます。
  2. コードを実行し、求められた場合はターミナルに入力して、保存された実行履歴を確認します。
  3. 複数のコードスニペットが必要な作業では、ライブラリ、ワークスペースツール、共有、チャットを利用します。

📊 Matplotlib 入門

1. Matplotlib とは?

Matplotlib は、静的・アニメーション・インタラクティブな可視化を作成するための人気の Python ライブラリです。データサイエンスや科学計算で特に有用です。

import matplotlib.pyplot as plt

2. 折れ線グラフ

X と Y の値を使った基本的な折れ線グラフ。

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [2, 4, 6, 8]

plt.plot(x, y)
plt.title("折れ線グラフ")
plt.xlabel("X 軸")
plt.ylabel("Y 軸")
plt.show()

3. 棒グラフ

棒グラフは離散的なカテゴリの比較に便利です。

categories = ['A', 'B', 'C']
values = [5, 7, 3]

plt.bar(categories, values)
plt.title("棒グラフ")
plt.show()

4. 散布図

2 つの数値変数の関係を示すのに使います。

x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

plt.scatter(x, y)
plt.title("散布図")
plt.show()

5. 円グラフ

円グラフは割合を表すのに使います。

labels = ['りんご', 'バナナ', 'さくらんぼ']
sizes = [30, 40, 30]

plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.title("果物の割合")
plt.show()

6. ヒストグラム

ヒストグラムは数値データの分布を把握するのに役立ちます。

import numpy as np

data = np.random.randn(1000)

plt.hist(data, bins=30)
plt.title("ヒストグラム")
plt.show()

7. 複数プロット

サブプロットを使って 1 つの図に複数のグラフを描画します。

x = [1, 2, 3, 4]
y1 = [1, 4, 9, 16]
y2 = [2, 3, 5, 7]

plt.subplot(1, 2, 1)
plt.plot(x, y1)
plt.title("プロット 1")

plt.subplot(1, 2, 2)
plt.plot(x, y2)
plt.title("プロット 2")

plt.tight_layout()
plt.show()

8. スタイルのカスタマイズ

色、スタイル、マーカーなどをカスタマイズできます。

plt.plot(x, y1, color='red', linestyle='--', marker='o')
plt.title("スタイル付きライン")
plt.show()

9. 画像として保存

savefig() を使ってプロットを画像ファイルに保存します。

plt.plot(x, y1)
plt.savefig("my_plot.png")

10. 3D プロット(上級)

Matplotlib は mpl_toolkits.mplot3d を通じて 3D 描画もサポートしています。

from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = [1, 2, 3]
y = [2, 3, 4]
z = [5, 6, 7]

ax.scatter(x, y, z)
plt.show()