首页 > 解决方案 > 在 Python 中更改名称的同时绘制一些图形

问题描述

我使用以下代码来绘制和保存 8 个数字。此代码不会一次显示所有 8 个数字。怎么做?因为现在我们应该关闭一个图形并出现另一个图形。

以及如何使用文件中的数据值为每个图形更改一次图形的名称和标题data?例如Fig1: ng = 100Fig2: ng = 250等等。100并且250是数据文件中的数据。

谢谢

row, uf, ng, Tg = np.genfromtxt('texdata.txt',unpack=True)
fig, (ax1) = plt.subplots(1)
for x in range(len(ng)):
    for xx in range(1,819):
            ET_list=[]
            z_list=[]
            for z in np.arange(1,7):
                    Ju = dfimppara.iloc[xx, 1]
                    Jl = dfimppara.iloc[xx, 2]
                    lim = Ju - Jl
                    if lim > 1:
                        pass
                    else:
                        if Ju<7:
                            ET_list.append(ET(xx, z, 100, 1e9, 1, ng[x], 0, Tg[x], 1))
                            z_list.append(z)                  
                            plt.plot(z_list, ET_list))
                        else:
                            pass
    ax1.title.set_text('Fig 1: ng = 100 ')  # we need to change this value for each figure and number of the figure
    plt.savefig('Fig1:T_j.png') # number of the figure should change
    plt.show()

标签: pythonmatplotlibdata-files

解决方案


您可以使用f string格式:

ax1.title.set_text(f'Fig {x}: TG = {Tg[x]}, ng = {ng[x]} ')
plt.savefig(f'Fig1:T_{x}.png')

假设ngx是您要使用的值。


推荐阅读