py4project 0.3.1__tar.gz → 0.3.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {py4project-0.3.1 → py4project-0.3.2}/CHANGES.md +22 -0
- {py4project-0.3.1 → py4project-0.3.2}/PKG-INFO +3 -1
- {py4project-0.3.1 → py4project-0.3.2}/README.md +2 -0
- {py4project-0.3.1 → py4project-0.3.2}/pyproject.toml +1 -1
- {py4project-0.3.1 → py4project-0.3.2}/src/py4project/__init__.py +1 -1
- {py4project-0.3.1 → py4project-0.3.2}/src/py4project/core.py +129 -12
- {py4project-0.3.1 → py4project-0.3.2}/tests/test_basic.py +130 -0
- {py4project-0.3.1 → py4project-0.3.2}/.github/workflows/publish.yml +0 -0
- {py4project-0.3.1 → py4project-0.3.2}/.github/workflows/test.yml +0 -0
- {py4project-0.3.1 → py4project-0.3.2}/.gitignore +0 -0
- {py4project-0.3.1 → py4project-0.3.2}/LICENSE +0 -0
- {py4project-0.3.1 → py4project-0.3.2}/examples/data_project.csv +0 -0
- {py4project-0.3.1 → py4project-0.3.2}/examples/example.ipynb +0 -0
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# 変更履歴
|
|
2
2
|
|
|
3
|
+
## 0.3.2(2026-11-23)
|
|
4
|
+
|
|
5
|
+
### 追加
|
|
6
|
+
|
|
7
|
+
- 作図関数6つ(`regression_plot`、`scatter_plot`、`bar_plot`、`box_plot`、
|
|
8
|
+
`line_plot`、`pie_plot`)に `figsize` を追加した。図の大きさを
|
|
9
|
+
(幅, 高さ) のインチで指定する。例: `figsize=(8, 5)`
|
|
10
|
+
キーワード指定のみなので、これまでの呼び出し方はそのまま使える。
|
|
11
|
+
- `figsize` の間違い(`figsize=8`、`figsize=(0, 5)`、`figsize=('8', '5')` など)を
|
|
12
|
+
日本語のエラーで知らせるようにした。単位付きの指定 `(8, 5, 'cm')` は
|
|
13
|
+
matplotlib のバージョンによって使えたり使えなかったりするため受け付けず、
|
|
14
|
+
インチへの換算方法を案内する。
|
|
15
|
+
|
|
16
|
+
### 変更
|
|
17
|
+
|
|
18
|
+
- `bar_plot` の標準の高さを、棒の数に合わせて自動で決めるようにした
|
|
19
|
+
(1本あたり0.3インチ+余白1インチ、最低3インチ)。17産業では約6.1インチで
|
|
20
|
+
これまでとほぼ同じ。47都道府県では約15インチになり、名前が重ならない。
|
|
21
|
+
以前は (9, 6) に固定で、47都道府県では縦軸の名前が重なっていた。
|
|
22
|
+
- `box_plot` も他の作図関数と同じく余白を詰めて描くようにした(`tight_layout`)。
|
|
23
|
+
描画領域が少し広くなる。
|
|
24
|
+
|
|
3
25
|
## 0.3.1(2026-09-16)
|
|
4
26
|
|
|
5
27
|
### 追加
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: py4project
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.2
|
|
4
4
|
Summary: 授業向けの回帰分析・散布図・折れ線グラフ・バープロット・ボックスプロット・円グラフのユーティリティ
|
|
5
5
|
Project-URL: Homepage, https://github.com/Py4Project/py4project
|
|
6
6
|
Project-URL: Repository, https://github.com/Py4Project/py4project
|
|
@@ -42,6 +42,8 @@ pip install py4project
|
|
|
42
42
|
- `line_plot(x, y, data, ...)` — 折れ線グラフ(`y` にリストを渡すと複数系列を重ねて表示)
|
|
43
43
|
- `pie_plot(x, data, label_col, ...)` — 円グラフ(パーセント表示。`top` や `other` で小さい項目をまとめられます)
|
|
44
44
|
|
|
45
|
+
作図関数はどれも `figsize=(幅, 高さ)` で図の大きさを指定できます(単位はインチ、1インチ=2.54cm)。
|
|
46
|
+
|
|
45
47
|
## 使用例
|
|
46
48
|
|
|
47
49
|
```python
|
|
@@ -18,6 +18,8 @@ pip install py4project
|
|
|
18
18
|
- `line_plot(x, y, data, ...)` — 折れ線グラフ(`y` にリストを渡すと複数系列を重ねて表示)
|
|
19
19
|
- `pie_plot(x, data, label_col, ...)` — 円グラフ(パーセント表示。`top` や `other` で小さい項目をまとめられます)
|
|
20
20
|
|
|
21
|
+
作図関数はどれも `figsize=(幅, 高さ)` で図の大きさを指定できます(単位はインチ、1インチ=2.54cm)。
|
|
22
|
+
|
|
21
23
|
## 使用例
|
|
22
24
|
|
|
23
25
|
```python
|
|
@@ -169,6 +169,77 @@ def _resolve_legend(columns, legend, func_name, arg_name):
|
|
|
169
169
|
return legend
|
|
170
170
|
|
|
171
171
|
|
|
172
|
+
_FIGSIZE_HINT = ('figsize=(8, 5) のように、(幅, 高さ) をインチで指定してください。'
|
|
173
|
+
'(1インチ=2.54cm)')
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _check_figsize(figsize):
|
|
177
|
+
"""figsize が (幅, 高さ) の正しい形かを確認する(内部用)
|
|
178
|
+
|
|
179
|
+
matplotlib にそのまま渡すと、間違いが英語のエラーになったり、
|
|
180
|
+
(0, 5) のように描く時点まで気づかれなかったりするため、先に確かめる。"""
|
|
181
|
+
if figsize is None:
|
|
182
|
+
return
|
|
183
|
+
|
|
184
|
+
if isinstance(figsize, str) or not isinstance(figsize, (tuple, list, np.ndarray)):
|
|
185
|
+
raise TypeError(_err(
|
|
186
|
+
'figsize の指定のしかたが正しくありません。',
|
|
187
|
+
f'figsize に {figsize!r} が指定されました。'
|
|
188
|
+
'図の大きさは (幅, 高さ) の2つの数で表します。',
|
|
189
|
+
_FIGSIZE_HINT))
|
|
190
|
+
|
|
191
|
+
values = list(figsize)
|
|
192
|
+
|
|
193
|
+
if len(values) == 3 and isinstance(values[2], str):
|
|
194
|
+
raise ValueError(_err(
|
|
195
|
+
'figsize に単位は指定できません。',
|
|
196
|
+
f'figsize に {tuple(values)!r} が指定されました。'
|
|
197
|
+
'py4project では図の大きさをインチで指定します。'
|
|
198
|
+
'(単位の指定は matplotlib のバージョンによって'
|
|
199
|
+
'使えたり使えなかったりするため、受け付けていません)',
|
|
200
|
+
'cm で考えたいときは 2.54 で割ってインチにしてください。'
|
|
201
|
+
' 例: 幅20cm・高さ12cm → figsize=(20/2.54, 12/2.54)'))
|
|
202
|
+
|
|
203
|
+
if len(values) != 2:
|
|
204
|
+
raise ValueError(_err(
|
|
205
|
+
'figsize には2つの数を指定してください。',
|
|
206
|
+
f'figsize に{len(values)}個の値が指定されました。',
|
|
207
|
+
_FIGSIZE_HINT))
|
|
208
|
+
|
|
209
|
+
for v in values:
|
|
210
|
+
if isinstance(v, (bool, np.bool_)) or not isinstance(
|
|
211
|
+
v, (int, float, np.integer, np.floating)):
|
|
212
|
+
raise TypeError(_err(
|
|
213
|
+
'figsize には数を指定してください。',
|
|
214
|
+
f'figsize に {v!r} が含まれています。'
|
|
215
|
+
'文字列(例:"8")ではなく、数(例:8)で書きます。',
|
|
216
|
+
_FIGSIZE_HINT))
|
|
217
|
+
|
|
218
|
+
if not all(np.isfinite(v) and v > 0 for v in values):
|
|
219
|
+
raise ValueError(_err(
|
|
220
|
+
'figsize には0より大きい数を指定してください。',
|
|
221
|
+
f'figsize に {tuple(values)!r} が指定されました。'
|
|
222
|
+
'幅や高さが0以下だと図を描けません。',
|
|
223
|
+
_FIGSIZE_HINT))
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def _new_figure(figsize, default=None):
|
|
227
|
+
"""図をつくる(内部用)
|
|
228
|
+
|
|
229
|
+
figsize が省略されたときは default を使う。
|
|
230
|
+
default も None なら matplotlib の標準の大きさになる。"""
|
|
231
|
+
size = default if figsize is None else tuple(float(v) for v in figsize)
|
|
232
|
+
return plt.subplots(figsize=size)
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def _bar_figsize(n_rows):
|
|
236
|
+
"""bar_plot の標準の大きさ(内部用)
|
|
237
|
+
|
|
238
|
+
1本あたり0.3インチに余白1インチを足した高さにする。
|
|
239
|
+
17行で約6インチ(0.3.1までの固定値とほぼ同じ)、47行で約15インチになる。"""
|
|
240
|
+
return (9.0, max(3.0, 0.3 * n_rows + 1.0))
|
|
241
|
+
|
|
242
|
+
|
|
172
243
|
def _label(value):
|
|
173
244
|
"""マーカー横に表示する文字列を安全に作る(内部用)
|
|
174
245
|
|
|
@@ -410,7 +481,8 @@ def regression(x, y, data, robust=False, alpha=0.05, stars=True):
|
|
|
410
481
|
|
|
411
482
|
|
|
412
483
|
def regression_plot(x=None, y=None, data=None, title=None, xlabel=None, ylabel=None,
|
|
413
|
-
line=True, label_col=None, robust=False, *, ols_res=None
|
|
484
|
+
line=True, label_col=None, robust=False, *, ols_res=None,
|
|
485
|
+
figsize=None):
|
|
414
486
|
"""散布図に回帰直線を重ねて表示する。
|
|
415
487
|
|
|
416
488
|
引数
|
|
@@ -445,6 +517,12 @@ def regression_plot(x=None, y=None, data=None, title=None, xlabel=None, ylabel=N
|
|
|
445
517
|
図を描きます。この場合 x、y、data の指定は不要です。
|
|
446
518
|
label_col と robust は一緒に使えません。
|
|
447
519
|
robust を変えたい場合は regression の側で指定してください。
|
|
520
|
+
figsize : (幅, 高さ)(キーワード指定のみ)
|
|
521
|
+
図の大きさをインチで指定します(1インチ=2.54cm)。
|
|
522
|
+
例: figsize=(8, 5)
|
|
523
|
+
省略すると matplotlib の標準の大きさ(通常は (6.4, 4.8))になります。
|
|
524
|
+
図を小さくしても文字や点の大きさは変わらないため、
|
|
525
|
+
名前を表示していると重なりやすくなります。
|
|
448
526
|
|
|
449
527
|
使用例
|
|
450
528
|
------
|
|
@@ -460,6 +538,7 @@ def regression_plot(x=None, y=None, data=None, title=None, xlabel=None, ylabel=N
|
|
|
460
538
|
|
|
461
539
|
欠損値のある行を除いた結果、データが3行に満たない場合はエラーになります。
|
|
462
540
|
"""
|
|
541
|
+
_check_figsize(figsize)
|
|
463
542
|
if ols_res is not None:
|
|
464
543
|
# ---- regression の結果をそのまま使う ----
|
|
465
544
|
# 使えない組み合わせを先に弾く
|
|
@@ -537,7 +616,7 @@ def regression_plot(x=None, y=None, data=None, title=None, xlabel=None, ylabel=N
|
|
|
537
616
|
if ylabel is None:
|
|
538
617
|
ylabel = y_name
|
|
539
618
|
|
|
540
|
-
fig, ax =
|
|
619
|
+
fig, ax = _new_figure(figsize)
|
|
541
620
|
ax.scatter(_x, _y, s=60, color='steelblue', zorder=3)
|
|
542
621
|
|
|
543
622
|
if label_col is not None and plot_data is not None:
|
|
@@ -576,7 +655,7 @@ def regression_plot(x=None, y=None, data=None, title=None, xlabel=None, ylabel=N
|
|
|
576
655
|
|
|
577
656
|
def scatter_plot(x, y, data, title=None, xlabel=None, ylabel=None,
|
|
578
657
|
line=True, label_col=None,
|
|
579
|
-
xlog=False, ylog=False, xylog=False):
|
|
658
|
+
xlog=False, ylog=False, xylog=False, *, figsize=None):
|
|
580
659
|
"""散布図にトレンド線を重ねて表示する。
|
|
581
660
|
|
|
582
661
|
regression_plotとよく似ていますが、こちらは推定式を表示せず、
|
|
@@ -611,6 +690,12 @@ def scatter_plot(x, y, data, title=None, xlabel=None, ylabel=None,
|
|
|
611
690
|
Trueで縦軸の変数を常用対数(log10)に変換します。
|
|
612
691
|
xylog : True または False
|
|
613
692
|
Trueで横軸と縦軸の両方を常用対数に変換します。
|
|
693
|
+
figsize : (幅, 高さ)(キーワード指定のみ)
|
|
694
|
+
図の大きさをインチで指定します(1インチ=2.54cm)。
|
|
695
|
+
例: figsize=(8, 5)
|
|
696
|
+
省略すると matplotlib の標準の大きさ(通常は (6.4, 4.8))になります。
|
|
697
|
+
図を小さくしても文字や点の大きさは変わらないため、
|
|
698
|
+
名前を表示していると重なりやすくなります。
|
|
614
699
|
|
|
615
700
|
使用例
|
|
616
701
|
------
|
|
@@ -624,6 +709,7 @@ def scatter_plot(x, y, data, title=None, xlabel=None, ylabel=None,
|
|
|
624
709
|
トレンド線を引く場合(line=True)、欠損値を除いたあとのデータが
|
|
625
710
|
3行に満たないとエラーになります。
|
|
626
711
|
"""
|
|
712
|
+
_check_figsize(figsize)
|
|
627
713
|
_check_dataframe(data, example='scatter_plot("事業所数", "従業者数", df)')
|
|
628
714
|
_check_x_not_y([x], y)
|
|
629
715
|
_check_column(data, x, 'x')
|
|
@@ -663,7 +749,7 @@ def scatter_plot(x, y, data, title=None, xlabel=None, ylabel=None,
|
|
|
663
749
|
_x = data[x_plot]
|
|
664
750
|
_y = data[y_plot]
|
|
665
751
|
|
|
666
|
-
fig, ax =
|
|
752
|
+
fig, ax = _new_figure(figsize)
|
|
667
753
|
ax.scatter(_x, _y, s=60, color='steelblue', zorder=3)
|
|
668
754
|
|
|
669
755
|
if label_col is not None:
|
|
@@ -706,7 +792,8 @@ _COLOR_MAP = {
|
|
|
706
792
|
|
|
707
793
|
|
|
708
794
|
def bar_plot(x, data, title=None, xlabel=None, unit=1000,
|
|
709
|
-
sort_by=None, label_col=None, ascending=None, legend=None
|
|
795
|
+
sort_by=None, label_col=None, ascending=None, legend=None,
|
|
796
|
+
*, figsize=None):
|
|
710
797
|
"""横向きの棒グラフを表示する。
|
|
711
798
|
|
|
712
799
|
産業ごとの値を横に並べた棒グラフを描きます。
|
|
@@ -747,6 +834,11 @@ def bar_plot(x, data, title=None, xlabel=None, unit=1000,
|
|
|
747
834
|
凡例に表示する名前。xと同じ順番、同じ数で指定します。
|
|
748
835
|
省略すると列名がそのまま使われます。
|
|
749
836
|
例: x=['w2016', 'w2021'] に対して legend=['2016年', '2021年']
|
|
837
|
+
figsize : (幅, 高さ)(キーワード指定のみ)
|
|
838
|
+
図の大きさをインチで指定します(1インチ=2.54cm)。
|
|
839
|
+
例: figsize=(9, 12)
|
|
840
|
+
省略すると幅は9インチ、高さは棒の数に合わせて自動で決まります
|
|
841
|
+
(17産業なら約6インチ、47都道府県なら約15インチ)。
|
|
750
842
|
|
|
751
843
|
使用例
|
|
752
844
|
------
|
|
@@ -758,6 +850,7 @@ def bar_plot(x, data, title=None, xlabel=None, unit=1000,
|
|
|
758
850
|
>>> bar_plot(['w2016', 'w2021'], df, label_col='産業',
|
|
759
851
|
... legend=['2016年', '2021年'])
|
|
760
852
|
"""
|
|
853
|
+
_check_figsize(figsize)
|
|
761
854
|
_check_dataframe(data, example='bar_plot("従業者数", df)')
|
|
762
855
|
|
|
763
856
|
if isinstance(x, pd.DataFrame):
|
|
@@ -812,7 +905,8 @@ def bar_plot(x, data, title=None, xlabel=None, unit=1000,
|
|
|
812
905
|
data = data.sort_values(sort_by,
|
|
813
906
|
ascending=False if ascending is None else ascending)
|
|
814
907
|
|
|
815
|
-
|
|
908
|
+
# 高さは棒の数に合わせる。固定にすると47都道府県で名前が重なる。
|
|
909
|
+
fig, ax = _new_figure(figsize, default=_bar_figsize(len(data)))
|
|
816
910
|
|
|
817
911
|
n = len(x_list)
|
|
818
912
|
y = np.arange(len(data))
|
|
@@ -844,7 +938,8 @@ def bar_plot(x, data, title=None, xlabel=None, unit=1000,
|
|
|
844
938
|
plt.show()
|
|
845
939
|
|
|
846
940
|
|
|
847
|
-
def box_plot(x, data, title=None, label_col=None, xlabel=None, xlog=False
|
|
941
|
+
def box_plot(x, data, title=None, label_col=None, xlabel=None, xlog=False,
|
|
942
|
+
*, figsize=None):
|
|
848
943
|
"""箱ひげ図を表示する。
|
|
849
944
|
|
|
850
945
|
データの散らばり方(中央値・四分位数・外れ値)を確認するための図です。
|
|
@@ -873,6 +968,12 @@ def box_plot(x, data, title=None, label_col=None, xlabel=None, xlog=False):
|
|
|
873
968
|
省略するとxの列名がそのまま使われます。
|
|
874
969
|
xlog : True または False
|
|
875
970
|
Trueでデータを常用対数(log10)に変換してから箱ひげ図を描きます。
|
|
971
|
+
figsize : (幅, 高さ)(キーワード指定のみ)
|
|
972
|
+
図の大きさをインチで指定します(1インチ=2.54cm)。
|
|
973
|
+
例: figsize=(8, 5)
|
|
974
|
+
省略すると matplotlib の標準の大きさ(通常は (6.4, 4.8))になります。
|
|
975
|
+
図を小さくしても文字や点の大きさは変わらないため、
|
|
976
|
+
名前を表示していると重なりやすくなります。
|
|
876
977
|
|
|
877
978
|
使用例
|
|
878
979
|
------
|
|
@@ -886,6 +987,7 @@ def box_plot(x, data, title=None, label_col=None, xlabel=None, xlog=False):
|
|
|
886
987
|
xlog=True にする場合、対象の列に0以下の値が含まれているとエラーになります。
|
|
887
988
|
box_plot に ylog や xylog はありません。
|
|
888
989
|
"""
|
|
990
|
+
_check_figsize(figsize)
|
|
889
991
|
_check_dataframe(data, example='box_plot("従業者数", df)')
|
|
890
992
|
|
|
891
993
|
if isinstance(x, pd.DataFrame):
|
|
@@ -926,7 +1028,7 @@ def box_plot(x, data, title=None, label_col=None, xlabel=None, xlog=False):
|
|
|
926
1028
|
|
|
927
1029
|
data = data.copy()
|
|
928
1030
|
|
|
929
|
-
fig, ax =
|
|
1031
|
+
fig, ax = _new_figure(figsize)
|
|
930
1032
|
|
|
931
1033
|
if xlog:
|
|
932
1034
|
x_log = [s + '_log' for s in x]
|
|
@@ -960,11 +1062,12 @@ def box_plot(x, data, title=None, label_col=None, xlabel=None, xlog=False):
|
|
|
960
1062
|
|
|
961
1063
|
if title:
|
|
962
1064
|
ax.set_title(title)
|
|
1065
|
+
plt.tight_layout()
|
|
963
1066
|
plt.show()
|
|
964
1067
|
|
|
965
1068
|
|
|
966
1069
|
def line_plot(x, y, data, title=None, xlabel=None, ylabel=None, legend=None,
|
|
967
|
-
xlog=False, ylog=False, xylog=False):
|
|
1070
|
+
xlog=False, ylog=False, xylog=False, *, figsize=None):
|
|
968
1071
|
"""折れ線グラフを表示する。
|
|
969
1072
|
|
|
970
1073
|
年や四半期のように順序のある変数を横軸にとり、値の推移を線で結びます。
|
|
@@ -1005,6 +1108,12 @@ def line_plot(x, y, data, title=None, xlabel=None, ylabel=None, legend=None,
|
|
|
1005
1108
|
Trueで縦軸の変数を常用対数(log10)に変換します。
|
|
1006
1109
|
xylog : True または False
|
|
1007
1110
|
Trueで横軸と縦軸の両方を常用対数に変換します。
|
|
1111
|
+
figsize : (幅, 高さ)(キーワード指定のみ)
|
|
1112
|
+
図の大きさをインチで指定します(1インチ=2.54cm)。
|
|
1113
|
+
例: figsize=(8, 5)
|
|
1114
|
+
省略すると matplotlib の標準の大きさ(通常は (6.4, 4.8))になります。
|
|
1115
|
+
図を小さくしても文字や点の大きさは変わらないため、
|
|
1116
|
+
名前を表示していると重なりやすくなります。
|
|
1008
1117
|
|
|
1009
1118
|
使用例
|
|
1010
1119
|
------
|
|
@@ -1017,6 +1126,7 @@ def line_plot(x, y, data, title=None, xlabel=None, ylabel=None, legend=None,
|
|
|
1017
1126
|
対数をとる列に0以下の値が含まれているとエラーになります。
|
|
1018
1127
|
横軸を対数にする場合、xは数値の列である必要があります。
|
|
1019
1128
|
"""
|
|
1129
|
+
_check_figsize(figsize)
|
|
1020
1130
|
_check_dataframe(data, example='line_plot("年", "従業者数", df)')
|
|
1021
1131
|
|
|
1022
1132
|
if isinstance(y, str):
|
|
@@ -1067,7 +1177,7 @@ def line_plot(x, y, data, title=None, xlabel=None, ylabel=None, legend=None,
|
|
|
1067
1177
|
else:
|
|
1068
1178
|
y_plot.append(col)
|
|
1069
1179
|
|
|
1070
|
-
fig, ax =
|
|
1180
|
+
fig, ax = _new_figure(figsize)
|
|
1071
1181
|
|
|
1072
1182
|
n = len(y)
|
|
1073
1183
|
for i, (col, name) in enumerate(zip(y_plot, names)):
|
|
@@ -1093,7 +1203,7 @@ def line_plot(x, y, data, title=None, xlabel=None, ylabel=None, legend=None,
|
|
|
1093
1203
|
|
|
1094
1204
|
|
|
1095
1205
|
def pie_plot(x, data, label_col, title=None, legend=False,
|
|
1096
|
-
other=None, other_label='その他', top=None):
|
|
1206
|
+
other=None, other_label='その他', top=None, *, figsize=None):
|
|
1097
1207
|
"""円グラフを表示する。
|
|
1098
1208
|
|
|
1099
1209
|
全体に占める割合を扇形の大きさで表します。
|
|
@@ -1130,6 +1240,12 @@ def pie_plot(x, data, label_col, title=None, legend=False,
|
|
|
1130
1240
|
例: top=5 なら、上位5項目とその他が表示されます。
|
|
1131
1241
|
残した項目は値の大きい順に並びます。
|
|
1132
1242
|
other と同時には使えません。
|
|
1243
|
+
figsize : (幅, 高さ)(キーワード指定のみ)
|
|
1244
|
+
図の大きさをインチで指定します(1インチ=2.54cm)。
|
|
1245
|
+
例: figsize=(8, 5)
|
|
1246
|
+
省略すると matplotlib の標準の大きさ(通常は (6.4, 4.8))になります。
|
|
1247
|
+
legend=True のときに図の幅を小さくすると、凡例に場所をとられて
|
|
1248
|
+
円が小さくなります。
|
|
1133
1249
|
|
|
1134
1250
|
使用例
|
|
1135
1251
|
------
|
|
@@ -1147,6 +1263,7 @@ def pie_plot(x, data, label_col, title=None, legend=False,
|
|
|
1147
1263
|
項目が多いと名前が重なって読みにくくなります。
|
|
1148
1264
|
目安として7項目程度までにするか、top や other でまとめてください。
|
|
1149
1265
|
"""
|
|
1266
|
+
_check_figsize(figsize)
|
|
1150
1267
|
_check_dataframe(data, example='pie_plot("従業者数", df, "産業")')
|
|
1151
1268
|
_check_column(data, x, 'x')
|
|
1152
1269
|
_check_column(data, label_col, 'label_col')
|
|
@@ -1251,7 +1368,7 @@ def pie_plot(x, data, label_col, title=None, legend=False,
|
|
|
1251
1368
|
if to_merge:
|
|
1252
1369
|
colors[-1] = '#9E9E9E'
|
|
1253
1370
|
|
|
1254
|
-
fig, ax =
|
|
1371
|
+
fig, ax = _new_figure(figsize)
|
|
1255
1372
|
|
|
1256
1373
|
if legend:
|
|
1257
1374
|
# 凡例を出すときは、扇形の横の名前を省いて重複を避ける
|
|
@@ -1179,3 +1179,133 @@ def test_equation_sign_via_ols_res():
|
|
|
1179
1179
|
title = matplotlib.pyplot.gcf().axes[0].get_title()
|
|
1180
1180
|
matplotlib.pyplot.close("all")
|
|
1181
1181
|
assert "+ -" not in title
|
|
1182
|
+
|
|
1183
|
+
|
|
1184
|
+
# ============================================================
|
|
1185
|
+
# figsize(0.3.2)
|
|
1186
|
+
# ============================================================
|
|
1187
|
+
|
|
1188
|
+
_PLOT_FUNCS = ["regression_plot", "scatter_plot", "bar_plot",
|
|
1189
|
+
"box_plot", "line_plot", "pie_plot"]
|
|
1190
|
+
|
|
1191
|
+
|
|
1192
|
+
def _size():
|
|
1193
|
+
size = tuple(round(float(v), 2) for v in matplotlib.pyplot.gcf().get_size_inches())
|
|
1194
|
+
matplotlib.pyplot.close("all")
|
|
1195
|
+
return size
|
|
1196
|
+
|
|
1197
|
+
|
|
1198
|
+
def _draw_each(df, **kw):
|
|
1199
|
+
"""6つの作図関数を順に呼び、図の大きさを返す"""
|
|
1200
|
+
res = regression("全国成長効果", "産業構成効果", df)
|
|
1201
|
+
calls = {
|
|
1202
|
+
"regression_plot": lambda: regression_plot("全国成長効果", "産業構成効果", df, **kw),
|
|
1203
|
+
"regression_plot(ols_res)": lambda: regression_plot(ols_res=res, **kw),
|
|
1204
|
+
"scatter_plot": lambda: scatter_plot("全国成長効果", "産業構成効果", df, **kw),
|
|
1205
|
+
"bar_plot": lambda: bar_plot("全国成長効果", df, label_col="産業", **kw),
|
|
1206
|
+
"box_plot": lambda: box_plot("全国成長効果", df, **kw),
|
|
1207
|
+
"line_plot": lambda: line_plot("産業コード", "全国成長効果", df, **kw),
|
|
1208
|
+
"pie_plot": lambda: pie_plot("全国成長効果", df, "産業", **kw),
|
|
1209
|
+
}
|
|
1210
|
+
out = {}
|
|
1211
|
+
for name, call in calls.items():
|
|
1212
|
+
call()
|
|
1213
|
+
out[name] = _size()
|
|
1214
|
+
return out
|
|
1215
|
+
|
|
1216
|
+
|
|
1217
|
+
def test_figsize_is_keyword_only_in_every_plot_function():
|
|
1218
|
+
import inspect
|
|
1219
|
+
import py4project
|
|
1220
|
+
for name in _PLOT_FUNCS:
|
|
1221
|
+
p = inspect.signature(getattr(py4project, name)).parameters["figsize"]
|
|
1222
|
+
assert p.kind is inspect.Parameter.KEYWORD_ONLY, name
|
|
1223
|
+
assert p.default is None, name
|
|
1224
|
+
|
|
1225
|
+
|
|
1226
|
+
def test_figsize_is_applied(sample_df):
|
|
1227
|
+
for name, size in _draw_each(sample_df, figsize=(8, 5)).items():
|
|
1228
|
+
assert size == (8.0, 5.0), name
|
|
1229
|
+
|
|
1230
|
+
|
|
1231
|
+
def test_default_sizes_are_unchanged(sample_df):
|
|
1232
|
+
"""figsize を省略したときの大きさは 0.3.1 と同じ(bar_plot を除く)"""
|
|
1233
|
+
default = tuple(round(float(v), 2) for v in matplotlib.rcParams["figure.figsize"])
|
|
1234
|
+
for name, size in _draw_each(sample_df).items():
|
|
1235
|
+
if name == "bar_plot":
|
|
1236
|
+
continue
|
|
1237
|
+
assert size == default, name
|
|
1238
|
+
|
|
1239
|
+
|
|
1240
|
+
def test_figsize_accepts_list_and_numpy(sample_df):
|
|
1241
|
+
for v in ([8, 5], np.array([8, 5]), (np.int64(8), np.float64(5.0))):
|
|
1242
|
+
scatter_plot("全国成長効果", "産業構成効果", sample_df, figsize=v)
|
|
1243
|
+
assert _size() == (8.0, 5.0)
|
|
1244
|
+
|
|
1245
|
+
|
|
1246
|
+
@pytest.mark.parametrize("bad, exc, message", [
|
|
1247
|
+
(8, TypeError, "指定のしかた"),
|
|
1248
|
+
("8x5", TypeError, "指定のしかた"),
|
|
1249
|
+
(("8", "5"), TypeError, "数を指定"),
|
|
1250
|
+
((True, 5), TypeError, "数を指定"),
|
|
1251
|
+
((8,), ValueError, "2つの数"),
|
|
1252
|
+
((8, 5, 3), ValueError, "2つの数"),
|
|
1253
|
+
((8, 5, "cm"), ValueError, "単位は指定できません"),
|
|
1254
|
+
((0, 5), ValueError, "0より大きい"),
|
|
1255
|
+
((-8, 5), ValueError, "0より大きい"),
|
|
1256
|
+
((float("inf"), 5), ValueError, "0より大きい"),
|
|
1257
|
+
((float("nan"), 5), ValueError, "0より大きい"),
|
|
1258
|
+
])
|
|
1259
|
+
def test_invalid_figsize_gives_japanese_error(sample_df, bad, exc, message):
|
|
1260
|
+
with pytest.raises(exc, match=message):
|
|
1261
|
+
scatter_plot("全国成長効果", "産業構成効果", sample_df, figsize=bad)
|
|
1262
|
+
|
|
1263
|
+
|
|
1264
|
+
def test_invalid_figsize_is_checked_before_data(sample_df):
|
|
1265
|
+
"""figsize の間違いは、データの検査より先に知らせる"""
|
|
1266
|
+
with pytest.raises(ValueError, match="0より大きい"):
|
|
1267
|
+
bar_plot("存在しない列", sample_df, figsize=(0, 5))
|
|
1268
|
+
|
|
1269
|
+
|
|
1270
|
+
def test_cm_error_explains_conversion(sample_df):
|
|
1271
|
+
with pytest.raises(ValueError) as exc:
|
|
1272
|
+
box_plot("全国成長効果", sample_df, figsize=(20, 12, "cm"))
|
|
1273
|
+
assert "2.54" in str(exc.value)
|
|
1274
|
+
|
|
1275
|
+
|
|
1276
|
+
@pytest.mark.parametrize("n_rows, height", [(3, 3.0), (17, 6.1), (47, 15.1)])
|
|
1277
|
+
def test_bar_plot_height_follows_number_of_rows(n_rows, height):
|
|
1278
|
+
df = pd.DataFrame({"名": [f"地域{i:02d}" for i in range(n_rows)],
|
|
1279
|
+
"値": np.arange(n_rows) + 1.0})
|
|
1280
|
+
bar_plot("値", df, label_col="名", unit=1)
|
|
1281
|
+
assert _size() == (9.0, height)
|
|
1282
|
+
|
|
1283
|
+
|
|
1284
|
+
def test_bar_plot_47_rows_labels_do_not_overlap():
|
|
1285
|
+
df = pd.DataFrame({"名": [f"都道府県{i:02d}" for i in range(47)],
|
|
1286
|
+
"値": np.arange(47) + 1.0})
|
|
1287
|
+
bar_plot("値", df, label_col="名", unit=1)
|
|
1288
|
+
fig = matplotlib.pyplot.gcf()
|
|
1289
|
+
fig.canvas.draw()
|
|
1290
|
+
r = fig.canvas.get_renderer()
|
|
1291
|
+
boxes = [t.get_window_extent(r) for t in fig.axes[0].get_yticklabels()]
|
|
1292
|
+
overlaps = sum(1 for i in range(len(boxes)) for j in range(i + 1, len(boxes))
|
|
1293
|
+
if boxes[i].overlaps(boxes[j]))
|
|
1294
|
+
matplotlib.pyplot.close("all")
|
|
1295
|
+
assert overlaps == 0
|
|
1296
|
+
|
|
1297
|
+
|
|
1298
|
+
def test_bar_plot_figsize_overrides_auto_height():
|
|
1299
|
+
df = pd.DataFrame({"名": [f"地域{i:02d}" for i in range(47)],
|
|
1300
|
+
"値": np.arange(47) + 1.0})
|
|
1301
|
+
bar_plot("値", df, label_col="名", unit=1, figsize=(9, 12))
|
|
1302
|
+
assert _size() == (9.0, 12.0)
|
|
1303
|
+
|
|
1304
|
+
|
|
1305
|
+
def test_box_plot_uses_tight_layout(sample_df):
|
|
1306
|
+
"""box_plot も他の関数と同じく余白を詰める"""
|
|
1307
|
+
box_plot("全国成長効果", sample_df)
|
|
1308
|
+
ax = matplotlib.pyplot.gcf().axes[0]
|
|
1309
|
+
width = ax.get_position().width
|
|
1310
|
+
matplotlib.pyplot.close("all")
|
|
1311
|
+
assert width > 0.85
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|