質問するログイン新規登録

回答編集履歴

5

修正d

2020/03/09 07:07

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -28,8 +28,12 @@
28
28
  ## 追記
29
29
 
30
30
  ```python
31
+ import matplotlib.pyplot as plt
32
+ import numpy as np
31
33
  from mpl_toolkits.axes_grid1 import ImageGrid
32
34
 
35
+ data = np.random.randint(0, 256, (20, 20))
36
+
33
37
  fig = plt.figure(figsize=(10, 5))
34
38
 
35
39
  grid = ImageGrid(
@@ -52,9 +56,9 @@
52
56
  extent=[0, data.shape[0], 0, data.shape[1]],
53
57
  origin="lower",
54
58
  )
59
+ grid[0].set_xlabel("X")
55
- ax1.set_xlabel("X")
60
+ grid[1].set_xlabel("X")
56
- ax2.set_xlabel("X")
57
- ax1.set_ylabel("Y")
61
+ grid[0].set_ylabel("Y")
58
62
 
59
63
  for ax in grid:
60
64
  # 目盛りはここで設定

4

修正

2020/03/09 07:07

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -36,7 +36,7 @@
36
36
  fig,
37
37
  111,
38
38
  nrows_ncols=(1, 2),
39
- axes_pad=0.1,
39
+ axes_pad=0.2,
40
40
  share_all=True,
41
41
  cbar_location="right",
42
42
  cbar_mode="single",
@@ -56,7 +56,8 @@
56
56
  ax2.set_xlabel("X")
57
57
  ax1.set_ylabel("Y")
58
58
 
59
- for ax in [grid[0], grid[1]]:
59
+ for ax in grid:
60
+ # 目盛りはここで設定
60
61
  ax.set_xticks(np.arange(0, 21, 2))
61
62
  ax.set_yticks(np.arange(0, 21, 2))
62
63
 
@@ -65,4 +66,4 @@
65
66
  plt.show()
66
67
  ```
67
68
 
68
- ![イメージ説明](3bd2006933674931f9845be509a9364b.png)
69
+ ![イメージ説明](05e8263af8016ae2cd31e5e54d85f0ee.png)

3

修正

2020/03/09 06:24

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -23,4 +23,46 @@
23
23
  plt.show()
24
24
  ```
25
25
 
26
- ![イメージ説明](1297354aa2c34608f337c46ddfd84d6f.png)
26
+ ![イメージ説明](1297354aa2c34608f337c46ddfd84d6f.png)
27
+
28
+ ## 追記
29
+
30
+ ```python
31
+ from mpl_toolkits.axes_grid1 import ImageGrid
32
+
33
+ fig = plt.figure(figsize=(10, 5))
34
+
35
+ grid = ImageGrid(
36
+ fig,
37
+ 111,
38
+ nrows_ncols=(1, 2),
39
+ axes_pad=0.1,
40
+ share_all=True,
41
+ cbar_location="right",
42
+ cbar_mode="single",
43
+ )
44
+
45
+ im1 = grid[0].imshow(
46
+ data, cmap="plasma", extent=[0, data.shape[0], 0, data.shape[1]], origin="lower",
47
+ )
48
+ im2 = grid[1].imshow(
49
+ data,
50
+ cmap="plasma",
51
+ interpolation="bicubic",
52
+ extent=[0, data.shape[0], 0, data.shape[1]],
53
+ origin="lower",
54
+ )
55
+ ax1.set_xlabel("X")
56
+ ax2.set_xlabel("X")
57
+ ax1.set_ylabel("Y")
58
+
59
+ for ax in [grid[0], grid[1]]:
60
+ ax.set_xticks(np.arange(0, 21, 2))
61
+ ax.set_yticks(np.arange(0, 21, 2))
62
+
63
+ grid.cbar_axes[0].colorbar(im2)
64
+
65
+ plt.show()
66
+ ```
67
+
68
+ ![イメージ説明](3bd2006933674931f9845be509a9364b.png)

2

修正

2020/03/09 06:22

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -18,7 +18,7 @@
18
18
 
19
19
  divider = make_axes_locatable(ax2)
20
20
  cax = divider.append_axes("right", size="5%", pad=0.05)
21
- plt.colorbar(im2, cax=cax)
21
+ fig.colorbar(im2, cax=cax)
22
22
 
23
23
  plt.show()
24
24
  ```

1

修正

2020/03/09 04:53

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -4,24 +4,23 @@
4
4
  ```python
5
5
  import matplotlib.pyplot as plt
6
6
  import numpy as np
7
+ from mpl_toolkits.axes_grid1 import make_axes_locatable
7
8
 
8
9
  data = np.random.randint(0, 256, (20, 20))
9
10
 
10
- fig, axes = plt.subplots(figsize=(8, 4), ncols=2)
11
+ fig, [ax1, ax2] = plt.subplots(figsize=(8, 4), ncols=2, sharey=True)
11
- im1 = axes[0].imshow(data, cmap="plasma")
12
- im2 = axes[1].imshow(data, cmap="plasma", interpolation="bicubic")
12
+ im1 = ax1.imshow(data, cmap="plasma", aspect="auto")
13
- axes[0].set_xlabel("X")
13
+ im2 = ax2.imshow(data, cmap="plasma", interpolation="bicubic", aspect="auto")
14
- axes[1].set_xlabel("X")
14
+ ax1.set_xlabel("X")
15
+ ax2.set_xlabel("X")
15
- axes[0].set_ylabel("Y")
16
+ ax1.set_ylabel("Y")
17
+ fig.subplots_adjust(wspace=0.07)
16
18
 
17
- axpos = axes[0].get_position()
19
+ divider = make_axes_locatable(ax2)
18
- # add_axes([x軸の開始位置, y軸の開始位置, x軸の長さ(全体に対する比率), y軸の長さ(全体に対する比率)])
19
- cbar_ax = fig.add_axes([0.87, axpos.y0, 0.02, axpos.height])
20
+ cax = divider.append_axes("right", size="5%", pad=0.05)
20
- cbar = fig.colorbar(im2, cax=cbar_ax)
21
+ plt.colorbar(im2, cax=cax)
21
- cbar.set_label("Temp")
22
+
22
- plt.subplots_adjust(right=0.85)
23
- plt.subplots_adjust(wspace=0.15)
24
23
  plt.show()
25
24
  ```
26
25
 
27
- ![イメージ説明](eaacfb1a661ea220e9fbfbf454a6fbfb.png)
26
+ ![イメージ説明](1297354aa2c34608f337c46ddfd84d6f.png)