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

回答編集履歴

2

質問追加に対する回答追加

2021/03/21 14:09

投稿

ppaul
ppaul

スコア24672

answer CHANGED
@@ -12,7 +12,7 @@
12
12
  x-axis autoscale setting will be inherited from the original
13
13
  Axes. To ensure that the tick marks of both y-axes align, see
14
14
  `~matplotlib.ticker.LinearLocator`.
15
-
15
+ ![グラフ](d5d69dc0dbafa075e6733010c6925720.png)
16
16
  Returns
17
17
  -------
18
18
  ax_twin : Axes
@@ -22,4 +22,57 @@
22
22
  -----
23
23
  For those who are 'picking' artists while using twinx, pick
24
24
  events are only called for the artists in the top-most axes.
25
- ```
25
+ ```
26
+
27
+ ラベルとか手抜きなので追加して下さい。
28
+
29
+ 仮データ作成
30
+ ```python
31
+ import pandas as pd
32
+ import io
33
+
34
+ indata1 = '''日付け 終値
35
+ 2020-03-02 1015
36
+ 2020-03-03 1025
37
+ 2020-03-04 1050
38
+ 2020-03-05 999
39
+ 2020-03-06 1080'''
40
+
41
+ with io.StringIO(indata1) as f:
42
+ df1 = pd.read_csv(f, sep=' +', engine='python', parse_dates=[0])
43
+
44
+ indata2 = '''日付け 終値
45
+ 2020-03-02 3056
46
+ 2020-03-03 3523
47
+ 2020-03-04 4024
48
+ 2020-03-05 5095
49
+ 2020-03-06 3800'''
50
+
51
+ with io.StringIO(indata2) as f:
52
+ df2 = pd.read_csv(f, sep=' +', engine='python', parse_dates=[0])
53
+ ```
54
+
55
+ 表示コード
56
+ ```python
57
+ import pandas as pd
58
+ import datetime
59
+ import matplotlib.pyplot as plt
60
+ import matplotlib.dates as mdates
61
+ import numpy as np
62
+
63
+ Date = df1["日付け"].values.astype(np.datetime64)
64
+ EP1 = df1['終値'].values
65
+ EP2 = df2['終値'].values
66
+
67
+ fig = plt.figure(figsize=(30,10))
68
+ ax = fig.add_subplot(111)
69
+ ax.plot(Date, EP1,'C0')
70
+ ax.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d'))
71
+ ax2 = ax.twinx()
72
+ ax2.plot(Date, EP2,'C1')
73
+ ax2.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d'))
74
+ plt.show()
75
+ ```
76
+ 実行結果
77
+
78
+ ![イメージ説明](182f49caef778a0e3bb93fa2d7915033.png)

1

コード追加

2021/03/21 14:09

投稿

ppaul
ppaul

スコア24672

answer CHANGED
@@ -1,3 +1,25 @@
1
1
  ax2 = ax1.twinx()
2
2
 
3
- が同じx軸を使うグラフを作っています。
3
+ が同じx軸を使うグラフを作っています。
4
+
5
+ ```python
6
+ >>> print(ax1.twinx.__doc__)
7
+
8
+ Create a twin Axes sharing the xaxis.
9
+
10
+ Create a new Axes with an invisible x-axis and an independent
11
+ y-axis positioned opposite to the original one (i.e. at right). The
12
+ x-axis autoscale setting will be inherited from the original
13
+ Axes. To ensure that the tick marks of both y-axes align, see
14
+ `~matplotlib.ticker.LinearLocator`.
15
+
16
+ Returns
17
+ -------
18
+ ax_twin : Axes
19
+ The newly created Axes instance
20
+
21
+ Notes
22
+ -----
23
+ For those who are 'picking' artists while using twinx, pick
24
+ events are only called for the artists in the top-most axes.
25
+ ```