Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all objects (artists) drawn on a figure

I'm hoping to setup a method which can convert a normal figure (dark lines, white/transparent background) to a pseudo-inverted figure (light lines, black/transparent background). I could just post-process invert the image, but directly inverted colors look awful, so I'd like to instead (try to) create a mapping from one set of colors to another, and then apply this to all artists which have been added to (all axes on) a figure.

Is there a way to access all objects (e.g. text, scatter, lines, ticklabels, etc) that have been added to a figure?


Edit: my motivation is to automatically create white-background and black-background versions of figures. White-background figures will always (I think) be required for publications (for example), while black-background figures may be better for presentations (i.e. talk slides). While it wouldn't be that much trouble to setup a flag, and change each color based on that, e.g.

if dark:
    col_line = 'cyan'
    col_bg = 'black'
else:
    col_line = 'red'
    col_bg = 'white'

# ... plot ...

It would be much cooler and more convenient (despite overhead) to do something like,

fig.savefig('dark.pdf')
invert(fig)
fig.savefig('light.pdf')
like image 756
DilithiumMatrix Avatar asked Apr 26 '17 14:04

DilithiumMatrix


People also ask

What is FigureCanvas?

FigureCanvas is the area onto which the figure is drawn, the matplotlib. backend_bases. Renderer is the object which knows how to draw on the FigureCanvas , and the matplotlib. artist. Artist is the object that knows how to use a renderer to paint onto the canvas.

What is fig Add_axes?

This can be done by fig.add_axes() . To add a subplot, here is the basic usage: fig = plt.figure() # or fig = plt.gcf()fig.add_axes([left, bottom, width, height]) The left , bottom , width , and height parameters are specified relative to the parent figure in terms of float . Note that fig.

What is an artist Python?

Artist is a 2D plotting library for Python. It's main focus is the output.

What is matplotlib fig?

figure. The figure module provides the top-level Artist , the Figure , which contains all the plot elements. The following classes are defined SubplotParams control the default spacing of the subplots Figure. Top level container for all plot elements.


2 Answers

Recursively call .get_children(), stop when the returned list is empty.

like image 200
Paul Brodersen Avatar answered Oct 08 '22 07:10

Paul Brodersen


You can use a different style or change an existing style to your needs instead of changing all properties of all possible artists yourself.

E.g. you might start with the "dark_background" style and then adjust some further parameters using rcParams.

import numpy as np
import matplotlib.pyplot as plt
plt.style.use("dark_background")

style = {"lines.linewidth" : 2,
         "axes.facecolor" : "#410448"}
plt.rcParams.update(style)

plt.plot(np.sin(np.linspace(0, 2 * np.pi)))

plt.show()

enter image description here

like image 32
ImportanceOfBeingErnest Avatar answered Oct 08 '22 06:10

ImportanceOfBeingErnest