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 でグラフの背景の色を変える方法(facecolor)

次のように、axes で facecolor を変更すればよい。 例 from matplotlib import pyplot as plt x = [2,7,8] y = [7,1 …

no image

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

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

no image

matplotlib で散布グラフを描く方法

pyplt.scatter を使って散布グラフを描くことができる。 点の色は、点ごとに変えることができる。 下の例では color の配列で点の色を指定している。 例 from matplotlib …

no image

matplotlib で、矢印を描く方法

arrowprops のパラメータは次を参考にして設定できる。 https://matplotlib.org/stable/api/_as_gen/matplotlib.patches.FancyAr …

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

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