Return a context manager for temporarily changing rcParams. The :rc:`backend` will not be reset by the context manager. rcParams changed both through the context manager invocation and in the body of the context will be reset on context exit. Parameters ---------- rc
(rc=None, fname=None)
| 1158 | |
| 1159 | @contextlib.contextmanager |
| 1160 | def rc_context(rc=None, fname=None): |
| 1161 | """ |
| 1162 | Return a context manager for temporarily changing rcParams. |
| 1163 | |
| 1164 | The :rc:`backend` will not be reset by the context manager. |
| 1165 | |
| 1166 | rcParams changed both through the context manager invocation and |
| 1167 | in the body of the context will be reset on context exit. |
| 1168 | |
| 1169 | Parameters |
| 1170 | ---------- |
| 1171 | rc : dict |
| 1172 | The rcParams to temporarily set. |
| 1173 | fname : str or path-like |
| 1174 | A file with Matplotlib rc settings. If both *fname* and *rc* are given, |
| 1175 | settings from *rc* take precedence. |
| 1176 | |
| 1177 | See Also |
| 1178 | -------- |
| 1179 | :ref:`customizing-with-matplotlibrc-files` |
| 1180 | |
| 1181 | Examples |
| 1182 | -------- |
| 1183 | Passing explicit values via a dict:: |
| 1184 | |
| 1185 | with mpl.rc_context({'interactive': False}): |
| 1186 | fig, ax = plt.subplots() |
| 1187 | ax.plot(range(3), range(3)) |
| 1188 | fig.savefig('example.png') |
| 1189 | plt.close(fig) |
| 1190 | |
| 1191 | Loading settings from a file:: |
| 1192 | |
| 1193 | with mpl.rc_context(fname='print.rc'): |
| 1194 | plt.plot(x, y) # uses 'print.rc' |
| 1195 | |
| 1196 | Setting in the context body:: |
| 1197 | |
| 1198 | with mpl.rc_context(): |
| 1199 | # will be reset |
| 1200 | mpl.rcParams['lines.linewidth'] = 5 |
| 1201 | plt.plot(x, y) |
| 1202 | |
| 1203 | """ |
| 1204 | orig = dict(rcParams.copy()) |
| 1205 | del orig['backend'] |
| 1206 | try: |
| 1207 | if fname: |
| 1208 | rc_file(fname) |
| 1209 | if rc: |
| 1210 | rcParams.update(rc) |
| 1211 | yield |
| 1212 | finally: |
| 1213 | rcParams._update_raw(orig) # Revert to the original rcs. |
| 1214 | |
| 1215 | |
| 1216 | def use(backend, *, force=True): |
nothing calls this directly
no test coverage detected
searching dependent graphs…