グラフの描画


In [1]:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from pandas import Series, DataFrame

練習問題

(1) -1≦x≦1 の範囲で、sin(2πx) と cos(2πx) のグラフを重ねて表示してください。軸ラベルや凡例などを好みで追加してください。


In [2]:
data_x = np.linspace(-1,1,100)
data_sin = np.sin(2*np.pi*data_x)
data_cos = np.cos(2*np.pi*data_x)

fig = plt.figure(figsize=(6,2))
subplot = fig.add_subplot(1,1,1)
subplot.set_xlabel('x')
subplot.set_ylabel('y')
subplot.set_xlim(-1,1)
subplot.plot(data_x, data_sin, label=u'y=sin 2πx')
subplot.plot(data_x, data_cos, label=u'y=cos 2πx')
subplot.legend(loc=1)


Out[2]:
<matplotlib.legend.Legend at 0x5666890>

(2) -1≦x≦1 の範囲で、sin(2πx) と cos(2πx) のグラフを(上下2段に)別々に表示してください。軸ラベルや凡例などを好みで追加してください。


In [3]:
data_x = np.linspace(-1,1,100)
data_sin = np.sin(2*np.pi*data_x)
data_cos = np.cos(2*np.pi*data_x)

fig = plt.figure(figsize=(6,4))

subplot = fig.add_subplot(2,1,1)
subplot.set_xlabel('x')
subplot.set_ylabel('y')
subplot.set_xlim(-1,1)
subplot.plot(data_x, data_sin, label=u'y=sin 2πx')
subplot.legend(loc=1)

subplot = fig.add_subplot(2,1,2)
subplot.set_xlabel('x')
subplot.set_ylabel('y')
subplot.set_xlim(-1,1)
subplot.plot(data_x, data_cos, label=u'y=cos 2πx')
subplot.legend(loc=1)


Out[3]:
<matplotlib.legend.Legend at 0x589c0d0>

(3) 次は、-10≦x≦10, -10≦y≦10(x, yは整数)の範囲について、x*x + y*y の値を保存した10x10行列(array)を用意して、ヒートマップを描くコードです。中間部分に必要なコードを追加して、実際にヒートマップを描いてください。

data = np.array([[x**2 + y**2 for x in range(-10,11)] for y in range(-10,11)]) # この部分に必要なコードを追加してください。 subplot.imshow(data, origin='lower', extent=(-10,10,-10,10), interpolation='nearest')

In [4]:
data = np.array([[x**2 + y**2 for x in range(-10,11)] for y in range(-10,11)])
fig = plt.figure()
subplot = fig.add_subplot(1,1,1)
subplot.imshow(data, origin='lower', extent=(-10,10,-10,10), interpolation='nearest')


Out[4]:
<matplotlib.image.AxesImage at 0x563ff50>

(4) (3)において、imshowメソッドのオプション interpolation='nearest' を取ると結果がどのように変わるか確認しなさい。あるいは、オプション cmap=plt.cm.gray_r を付けるとどのように変わるか確認しなさい。


In [5]:
data = np.array([[x**2 + y**2 for x in range(-10,11)] for y in range(-10,11)])
fig = plt.figure()
subplot = fig.add_subplot(1,1,1)
subplot.imshow(data, origin='lower', extent=(-10,10,-10,10))


Out[5]:
<matplotlib.image.AxesImage at 0x5b43f90>

In [6]:
data = np.array([[x**2 + y**2 for x in range(-10,11)] for y in range(-10,11)])
fig = plt.figure()
subplot = fig.add_subplot(1,1,1)
subplot.imshow(data, origin='lower', extent=(-10,10,-10,10), cmap=plt.cm.gray_r)


Out[6]:
<matplotlib.image.AxesImage at 0x5c3bc50>

(5) 次は、(3)と同じデータを 0≦x≦10, 0≦y≦10 の範囲に制限したものです。(3)と同様のコードでヒートマップを描いて、originオプションの有無、もしくは、extentオプションに指定する値を変更した際にグラフがどのように変化するか観察してください。

(ヒント:extentオプションは、グラフの目盛りに付ける数値を指定するもので、グラフに描くデータの範囲を指定するものではありません。)

data = np.array([[x**2 + y**2 for x in range(0,11)] for y in range(0,11)]) # この部分に必要なコードを追加してください。 subplot.imshow(data, origin='lower', extent=(0,10,0,10), interpolation='nearest')

問題文の指定どおりにグラフを描くと次が得られます。


In [7]:
data = np.array([[x**2 + y**2 for x in range(0,11)] for y in range(0,11)])
fig = plt.figure()
subplot = fig.add_subplot(1,1,1)
subplot.imshow(data, origin='lower', extent=(0,10,0,10), interpolation='nearest')


Out[7]:
<matplotlib.image.AxesImage at 0x5c4fc10>

originオプションを取ると左上を原点としてデータが表示されます。ただし、目盛りの付き方は先ほどと変わりません。


In [8]:
data = np.array([[x**2 + y**2 for x in range(0,11)] for y in range(0,11)])
fig = plt.figure()
subplot = fig.add_subplot(1,1,1)
subplot.imshow(data, extent=(0,10,0,10), interpolation='nearest')


Out[8]:
<matplotlib.image.AxesImage at 0x5ef5c90>

左上を原点として目盛りを付けるには、extentオプションの指定を変更する必要があります。


In [9]:
data = np.array([[x**2 + y**2 for x in range(0,11)] for y in range(0,11)])
fig = plt.figure()
subplot = fig.add_subplot(1,1,1)
subplot.imshow(data, extent=(0,10,10,0), interpolation='nearest')


Out[9]:
<matplotlib.image.AxesImage at 0x612a5d0>