How to create a strip-like chart with matplotlib? - python

Is there any way to create a plot with matplotlib so when the window is maximized, the plot only occupies a strip of the screen? As an example, please consider the example below. In this case, I want to arrange the 3 subplots in an horizontal line, and I would like to keep the size small, that is why I added 2 rows with blank subplots. When I use figsize the window that pops does have the specified size, but once it's maximized, matplotlib re-fits the subplots to occupy the entire screen. I would like matplotlib to redraw the contents to fit this "strip", not the other way round.
Thus, is it possible to create the strip directly, without un-wanted resizes from matplotlib and without adding all these innecessary blank subplots to do the trick?
Thanks!
EDIT
I am running this program using Python 3.5 and Matplotlib 2.2.3. The interactive backend is TkAgg.

Matplotlib's subplot grids are positionned in units relative to the figure size. Positionning subplots in absolute coordinates hence requires some tweaking.
You could define the width and height of the grid in inches and then calculate the margins of the grid each time the figure is resized.
In the following the grid always stays 4 inch wide and 1 inch tall, no matter the figure size.
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(42)
fig, axes = plt.subplots(ncols=3, sharey=True)
for ax in axes:
ax.plot(np.arange(30), np.cumsum(np.random.randn(30)))
# set width and height in physical units (inches)
width = 4 # inch
height= 1 # inch
def resize(evt=None):
w,h = fig.get_size_inches()
l = ((w-width)/2.)/w
b = ((h-height)/2.)/h
fig.subplots_adjust(left=l, right=1.-l, bottom=b, top=1.-b)
fig.canvas.draw_idle()
resize()
fig.canvas.mpl_connect("resize_event", resize)
plt.show()

You didn't explain about your visualisation approach.
So according to me you can try subplot for it.
see how subplot works here (https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.subplot)
from matplotlib import pyplot as PLT
fig = PLT.figure()
ax1 = fig.add_subplot(211)
ax1.plot([(1, 2), (3, 4)], [(4, 3), (2, 3)])
ax2 = fig.add_subplot(221)
ax2.plot([(7, 2), (5, 3)], [(1, 6), (9, 5)])
ax3 = fig.add_subplot(222)
ax3.plot([(7, 2), (5, 3)], [(1, 6), (9, 5)])
PLT.show()

Related

Adding subplots to a particular figure using "subplot2grid" method

I am trying to add subplots of differing sizes to a particular matplotlib figure, and am unsure of how to do so. In the case of there only being one figure, the "subplot2grid" can be utilized as follows:
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = plt.subplot2grid((2, 2), (0, 0), colspan=2)
ax1 = plt.subplot2grid((2, 2), (1, 1))
plt.show()
The above code creates a figure, and adds two subplots to that figure, each with different dimensions. Now, my issue arises in the case of having multiple figures -- I cannot find the appropriate way to add subplots to a particular figure using "subplot2grid." Using the more simple "add_subplot" method, one can add subplots to a particular figure, as seen in the below code:
import matplotlib.pyplot as plt
fig1 = plt.figure()
fig2 = plt.figure()
ax1 = fig1.add_subplot(2, 2, 1)
ax2 = fig1.add_subplot(2, 2, 4)
plt.show()
I am looking for the analogous method for adding subplots of different sizes (preferably using some sort of grid manager, e.g. "subplot2grid") to a particular figure. I have reservations about using the plt."x" style because it operates on the last figure that was created -- my code will have several figures, all of which I will need to have subplots of different sizes.
Thanks in advance,
Curtis M.
In the future (probably the upcoming release?), subplot2grid
will take a fig argument
subplot2grid(shape, loc, rowspan=1, colspan=1, fig=None, **kwargs)
such that the following would be possible:
import matplotlib.pyplot as plt
fig1=plt.figure()
fig2=plt.figure()
ax1 = plt.subplot2grid((2, 2), (0, 0), colspan=2, fig=fig1)
ax2 = plt.subplot2grid((2, 2), (1, 1), fig=fig1)
plt.show()
As of now (version 2.0.2) this is not yet possible. Alternatively, you can manually define the underlying GridSpec
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
fig1=plt.figure()
fig2=plt.figure()
spec1 = GridSpec(2, 2).new_subplotspec((0,0), colspan=2)
ax1 = fig1.add_subplot(spec1)
spec2 = GridSpec(2, 2).new_subplotspec((1,1))
ax2 = fig1.add_subplot(spec2)
plt.show()
Or you can simply set the current figure, such that plt.subplot2grid will work on that exact figure (as shown in this question)
import matplotlib.pyplot as plt
fig1=plt.figure(1)
fig2=plt.figure(2)
# ... some other stuff
plt.figure(1) # set current figure to fig1
ax1 = plt.subplot2grid((2, 2), (0, 0), colspan=2)
ax2 = plt.subplot2grid((2, 2), (1, 1))
plt.show()

Using pyplot to create grids of plots

I am new to python and having some difficulties with plotting using pyplot. My goal is to plot a grid of plots in-line (%pylab inline) in Juypter Notebook.
I programmed a function plot_CV which plots cross-validation erorr over the degree of polynomial of some x where across plots the degree of penalization (lambda) is supposed to vary. Ultimately there are 10 elements in lambda and they are controlled by the first argument in plot_CV. So
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
ax1 = plot_CV(1,CV_ve=CV_ve)
Gives
Now I think I have to use add_subplot to create a grid of plots as in
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax1 = plot_CV(1,CV_ve=CV_ve)
ax2 = fig.add_subplot(2,2,2)
ax2 = plot_CV(2,CV_ve=CV_ve)
ax3 = fig.add_subplot(2,2,3)
ax3 = plot_CV(3,CV_ve=CV_ve)
ax4 = fig.add_subplot(2,2,4)
ax4 = plot_CV(4,CV_ve=CV_ve)
plt.show()
If I continue this, however, then the plots get smaller and smaller and start to overlap on the x and y labels. Here a picture with a 3 by 3 plot.
Is there a way to space the plots evenly, so that they do not overlap and make better use of the horizontal and vertical in-line space in Jupyter Notebook? To illustrate this point here a screenshot from jupyter:
Final note: I still need to add a title or annotation with the current level of lambda used in plot_CV.
Edit: Using the tight layout as suggested, gives:
Edit 2: Using the fig.set_figheight and fig.set_figwidth I could finally use the full length and heigth available.
The first suggestion to your problem would be taking a look at the "Tight Layout guide" for matplotlib.
They have an example that looks visually very similar to your situation. As well they have examples and suggestions for taking into consideration axis labels and plot titles.
Furthermore you can control the overall figure size by using Figure from the matplotlib.figure class.
Figure(figsize = (x,y))
figsize: x,y (inches)
EDIT:
Here is an example that I pulled from the matplotlib website and added in the:
fig.set_figheight(15)
fig.set_figwidth(15)
example:
import matplotlib.pyplot as plt
plt.rcParams['savefig.facecolor'] = "0.8"
def example_plot(ax, fontsize=12):
ax.plot([1, 2])
ax.locator_params(nbins=3)
ax.set_xlabel('x-label', fontsize=fontsize)
ax.set_ylabel('y-label', fontsize=fontsize)
ax.set_title('Title', fontsize=fontsize)
plt.close('all')
fig = plt.figure()
fig.set_figheight(15)
fig.set_figwidth(15)
ax1 = plt.subplot2grid((3, 3), (0, 0))
ax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2)
ax4 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
example_plot(ax1)
example_plot(ax2)
example_plot(ax3)
example_plot(ax4)
plt.tight_layout()
You can achieve padding of your subplots by using tight_layout this way:
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
That way you can keep your subplots from crowding each other even further.
Have a good one!

Adjust figure size with respect to the axes size?

Let's say I create a simple plot with matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
plot stuff etc .. .
After I finished the plotting routines I want to adjust the size of my figure (but keeping all aspect ratios the same), such that the total height of ax is set to a constant value i.e. 3 inches.
Is there a nice and short way to archieve this ?
EDIT: I know how to change the figure size, but here i want to adjust it according to the size of the axes.
Here is the function to set figure height in inch unit:
def set_axes_height(ax, h):
fig = ax.figure
aw, ah = np.diff(ax.transAxes.transform([(0, 0), (1, 1)]), axis=0)[0]
fw, fh = fig.get_size_inches()
dpi = fig.get_dpi()
scale = h / (ah / dpi)
fig.set_size_inches(fw*scale, fh*scale, forward=True)

