Automate the Populating of Subplots

假如想象 提交于 2019-12-22 08:07:11

问题


I am in the process of writing a python script that will (1) obtain a list of y-values for each subplot to plot against a common set of x-values, (2) make each of these subplots a scatter-plot and put it in the appropriate location in the subplot grid, and (3) complete these tasks for different sizes of subplot grids. What I mean by the third statement is this: the test case I'm using results in an array of 64 plots, 8 rows and 8 columns. I would like for the code to be able to handle any size array (roughly between 50 and 80 plots) for various grid dimensions without me having to go back in each time I run the code and say "Okay, here's the number of rows and columns I need."

Right now, I'm using an exec command to obtain the y-values, and that's working fine. I'm able to make each of the subplots and get it to populate the grid, but only if I type everything in by hand (64 times of doing the same thing is just dumb, so I know there's got to be a way to automate this).

Could anyone suggest a way in which this might be accomplished? I cannot provide data or my code, as this is research material and is not mine to release. Please excuse me if this question is very basic or is something that I should be able to determine from existing documentation. I am very new to programming, and could use a little guidance!


回答1:


A useful function for things like this is plt.subplots(nrows, ncols) which will return an array (a numpy object array) of subplots on a regular grid.

As an example:

import matplotlib.pyplot as plt
import numpy as np

fig, axes = plt.subplots(nrows=4, ncols=4, sharex=True, sharey=True)

# "axes" is a 2D array of axes objects.  You can index it as axes[i,j] 
# or iterate over all items with axes.flat

# Plot on all axes
for ax in axes.flat:
    x, y = 10 * np.random.random((2, 20))
    colors = np.random.random((20, 3))
    ax.scatter(x, y, s=80, facecolors=colors, edgecolors='')
    ax.set(xticks=np.linspace(0, 10, 6), yticks=np.linspace(0, 10, 6))

# Operate on just the top row of axes:
for ax, label in zip(axes[0, :], ['A', 'B', 'C', 'D']):
    ax.set_title(label, size=20)

# Operate on just the first column of axes:
for ax, label in zip(axes[:, 0], ['E', 'F', 'G', 'H']):
    ax.set_ylabel(label, size=20)

plt.show()




回答2:


If you are going to use Python, you will want to take a stroll through the Python Tutorial to learn the the language and data structures available to you, there is good instructional material online, you might want to consider some of the computer science 101 courses that use Python - MIT OCW, edX, How To Think Like A Computer Scientist...

Without knowing the full details, I imagine it could be something like this which is a mixture of real code and pseudocode:

yvalues = list()
while yvalues_exist:
    yvalues.append(get_yvalue)

#limit plot to eight columns
columns = 8
quotient, remainder = divmod(len(yvalues), columns)
rows = quotient
if remainder:
    rows = rows + 1
for n, yvalue in enumerate(yvalues):
    plt.subplot(rows, columns, n)
    plt.plot(yvalue)

list(), append(), while, divmod(), len(), if, for, enumerate() are all Python statements, methods, or built-in functions.



来源:https://stackoverflow.com/questions/24828771/automate-the-populating-of-subplots

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!