matplotlib

matplotlib のプロットスクリプト内で計算する方法

投稿日:

gnuplot の場合

gnuplot では、plot “data.txt” u 1:($2*2)などと書くと、

数値を加工(データ値を使った計算)を行った結果をグラフ化することができる。

matplotlib の場合

いっぽう、matplotlib では map を使ってデータ(リスト)を操作することにより、データを加工することができる。

各データに対して、myfunction という関数を作用させたものをプロットする。

# -*- coding: utf-8 -*-
import numpy as np
from matplotlib import pyplot as plt

x = np.linspace(-2,5,50)
y = x

def myfunction(x):
    return x*x - 2 * x + 1

y2 = map(myfunction, y)

plt.rcParams['font.family'] = 'Hiragino Sans'
plt.plot(x, list(y2), label='関数', marker='o')
plt.xlabel('年')
plt.ylabel('トン')
plt.legend()
plt.show()

結果

-matplotlib
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

matplotlib で、グラフの点の見た目を変更する方法

「marker」で指定すればよい。 次のページを参考に、plotの「marker」を変更する。 https://matplotlib.org/stable/api/markers_api.html 例 …

matplotlib のグラフ作成と gnuplot との対応 比較

起動方法 gnuplot コマンドで $ gnuplot とすれば起動できる。 初期設定ファイルは gnuplotrc にある。 gnuplotrc の場所の見つけ方(macの場合) https:// …

no image

matplotlib で、縦横の線(グリッド)を描く方法

pyplot grid で縦横線(罫線のようなもの)を引くことができる。 例 from matplotlib import pyplot as plt data_x = [25,26,27] …

no image

matplotlib で、y軸に二種類の軸を設定する方法

y軸に、2種類のグラフを異なるスケールでプロットする。 matplotlib axes の twinx() を使う。 https://matplotlib.org/stable/api/_as_gen …

no image

matplotlib で、グラフのタイトルに上付き文字を入力する方法

数学記号などで、グラフのタイトルに上付きの文字を入力したい場合がある。 その場合は次のように書いて設定できる。 例 import numpy as np from matplotlib import …