Elffuss BraidHub

kikocis/auto-code-rover public

main · 3.2 KiBRawPermalink
1[Bug]: DPI of a figure is doubled after unpickling on M1 Mac
2### Bug summary
3
4When a figure is unpickled, it's dpi is doubled. This behaviour happens every time and if done in a loop it can cause an `OverflowError`.
5
6### Code for reproduction
7
8```python
9import numpy as np
10import matplotlib
11import matplotlib.pyplot as plt
12import pickle
13import platform
14
15print(matplotlib.get_backend())
16print('Matplotlib ver:', matplotlib.__version__)
17print('Platform:', platform.platform())
18print('System:', platform.system())
19print('Release:', platform.release())
20print('Python ver:', platform.python_version())
21
22
23def dump_load_get_dpi(fig):
24 with open('sinus.pickle','wb') as file:
25 pickle.dump(fig, file)
26
27 with open('sinus.pickle', 'rb') as blob:
28 fig2 = pickle.load(blob)
29 return fig2, fig2.dpi
30
31
32def run():
33 fig = plt.figure()
34 x = np.linspace(0,2*np.pi)
35 y = np.sin(x)
36
37 for i in range(32):
38 print(f'{i}: {fig.dpi}')
39 fig, dpi = dump_load_get_dpi(fig)
40
41
42if __name__ == '__main__':
43 run()
44```
45
46
47### Actual outcome
48
49```
50MacOSX
51Matplotlib ver: 3.5.2
52Platform: macOS-12.4-arm64-arm-64bit
53System: Darwin
54Release: 21.5.0
55Python ver: 3.9.12
560: 200.0
571: 400.0
582: 800.0
593: 1600.0
604: 3200.0
615: 6400.0
626: 12800.0
637: 25600.0
648: 51200.0
659: 102400.0
6610: 204800.0
6711: 409600.0
6812: 819200.0
6913: 1638400.0
7014: 3276800.0
7115: 6553600.0
7216: 13107200.0
7317: 26214400.0
7418: 52428800.0
7519: 104857600.0
7620: 209715200.0
7721: 419430400.0
78Traceback (most recent call last):
79 File "/Users/wsykala/projects/matplotlib/example.py", line 34, in <module>
80 run()
81 File "/Users/wsykala/projects/matplotlib/example.py", line 30, in run
82 fig, dpi = dump_load_get_dpi(fig)
83 File "/Users/wsykala/projects/matplotlib/example.py", line 20, in dump_load_get_dpi
84 fig2 = pickle.load(blob)
85 File "/Users/wsykala/miniconda3/envs/playground/lib/python3.9/site-packages/matplotlib/figure.py", line 2911, in __setstate__
86 mgr = plt._backend_mod.new_figure_manager_given_figure(num, self)
87 File "/Users/wsykala/miniconda3/envs/playground/lib/python3.9/site-packages/matplotlib/backend_bases.py", line 3499, in new_figure_manager_given_figure
88 canvas = cls.FigureCanvas(figure)
89 File "/Users/wsykala/miniconda3/envs/playground/lib/python3.9/site-packages/matplotlib/backends/backend_macosx.py", line 32, in __init__
90 _macosx.FigureCanvas.__init__(self, width, height)
91OverflowError: signed integer is greater than maximum
92```
93
94### Expected outcome
95
96```
97MacOSX
98Matplotlib ver: 3.5.2
99Platform: macOS-12.4-arm64-arm-64bit
100System: Darwin
101Release: 21.5.0
102Python ver: 3.9.12
1030: 200.0
1041: 200.0
1052: 200.0
1063: 200.0
1074: 200.0
1085: 200.0
1096: 200.0
1107: 200.0
1118: 200.0
1129: 200.0
11310: 200.0
11411: 200.0
11512: 200.0
11613: 200.0
11714: 200.0
11815: 200.0
11916: 200.0
12017: 200.0
12118: 200.0
12219: 200.0
12320: 200.0
12421: 200.0
12522: 200.0
126```
127
128### Additional information
129
130This seems to happen only on M1 MacBooks and the version of python doesn't matter.
131
132### Operating system
133
134OS/X
135
136### Matplotlib Version
137
1383.5.2
139
140### Matplotlib Backend
141
142MacOSX
143
144### Python version
145
1463.9.12
147
148### Jupyter version
149
150_No response_
151
152### Installation
153
154pip