Help us understand the problem. What is going on with this article?

matplotlibで複数の図を同時に書くには?

plt.subplots()をつかう

import matplotlib.pyplot as plt
# plt.subplots() can be used for multi-ploting
fig, axes = plt.subplots(nrows=1, ncols=2)
x = [ [i for i in range(10)], [i for i in range(10)] ]
y = [ [xi for xi in x[0]]     ,   [xi**2 for xi in x[1]] ]

# axes.flat is an iterable object that returns each axis in a list.
for i, ax in enumerate(axes.flat):
    ax.scatter(x[i],y[i])

output_1_0.png

axes.flatはiterableオブジェクトであり、これに対してループを回すとここの軸axを返してくれます。これらの軸に対して ax.scatterをすることで描写しました。

ついでに二番目の図の縦軸をけしてみた:plt.setp

複数のグラフを書くときは同じ軸ラベルを使いたい時があるので、その練習として取り上げる。

# if you want to remove the y-ticks of the second figure, then you can do like:
fig, axes = plt.subplots(nrows=1, ncols=2)
x = [ [i for i in range(10)], [i for i in range(10)] ]
y = [ [xi for xi in x[0]]     ,   [xi**2 for xi in x[1]] ]

for i, ax in enumerate(axes.flat):
    ax.scatter(x[i],y[i])
#Note: this function works such that yticks labels are removed.
plt.setp(ax, yticklabels=[])
#or
#plt.setp(ax.get_yticklabels(), visible = False)

output_2_1.png

思ったとうりになりました。

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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした