如何在python中一起打印多个图? [英] How to print multiple plots together in python?

查看:234
本文介绍了如何在python中一起打印多个图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在7行6列中打印约42个图,但是在jupyter笔记本中打印的输出显示所有图在一个图的下面.我希望它们以(7,6)格式进行比较.我正在使用matplotlib.subplot2grid()函数.

注意:我没有得到任何错误,我的代码也可以正常工作,但是情节是一个又一个的情节,而不是网格/矩阵形式.这是我的代码:

  def draw_umap(n_neighbors = 15,min_dist = 0.1,n_components = 2,metric ='euclidean',title =''):适合= umap.UMAP(n_neighbors = n_neighbors,min_dist = min_dist,n_components = n_components,公制=公制)u = fit.fit_transform(df);地块= []plt.figure(0)无花果= plt.figure()fig.set_figheight(10)fig.set_figwidth(10)对于我在范围(7)中:对于范围(6)中的j:plt.subplot2grid((7,6),(i,j),rowpan = 7,colspan = 6)plt.scatter(u [:,0],u [:,1],c = df.iloc [:,0])plt.title(title,fontsize = 8)n =范围(7)d =范围(6)对于n_neighbors中的n:对于dist中的d:draw_umap(n_neighbors = n,min_dist = d,title =''n_neighbors = {}''.format(n)+``min_dist = {}''.format(d)) 

我确实引用了此

这也可以在seaborn中完成,但是我需要查看您的数据集是什么样,以提供与您的案例相关的解决方案.



您可以在此答案的第二个解决方案中找到这种方法的更详尽的示例.

I am trying to print about 42 plots in 7 rows, 6 columns, but the printed output in jupyter notebook, shows all the plots one under the other. I want them in (7,6) format for comparison. I am using matplotlib.subplot2grid() function.

Note: I do not get any error, and my code works, however the plots are one under the other, vs being in a grid/ matrix form. Here is my code:

def draw_umap(n_neighbors=15, min_dist=0.1, n_components=2, metric='euclidean', title=''):
fit = umap.UMAP(
    n_neighbors=n_neighbors,
    min_dist=min_dist,
    n_components=n_components,
    metric=metric
)
u = fit.fit_transform(df);
plots = []
plt.figure(0)
fig = plt.figure()
fig.set_figheight(10)
fig.set_figwidth(10)
for i in range(7):
    for j in range(6):
        plt.subplot2grid((7,6), (i,j), rowspan=7, colspan=6)
        
    plt.scatter(u[:,0], u[:,1], c= df.iloc[:,0])
        
    plt.title(title, fontsize=8)

n=range(7)
d=range(6)

for n in n_neighbors:
    for d in dist:
        draw_umap(n_neighbors=n, min_dist=d, title="n_neighbors={}".format(n) + " min_dist={}".format(d))

I did refer to this post to get the plots in a grid and followed the code. I also referred to this post, and modified my code for size of the fig.

Is there a better way to do this using Seaborn?

What am I missing here? Please help!

解决方案

Both questions that you have linked contain solutions that seem more complicated than necessary. Note that subplot2grid is useful only if you want to create subplots of varying sizes which I understand is not your case. Also note that according to the docs Using GridSpec, as demonstrated in GridSpec demo is generally preferred, and I would also recommend this function only if you want to create subplots of varying sizes.

The simple way to create a grid of equal-sized subplots is to use plt.subplots which returns an array of Axes through which you can loop to plot your data as shown in this answer. That solution should work fine in your case seeing as you are plotting 42 plots in a grid of 7 by 6. But the problem is that in many cases you may find yourself not needing all the Axes of the grid, so you will end up with some empty frames in your figure.

Therefore, I suggest using a more general solution that works in any situation by first creating an empty figure and then adding each Axes with fig.add_subplot as shown in the following example:

import numpy as np               # v 1.19.2
import matplotlib.pyplot as plt  # v 3.3.4

# Create sample dataset
rng = np.random.default_rng(seed=1)  # random number generator
nvars = 8
nobs = 50
xs = rng.uniform(size=(nvars, nobs))
ys = rng.normal(size=(nvars, nobs))

# Create figure with appropriate space between subplots
fig = plt.figure(figsize=(10, 8))
fig.subplots_adjust(hspace=0.4, wspace=0.3)

# Plot data by looping through arrays of variables and list of colors
colors = plt.get_cmap('tab10').colors
for idx, x, y, color in zip(range(len(xs)), xs, ys, colors):
    ax = fig.add_subplot(3, 3, idx+1)
    ax.scatter(x, y, color=color)

This could be done in seaborn as well, but I would need to see what your dataset looks like to provide a solution relevant to your case.



You can find a more elaborate example of this approach in the second solution in this answer.

这篇关于如何在python中一起打印多个图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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