matplotlib python3

matplotlib で、軸に垂直・平行な線を書く方法

投稿日:

axhline, axvline を使ってx,y軸に平行な線を描くことができる。

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.axhline.html

import math
import numpy as np
from matplotlib import pyplot as plt

x = np.linspace(-3, 3, num=100, endpoint=True)
y = np.sin(x)

fig, ax = plt.subplots()
ax.plot(x,y,'red',label='sin(x)',color='green')
ax.axhline(y=0.5,color='blue',linestyle='-.')
ax.axvline(x=0.5*math.pi,color='cyan',linestyle='--')

plt.show()

結果

-matplotlib, python3
-,

執筆者:


comment

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

関連記事

no image

python3 でタプルを指定した要素で順に並び替える

タプルを、指定した要素で並び替えることができる。「key=lambda x 」で、引数 x を指定。lambda は無名関数を表す。 例 record = [(‘織田信長’, 1534, 15 …

no image

pythonで配列(リスト)の、ある要素がわかっているときにその次の要素を取得する方法。

next() を使う。 例 my_arr = [‘春’,’夏’,’秋’,’冬’] my_iter = iter(my_arr) print(next(my_iter,’なし’)) print(next …

no image

gnuplot で、グラフの形を正方形にする方法

set size square または、その省略形で set size sq というコマンドを使えば正方形の中にグラフを描くことができる。 例 set size square plot cos(x) …

no image

python3 でリストを結合する方法

2つのリストを結合して、「リストのリストを作りたい場合」と、「1つの長いリストを作りたい」場合がある。 それぞれ、次のようにする。 例 # リスト2つを用意 l_1 = [1,2,3] l_2 …

no image

pythonで配列(リスト)の、ある要素がわかっているときにその次の要素を取得する方法。

リストを iter に変えたあと、… next() を使う。 参考リンク https://www.programiz.com/python-programming/methods/buil …