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 で、x,y の軸上の数値を表示する方法

ax の xtics と ytics を使って表示する。 詳細は次のドキュメントを参照。 https://matplotlib.org/stable/gallery/lines_bars_and_ma …

no image

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

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

no image

matplotlib で、粗い刻みと細かい刻みの目盛りを表示する方法

set_major_locator で粗い刻みの目盛りを調整する。 set_minor_locator で、細かい刻みの目盛りを調整する。 以下の例で set_major_formatter は、表示 …

no image

matplotlib でグラフ表示ウィンドウの画面上の位置を自由に設定する方法

matplotlib.use(‘TkAgg’) としておき、 get_current_fig_manager().window.wm_geometry(“+20+50”) として、(+20+50)のと …

no image

matplotlib で、線の種類を変更する方法

「linestyle = 」として、線の種類を指定することができる設定の詳細は、次のリンクを参照。 https://matplotlib.org/stable/gallery/lines_bars_a …