Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
OrganizationEventAdvent CalendarQiitadon (β)
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.


【Python@matplotlib】matplotlib にて横、縦の補助線を描く方法について

一般的なやり方は、例えば y = 0 の横線を描くためには、x = [xmin, xmax], y = [0, 0] リストを作成して描きます。
尚、matplotlib では、専有の関数があります、hlines (横線)、 vlines (縦線)。これを使用して、簡単に何本の補助線でも描けます。以下のサンプルをご参照ください。

参考:plt.hlines([value1, value2, ...], xmin, xmax, linestyle="dashed")
value1, value2 は、横線を描く際の y の値に相当します。

import numpy as np
import matplotlib.pyplot as plt

dx = 0.1
xmin, xmax = -3, 3
x = np.arange(xmin, xmax+dx, dx)
y = np.sin(x)

p = plt.plot(x, y, "blue", linestyle='solid')
p = plt.hlines([0], xmin, xmax, "blue", linestyles='dashed')     # hlines
p = plt.plot([xmin, xmax],[0.5, 0.5], "red", linestyle='dashed') # normal way

p = plt.xlim(xmin, xmax)
p = plt.ylim(-1, 1)
p = plt.xlabel('X')
p = plt.ylabel('Y')


Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Help us understand the problem. What are the problem?