在matplotlib中动态添加/创建子图 [英] Dynamically add/create subplots in matplotlib

查看:73
本文介绍了在matplotlib中动态添加/创建子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个由几个具有共享x/y轴的子图组成的图. 从文档中看起来应该像这样(尽管我的子图将是散布图):(代码在这里) )

I want to create a plot consisting of several subplots with shared x/y axes. It should look something like this from the documentation (though my subplots will be scatterblots): (code here)

但是我想动态创建子图!

因此,子图的数量取决于上一个函数的输出. (根据我的脚本的输入,每个图大概有3到15个子图,每个子图来自不同的数据集.)

So the number of subplots depends on the output of a previous function. (It will probably be around 3 to 15 subplots per diagram, each from a distinct dataset, depending on the input of my script.)

谁能告诉我如何做到这一点?

Can anyone tell me how to accomplish that?

推荐答案

import matplotlib.pyplot as plt
from pylab import *
import numpy as np

x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)

subplots_adjust(hspace=0.000)
number_of_subplots=3

for i,v in enumerate(xrange(number_of_subplots)):
    v = v+1
    ax1 = subplot(number_of_subplots,1,v)
    ax1.plot(x,y)

plt.show()

此代码有效,但是您需要更正轴.我曾经习惯subplot在同一列中绘制3个图形.您需要做的就是为number_of_plots变量分配一个整数.如果每个图的X和Y值不同,则需要为每个图分配它们.

This code works but you will need to correct the axes. I used to subplot to plot 3 graphs all in the same column. All you need to do is assign an integer to number_of_plots variable. If the X and Y values are different for each plot you will need to assign them for each plot.

subplot的工作方式如下.这将创建一个3x1的网格并将图放置在第一位置.在下一个交互中,如果我的subplot值为3,1,2,它将再次创建3x1网格,但将图放置在第二个位置,依此类推.

subplot works as follows, if for example I had a subplot values of 3,1,1. This creates a 3x1 grid and places the plot in the 1st position. In the next interation if my subplot values were 3,1,2 it again creates a 3x1 grid but places the plot in the 2nd position and so forth.

这篇关于在matplotlib中动态添加/创建子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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