Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create subplots of pictures made with the hist() function in matplotlib/pyplot/numpy ?

Tags:

matplotlib

I would like to create four subplots of pictures made with the hist() function, using matplotlib, pyplot and/or numpy. (I am not entirely sure what the differences between these things are. Usually I just import whatever I need -- based on an example.)

In my case, I have a a list consisting of four lists that describe what the amount of virus particles are at the end of some simulation involving the virus population. Each of these four lists contain 30 (whole) numbers. Most of the numbers are either between 0 and 10, or between 450 and 600 (which means that, in the former case, the virus population has (nearly) become extinct, or that, in the latter case, the virus population has survived and adapted to certain changing conditions).

I would like to show, in each of the subplots created with the hist() function, how often the virus population (nearly) goes extinct, has adapted, or is somewhere in between. So I would like to create four subplot histogram pictures that are bundled together in one big picture. On the x-axis, the population at the end of the simulation is shown, and on the y-axis, the frequency of the virus population having this amount of virus particles is shown.

Also, I would like to be able to give each of the subplots a title and label the x- and y-axes.

I already tried to do this numerous times by looking at the documentation on the hist() function and the subplot option in pyplot, but I couldn't figure out how to combine these options. Could you please give me a small example of this? Then I will probably be able to extrapolate how to adjust the example to my situation.

like image 861
Max Muller Avatar asked Nov 24 '13 12:11

Max Muller


People also ask

How do you plot a histogram in a subplot?

Set the figure size and adjust the padding between and around the subplots. Make two dataframes, df1 and df2, of two-dimensional, size-mutable, potentially heterogeneous tabular data. Create a figure and a set of subplots.

How do I make multiple subplots in matplotlib?

To create multiple plots use matplotlib. pyplot. subplots method which returns the figure along with Axes object or array of Axes object. nrows, ncols attributes of subplots() method determine the number of rows and columns of the subplot grid.

What does subplots () do in matplotlib?

Subplots mean groups of axes that can exist in a single matplotlib figure. subplots() function in the matplotlib library, helps in creating multiple layouts of subplots. It provides control over all the individual plots that are created.


1 Answers

Unless you are not already familiar with it, you should have a look at the matplotlib gallery.
There you will find lots of examples hot to use the add_subplot and subplots commands.

A minimal example of what you propably want to achieve is

import matplotlib.pyplot as plt
import numpy as np

data=np.random.random((4,10))
xaxes = ['x1','x2','x3','x4']
yaxes = ['y1','y2','y3','y4']
titles = ['t1','t2','t3','t4'] 

f,a = plt.subplots(2,2)
a = a.ravel()
for idx,ax in enumerate(a):
    ax.hist(data[idx])
    ax.set_title(titles[idx])
    ax.set_xlabel(xaxes[idx])
    ax.set_ylabel(yaxes[idx])
plt.tight_layout()

which results in a plot like
enter image description here

like image 173
Jakob Avatar answered Oct 20 '22 16:10

Jakob