如何在matplotlib/pyplot/numpy中使用hist()函数创建图片的子图? [英] How to create subplots of pictures made with the hist() function in matplotlib/pyplot/numpy ?

查看:223
本文介绍了如何在matplotlib/pyplot/numpy中使用hist()函数创建图片的子图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用hist()函数使用matplotlib,pyplot和/或numpy创建图片的四个子图. (我不确定这些东西之间的区别是什么.通常我只是导入一个我需要的东西-基于一个例子.)

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.)

就我而言,我有一个包含四个列表的列表,这些列表描述了在涉及病毒种群的某些模拟结束时病毒颗粒的数量.这四个列表中的每一个都包含30个(整个)数字.大多数数字介于0到10之间,或介于450到600之间(这意味着,在前一种情况下,病毒种群已经(几乎)灭绝了;或者,在后一种情况下,病毒种群得以幸存了下来,适应某些变化的条件).

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).

我想在使用hist()函数创建的每个子图中显示病毒种群(几乎)灭绝,适应或处于之间的频率.因此,我想创建四张子图直方图图片,这些图片捆绑在一起成为一张大图片.在x轴上,示出了模拟结束时的种群,在y轴上,示出了具有该病毒颗粒量的病毒种群的频率.

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.

此外,我希望能够给每个子图一个标题并标记x轴和y轴.

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

通过查看hist()函数的文档和pyplot中的subplot选项,我已经尝试了很多次,但是我不知道如何组合这些选项.您能给我一个小例子吗?然后,我可能可以推断出如何根据我的情况调整示例.

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.

推荐答案

除非您还不熟悉它,否则应该看看

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

这篇关于如何在matplotlib/pyplot/numpy中使用hist()函数创建图片的子图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