Matplotlib:使用循环绘制的 8 个图的相同标题 [英] Matplotlib: Same title for 8 plots plotted using loop

查看:29
本文介绍了Matplotlib:使用循环绘制的 8 个图的相同标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码可以生成 8 个图.我想将各阶段作为标题放在每个情节中.所以我成功地把这个阶段放在了情节上.但是,与其采用相应的阶段,不如总是采用最后一个阶段在每个图中显示.8phases.txt文件包含以下8行,我想在每个图中将它们放入-

I have the following code which generates 8 plots. I want to put the phases as titles in each plot. So I have succeded to put the phase on the plot. But instead of taking corresponding phase, it is always taking the last phase to show in each plot. The 8phases.txt file has the following 8 lines which I want to put in each plot -

-1   1   -1    
-1   1    1
 1   1    1
 1  -1    1
-1  -1   -1
 1   1   -1
 1  -1   -1
-1  -1    1

这是代码 -

import numpy as np
import matplotlib.pyplot as plt

D=12
n=np.arange(1,4)
x = np.linspace(-D/2,D/2, 3000)
I = np.array([125,300,75])
phase = np.genfromtxt('8phases.txt')
I_phase = I*phase

for i in I_phase:
    F = sum(m*np.cos(2*np.pi*l*x/D) for m,l in zip(i,n))
    f,(ax1,ax2) = plt.subplots(2)
    for row in phase:
        ax1.plot(x,F,'g')
        ax1.set_title(row)

plt.show()

推荐答案

我认为您最内层的循环是不必要的;它正在重新创建相同的图 8 次,并使用 8 个值中的每一个更新标题 8 次.

I think your inner-most loop is unnecessary; it is recreating the same plot 8 times and updating the title 8 times with each of the 8 values.

如果我了解您的要求,我相信这会给出正确的结果:

If I understood what you are asking for, I believe this gives the correct results:

...

for index,i in enumerate(I_phase):
    F = sum(m*np.cos(2*np.pi*l*x/D) for m,l in zip(i,n))
    f,(ax1,ax2) = plt.subplots(2)
    ax1.plot(x,F,'g')
    ax1.set_title(phase[index])

...

(我通常会使用"i"代替"index",但是您已经使用了"i")

(I would normally use "i" instead of "index", but you had already used "i")

这篇关于Matplotlib:使用循环绘制的 8 个图的相同标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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