Reputation: 4098
I am using matplotlib.pyplot for a little module I am developing (code appended). However, I can't work out how to customise the figure (increas figure size, change background canvas colour). How do I do this. I have tried fig = plt.figure(figsize=(16,8))
after self.fig, self.ax = plt.subplots()
and this does increase the figure size and add my buttons but shows my plot on a seperate figure. What is my simple mistake?
# Setup the axes.
self.fig, self.ax = plt.subplots()
self.slider_ax = self.fig.add_axes([0.2, 0.03, 0.65, 0.03])
self.delete_ax = self.fig.add_axes([0.85, 0.84, 0.1, 0.04])
self.register_ax = self.fig.add_axes([0.85, 0.78, 0.1, 0.04])
self.add_ax = self.fig.add_axes([0.85, 0.72, 0.1, 0.04])
self.save_ax = self.fig.add_axes([0.85, 0.66, 0.1, 0.04])
self.reset_ax = self.fig.add_axes([0.85, 0.16, 0.1, 0.04])
self.quit_ax = self.fig.add_axes([0.85, 0.1, 0.1, 0.04])
# Make the slider
! self.slider = Slider(self.slider_ax, 'Frame', 0, 1,
valinit=0, valfmt='%1.2f', closedmin=True,
closedmax=False)
self.slider.on_changed(self.update)
#Make the buttons
self.del_button = Button(self.delete_ax, 'Delete')
self.del_button.on_clicked(self.delete)
self.reg_button = Button(self.register_ax, 'Register')
self.reg_button.on_clicked(self.register)
self.add_button = Button(self.add_ax, "Add")
self.add_button.on_clicked(self.add)
self.save_button = Button(self.save_ax, "Save")
self.save_button.on_clicked(self.save)
self.quit_button = Button(self.quit_ax, "Quit")
self.quit_button.on_clicked(self.quit)
self.reset_button = Button(self.reset_ax, "Reset")
self.reset_button.on_clicked(self.reset)
# Plot the first slice of the image
self.im = self.ax.imshow(np.array(raw_dicom_stack[0]), cmap = cm.gray)
Upvotes: 3
Views: 3167
Reputation: 8668
plt.subplots has **fig_kw
that contains all the keyword arguments that can be sent to the figure (see figure doc). One of them is figsize
. You can do
self.fig, self.ax = plt.subplots(figsize=(16,8), ...)
otherwise you can use figure methods once the figure is created
self.fig.set_size_inches(16,8, forward=True)
If you call self.fig = plt.figure()
after subplots
, correctly, a second figure shows up. And as you add all the buttons to self.fig
, they all go to the last defined.
At last: it would be probably better to create self.ax with add_axes, to be more coherent with all the other axes and have more control on his positioning
edit (elaborate the last sentence)
self.ax
is a matplotlib.axes.AxesSubplot
object (the same that you would get from e.g. plt.subplot(111), while all the buttons are added to
matplotlib.axes.Axesobjects (created with
add_axes`).
Putting axes and subplots together can give problems with the relative position of the plot with respect to the buttons and slider if the figure size is changed or the window resized.
I think that a consistent division of the space in the figure canvas, either using axes
or subplots
, would be better to avoid such possible problems. So I suggest either to create the plotting axes as self.ax = fig.add_axes(...)
or to use something like gridspec.
Upvotes: 4
Reputation: 21849
Each call to plt.figure
will create a new figure instance. What you want to do is modify an existing figure. You can do this with the methods available on your figure instance. The two directly relevant to this question are set_figwidth, set_figheight and set_facecolor.
HTH
Upvotes: 2