| 1 | [Bug]: DPI of a figure is doubled after unpickling on M1 Mac |
| 2 | ### Bug summary |
| 3 | |
| 4 | When 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 |
| 9 | import numpy as np |
| 10 | import matplotlib |
| 11 | import matplotlib.pyplot as plt |
| 12 | import pickle |
| 13 | import platform |
| 14 | |
| 15 | print(matplotlib.get_backend()) |
| 16 | print('Matplotlib ver:', matplotlib.__version__) |
| 17 | print('Platform:', platform.platform()) |
| 18 | print('System:', platform.system()) |
| 19 | print('Release:', platform.release()) |
| 20 | print('Python ver:', platform.python_version()) |
| 21 | |
| 22 | |
| 23 | def 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 | |
| 32 | def 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 | |
| 42 | if __name__ == '__main__': |
| 43 | run() |
| 44 | ``` |
| 45 | |
| 46 | |
| 47 | ### Actual outcome |
| 48 | |
| 49 | ``` |
| 50 | MacOSX |
| 51 | Matplotlib ver: 3.5.2 |
| 52 | Platform: macOS-12.4-arm64-arm-64bit |
| 53 | System: Darwin |
| 54 | Release: 21.5.0 |
| 55 | Python ver: 3.9.12 |
| 56 | 0: 200.0 |
| 57 | 1: 400.0 |
| 58 | 2: 800.0 |
| 59 | 3: 1600.0 |
| 60 | 4: 3200.0 |
| 61 | 5: 6400.0 |
| 62 | 6: 12800.0 |
| 63 | 7: 25600.0 |
| 64 | 8: 51200.0 |
| 65 | 9: 102400.0 |
| 66 | 10: 204800.0 |
| 67 | 11: 409600.0 |
| 68 | 12: 819200.0 |
| 69 | 13: 1638400.0 |
| 70 | 14: 3276800.0 |
| 71 | 15: 6553600.0 |
| 72 | 16: 13107200.0 |
| 73 | 17: 26214400.0 |
| 74 | 18: 52428800.0 |
| 75 | 19: 104857600.0 |
| 76 | 20: 209715200.0 |
| 77 | 21: 419430400.0 |
| 78 | Traceback (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) |
| 91 | OverflowError: signed integer is greater than maximum |
| 92 | ``` |
| 93 | |
| 94 | ### Expected outcome |
| 95 | |
| 96 | ``` |
| 97 | MacOSX |
| 98 | Matplotlib ver: 3.5.2 |
| 99 | Platform: macOS-12.4-arm64-arm-64bit |
| 100 | System: Darwin |
| 101 | Release: 21.5.0 |
| 102 | Python ver: 3.9.12 |
| 103 | 0: 200.0 |
| 104 | 1: 200.0 |
| 105 | 2: 200.0 |
| 106 | 3: 200.0 |
| 107 | 4: 200.0 |
| 108 | 5: 200.0 |
| 109 | 6: 200.0 |
| 110 | 7: 200.0 |
| 111 | 8: 200.0 |
| 112 | 9: 200.0 |
| 113 | 10: 200.0 |
| 114 | 11: 200.0 |
| 115 | 12: 200.0 |
| 116 | 13: 200.0 |
| 117 | 14: 200.0 |
| 118 | 15: 200.0 |
| 119 | 16: 200.0 |
| 120 | 17: 200.0 |
| 121 | 18: 200.0 |
| 122 | 19: 200.0 |
| 123 | 20: 200.0 |
| 124 | 21: 200.0 |
| 125 | 22: 200.0 |
| 126 | ``` |
| 127 | |
| 128 | ### Additional information |
| 129 | |
| 130 | This seems to happen only on M1 MacBooks and the version of python doesn't matter. |
| 131 | |
| 132 | ### Operating system |
| 133 | |
| 134 | OS/X |
| 135 | |
| 136 | ### Matplotlib Version |
| 137 | |
| 138 | 3.5.2 |
| 139 | |
| 140 | ### Matplotlib Backend |
| 141 | |
| 142 | MacOSX |
| 143 | |
| 144 | ### Python version |
| 145 | |
| 146 | 3.9.12 |
| 147 | |
| 148 | ### Jupyter version |
| 149 | |
| 150 | _No response_ |
| 151 | |
| 152 | ### Installation |
| 153 | |
| 154 | pip |