bunch of histograms next to each other

In this figure, in the 1st plot, the grid divides the plot in "windows" and each window is divided in subwindows (made with let's say 5 data).
Then the slope of each subwindow is calculated and saved.
Next I divide the polar plane in 16 quadrants and calculate which quadrant correspond to each slope. So, I get something like this:
1,1,1,1,1,1,1,1,1,1,1,2,2,2,3,4
4,1,-1,-2,...
In the dataset above, each number is the quadrant that represents the slope of a subwindow and each row represents a window (The histograms are calculated with this dataset).
What I'm looking for is that the figure at the top, the 2nd plot shows the histogram of each window under its corresponding window.
All I could get is this from the matplotlib page but none of those examples are close to what I need because I need the histograms next to each other without blocking each other.
Sometimes, depending on the parameters used, it could be more than 800 histograms in the same plot.
Here's an example of how you can display multiple plots side-by-side below a larger one using Gridspec:
import numpy as np
import matplotlib.pyplot as plt
# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)
# plot it
fig = plt.figure(figsize=(8, 6))
ax0 = plt.subplot2grid((2, 4), (0, 0), colspan=4)
ax0.plot(x, y)
ax1 = plt.subplot2grid((2, 4), (1, 0))
ax1.hist(y)
ax2 = plt.subplot2grid((2, 4), (1, 1))
ax2.hist(y)
ax2.set_yticklabels([])
ax2.set_yticks([])
ax3 = plt.subplot2grid((2, 4), (1, 2))
ax3.hist(y)
ax3.set_yticklabels([])
ax3.set_yticks([])
ax4 = plt.subplot2grid((2, 4), (1, 3))
ax4.hist(y)
ax4.set_yticklabels([])
ax4.set_yticks([])
plt.subplots_adjust(wspace=0) # no space left between hists in 2nd row
Results in:

Using passed axis objects in a matplotlib.pyplot figure?

I am currently attempting to use passed axis object created in function, e.g.:
def drawfig_1():
import matplotlib.pyplot as plt
# Create a figure with one axis (ax1)
fig, ax1 = plt.subplots(figsize=(4,2))
# Plot some data
ax1.plot(range(10))
# Return axis object
return ax1
My question is, how can I use the returned axis object, ax1, in another figure? For example, I would like to use it in this manner:
# Setup plots for analysis
fig2 = plt.figure(figsize=(12, 8))
# Set up 2 axes, one for a pixel map, the other for an image
ax_map = plt.subplot2grid((3, 3), (0, 0), rowspan=3)
ax_image = plt.subplot2grid((3, 3), (0, 1), colspan=2, rowspan=3)
# Plot the image
ax_psf.imshow(image, vmin=0.00000001, vmax=0.000001, cmap=cm.gray)
# Plot the map
???? <----- #I don't know how to display my passed axis here...
I've tried statements such as:
ax_map.axes = ax1
and although my script does not crash, my axis comes up empty. Any help would be appreciated!
You are trying to make a plot first and then put that plot as a subplot in another plot (defined by subplot2grid). Unfortunately, that is not possible. Also see this post: How do I include a matplotlib Figure object as subplot?.
You would have to make the subplot first and pass the axis of the subplot to your drawfig_1() function to plot it. Of course, drawfig_1() will need to be modified. e.g:
def drawfig_1(ax1):
ax1.plot(range(10))
return ax1
# Setup plots for analysis
fig2 = plt.figure(figsize=(12, 8))
# Set up 2 axes, one for a pixel map, the other for an image
ax_map = plt.subplot2grid((3, 3), (0, 0), rowspan=3)
ax_image = plt.subplot2grid((3, 3), (0, 1), colspan=2, rowspan=3)
# Plot the image
ax_image.imshow(image, vmin=0.00000001, vmax=0.000001, cmap=cm.gray)
# Plot the map:
drawfig_1(ax_map)

Categories

Resources