回答編集履歴
3
answer
CHANGED
@@ -21,10 +21,10 @@
|
|
21
21
|
pivot[area].plot.line(ax=ax[i])
|
22
22
|
ax[i].set_title(area)
|
23
23
|
ax[i].xaxis.set_major_locator(mdates.DayLocator(range((pivot.index.max()-pivot.index.min()).days + 2)))
|
24
|
-
ax[i].xaxis.set_major_formatter(mdates.DateFormatter('%Y/%m/%d'))
|
24
|
+
ax[i].xaxis.set_major_formatter(mdates.DateFormatter('%Y/%-m/%-d'))
|
25
25
|
ax[i].yaxis.set_major_locator(MaxNLocator(integer=True))
|
26
26
|
|
27
27
|
plt.tight_layout()
|
28
28
|
plt.show()
|
29
29
|
```
|
30
|
-

|
2
answer
CHANGED
@@ -5,27 +5,26 @@
|
|
5
5
|
from matplotlib import dates as mdates
|
6
6
|
from matplotlib.ticker import MaxNLocator
|
7
7
|
|
8
|
-
df_test = pd.DataFrame({'ID': [1111,
|
8
|
+
df_test = pd.DataFrame({'ID': [1111, 2222, 3333],
|
9
|
-
'日付': ['2022/1/1', '2022/1/1', '2022/1/2', '2022/1/2', '2022/1/2'],
|
10
|
-
'
|
9
|
+
'日付': ['2022/1/1', '2022/1/1', '2022/1/2'],
|
10
|
+
'種別': ['新規', '新規', '変更'],
|
11
|
-
'都道府県': ['東京', '神奈川', '大阪'
|
11
|
+
'都道府県': ['東京', '神奈川', '大阪'],
|
12
|
-
'エリア': ['関東', '関東', '大阪'
|
12
|
+
'エリア': ['関東', '関東', '大阪']})
|
13
13
|
df_test['日付'] = pd.to_datetime(df_test['日付']).dt.date
|
14
14
|
|
15
15
|
pivot = df_test.pivot_table(values=['ID'], index=['日付'], columns=['エリア', '種別'], aggfunc='size')
|
16
16
|
pivot = pivot.reindex(pd.MultiIndex.from_product(pivot.columns.levels), axis=1).fillna(0, downcast='infer')
|
17
|
-
print(pivot)
|
18
|
-
pivot.plot.line()
|
19
17
|
|
18
|
+
areas = pivot.columns.levels[0]
|
19
|
+
_, ax = plt.subplots(1, len(areas))
|
20
|
+
for i, area in enumerate(areas):
|
21
|
+
pivot[area].plot.line(ax=ax[i])
|
22
|
+
ax[i].set_title(area)
|
20
|
-
|
23
|
+
ax[i].xaxis.set_major_locator(mdates.DayLocator(range((pivot.index.max()-pivot.index.min()).days + 2)))
|
21
|
-
|
24
|
+
ax[i].xaxis.set_major_formatter(mdates.DateFormatter('%Y/%m/%d'))
|
22
|
-
|
25
|
+
ax[i].yaxis.set_major_locator(MaxNLocator(integer=True))
|
26
|
+
|
27
|
+
plt.tight_layout()
|
23
28
|
plt.show()
|
24
|
-
|
25
|
-
# エリア 大阪 関東
|
26
|
-
# 種別 変更 新規 変更 新規
|
27
|
-
# 日付
|
28
|
-
# 2022-01-01 0 0 1 1
|
29
|
-
# 2022-01-02 1 1 0 1
|
30
29
|
```
|
31
|
-

|
1
answer
CHANGED
@@ -3,28 +3,29 @@
|
|
3
3
|
import numpy as np
|
4
4
|
import matplotlib.pyplot as plt
|
5
5
|
from matplotlib import dates as mdates
|
6
|
+
from matplotlib.ticker import MaxNLocator
|
6
7
|
|
7
|
-
df_test = pd.DataFrame({'ID': [1111, 2222,
|
8
|
+
df_test = pd.DataFrame({'ID': [1111, 1111, 2222, 2222, 111],
|
9
|
+
'日付': ['2022/1/1', '2022/1/1', '2022/1/2', '2022/1/2', '2022/1/2'],
|
8
|
-
'
|
10
|
+
'種別': ['新規', '変更', '新規', '新規', '変更'],
|
9
|
-
'種別': ['新規', '新規', '変更'],
|
10
|
-
'都道府県': ['東京', '神奈川', '大阪'],
|
11
|
+
'都道府県': ['東京', '神奈川', '大阪', '東京', '大阪'],
|
11
|
-
'エリア': ['関東', '関東', '大阪']})
|
12
|
+
'エリア': ['関東', '関東', '大阪', '関東', '大阪']})
|
12
13
|
df_test['日付'] = pd.to_datetime(df_test['日付']).dt.date
|
13
14
|
|
14
|
-
pivot = df_test.pivot_table(values=['ID'], index=['日付'], columns=['エリア', '種別'], aggfunc='size')
|
15
|
+
pivot = df_test.pivot_table(values=['ID'], index=['日付'], columns=['エリア', '種別'], aggfunc='size')
|
15
|
-
pivot = pivot.reindex(pd.MultiIndex.from_product(pivot.columns.levels), axis=1).fillna(0)
|
16
|
+
pivot = pivot.reindex(pd.MultiIndex.from_product(pivot.columns.levels), axis=1).fillna(0, downcast='infer')
|
16
17
|
print(pivot)
|
18
|
+
pivot.plot.line()
|
17
19
|
|
18
|
-
pivot.plot.line()
|
19
20
|
plt.gca().xaxis.set_major_locator(mdates.DayLocator(range((pivot.index.max()-pivot.index.min()).days + 2)))
|
20
21
|
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y/%m/%d'))
|
22
|
+
plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True))
|
21
23
|
plt.show()
|
22
24
|
|
23
25
|
# エリア 大阪 関東
|
24
26
|
# 種別 変更 新規 変更 新規
|
25
27
|
# 日付
|
26
|
-
# 2022-01-01 0 0
|
28
|
+
# 2022-01-01 0 0 1 1
|
27
|
-
# 2022-01-02 1
|
29
|
+
# 2022-01-02 1 1 0 1
|
28
30
|
```
|
29
|
-
|
30
|
-

|