回答編集履歴

5

修正

2020/07/18 13:38

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -90,6 +90,34 @@
90
90
 
91
91
 
92
92
 
93
+ このように matplotlibrc に書いておくと、`import matplotlib.pyplot as plt` した段階で記述した項目が反映されています。
94
+
95
+
96
+
97
+ ```python
98
+
99
+ import matplotlib.pyplot as plt
100
+
101
+
102
+
103
+ print(plt.rcParams["font.family"]) # ['sans-serif']
104
+
105
+ print(plt.rcParams["xtick.direction"]) # in
106
+
107
+ print(plt.rcParams["ytick.direction"]) # in
108
+
109
+ print(plt.rcParams["xtick.major.width"]) # 2.0
110
+
111
+ print(plt.rcParams["ytick.major.width"]) # 2.0
112
+
113
+ print(plt.rcParams["font.size"]) # 16.0
114
+
115
+ print(plt.rcParams["axes.linewidth"]) # 2.0
116
+
117
+ ```
118
+
119
+
120
+
93
121
  上記記述で以下のコードを実行すると、質問と同じ図が作成できました。
94
122
 
95
123
  設定項目にないものは設定できないので、目盛りの書式等はコードから除くことはできません。

4

修正

2020/07/18 13:38

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -43,3 +43,83 @@
43
43
  shutil.copyfile(default_config_path, config_path)
44
44
 
45
45
  ```
46
+
47
+
48
+
49
+ ## 追記
50
+
51
+
52
+
53
+ `matplotlibrc` は matplotlib のデフォルト設定を記述するファイルです。
54
+
55
+ Python のスクリプトをペタっと貼るのではなく、設定ファイルのフォーマットに従って、設定を追記します。どのように書けばいいかはコメントアウトされている各種設定項目に書いてあります。
56
+
57
+
58
+
59
+ 例えば、質問の内容だと以下を追記すればよいです。
60
+
61
+
62
+
63
+ ```
64
+
65
+ # Custom Settings
66
+
67
+ font.family : sans-serif
68
+
69
+ xtick.direction : in
70
+
71
+ ytick.direction : in
72
+
73
+ xtick.major.width : 2.0
74
+
75
+ ytick.major.width : 2.0
76
+
77
+ font.size : 16
78
+
79
+ axes.linewidth : 2.0
80
+
81
+ figure.figsize : 6.0, 6.0
82
+
83
+ figure.autolayout : True
84
+
85
+ savefig.transparent: True
86
+
87
+ savefig.dpi : 300
88
+
89
+ ```
90
+
91
+
92
+
93
+ 上記記述で以下のコードを実行すると、質問と同じ図が作成できました。
94
+
95
+ 設定項目にないものは設定できないので、目盛りの書式等はコードから除くことはできません。
96
+
97
+
98
+
99
+ ```
100
+
101
+ import matplotlib.pyplot as plt
102
+
103
+
104
+
105
+ plt.rcParams["font.size"] = 16 # Notebook の場合は必要
106
+
107
+
108
+
109
+ fig, ax = plt.subplots(figsize=(6, 6))
110
+
111
+ ax.xaxis.set_major_formatter(plt.FormatStrFormatter("%.3f"))
112
+
113
+ ax.yaxis.set_major_formatter(plt.FormatStrFormatter("%.3f"))
114
+
115
+ plt.locator_params(axis="y", nbins=6)
116
+
117
+
118
+
119
+ plt.show()
120
+
121
+ ```
122
+
123
+
124
+
125
+ ![イメージ説明](d4e90dce12aa1b6ba01c8203a962ce70.jpeg)

3

修正

2020/07/18 13:33

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -24,8 +24,6 @@
24
24
 
25
25
  import matplotlib as mpl
26
26
 
27
- import matplotlib.pyplot as plt # これを呼ばないと matplotlib.font_manager にアクセスできない
28
-
29
27
 
30
28
 
31
29
  # 設定ファイルをコピーする。

2

修正

2020/07/18 11:29

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -7,3 +7,41 @@
7
7
 
8
8
 
9
9
  [matplotlib - コピペするだけで matplotlib を日本語化する方法](https://pystyle.info/japanize-matplotlib-just-by-copying-and-pasting/#outline__4)
10
+
11
+
12
+
13
+ ## 追記
14
+
15
+
16
+
17
+ ```python
18
+
19
+ import shutil
20
+
21
+ from pathlib import Path
22
+
23
+
24
+
25
+ import matplotlib as mpl
26
+
27
+ import matplotlib.pyplot as plt # これを呼ばないと matplotlib.font_manager にアクセスできない
28
+
29
+
30
+
31
+ # 設定ファイルをコピーする。
32
+
33
+ config_dir = Path(mpl.get_configdir())
34
+
35
+ config_dir.mkdir(exist_ok=True, parents=True)
36
+
37
+
38
+
39
+ default_config_path = Path(mpl.__file__).parent / "mpl-data/matplotlibrc"
40
+
41
+ config_path = config_dir / "matplotlibrc"
42
+
43
+ print(f"設定ファイルの場所 {config_path}")
44
+
45
+ shutil.copyfile(default_config_path, config_path)
46
+
47
+ ```

1

修正

2020/07/18 11:27

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  matplotlibrc はデフォルトでは存在しないので、以下のコードをコピペして実行すれば作成できます。
4
4
 
5
+ 作成場所は `<ホームディレクトリ>/.config/matplotlib/matplotlibrc` になるので、作成したあとにテキストエディタで適宜編集してください。
6
+
5
7
 
6
8
 
7
9
  [matplotlib - コピペするだけで matplotlib を日本語化する方法](https://pystyle.info/japanize-matplotlib-just-by-copying-and-pasting/#outline__4)