有没有办法在循环中保存多个图而无需在python中覆盖?

beyond_inifinity

我有一个汇总的数据框列表,其中包含不同国家的贸易统计信息。我可以通过遍历此数据框列表来制作各个图。现在,我打算将其保存在本地而不进行覆盖,我的意思是用各自的标题名称保存每个地块。为此,我尝试了以下操作:

我有以下名称的数据框列表:

[dfList[i].name for i in range(len(dfList))]

['AR  fresh-Beef-E',
 'AUC  fresh-Beef-E',
 'BR  fresh-Beef-E',
 'CA  fresh-Beef-E',
 'CL  fresh-Beef-E',
 'CN  fresh-Beef-E',
 'E28  fresh-Beef-E',
 'EG  fresh-Beef-E',
 'IN  fresh-Beef-E',
 'JP  fresh-Beef-E',
 'KR  fresh-Beef-E',
 'MX  fresh-Beef-E',
 'NZ  fresh-Beef-E',
 'PY  fresh-Beef-E',
 'US  fresh-Beef-E',
 'UY  fresh-Beef-E',
 'ZA  fresh-Beef-E']

当前尝试

打算将图及其标题作为文件名保存在本地:

import os
import pandas as pd
import matplotlib.pyplot as plt

outpath = r'C:/Users/plots'

if os.path.exists(outpath):
    shutil.rmtree(outpath)
_ = os.mkdir(outpath)

for i in range(len(dfList)) :
    plt.figure()
    my_plotter(dfList[i],title=dfList[i].name)
    plt.savefig(path.join(outpath,"dataname_{0}.png".format(i)))
    plt.close()

新的更新

这是我的绘图功能的样子:

def my_plotter(df, plot_type='something', ylab_nm='something', title=None):
    fig, ax1 = plt.subplots(figsize=figsize)
    if plot_type=='something':
        _ = df.plot(kind='line', ax=ax1, marker='o', ls='-', linewidth=2, color=colors)
    else:
        df.loc[:, 'Total'] = df.sum(axis=1)
        _ = df.div(df.Total, axis=0).iloc[:, :-1].plot(kind='line', ax=ax1, marker='o', ls='--', linewidth=4, color=colors)
        df.drop('Total', axis=1, inplace=True)
        ax1.yaxis.set_major_formatter(mtick.PercentFormatter(xmax=1, decimals=0))
        ax1.xaxis.set_major_locator(mdates.MonthLocator(bymonthday=1, interval=3))

    ax1.set(title=title)
    plt.title(title)
    ax1.xaxis.label.set_visible(False)
    plt.style.use('ggplot')
    plt.xticks(rotation=90)
    plt.show()

但是上述尝试并未将绘图保存到本地目录。我调查SO并尝试了可能的建议,但仍然没有获得所有应该通过获取其图名而命名并保存到本地文件目录的图。在我的尝试中,所有地块均未保存到本地目录。

谁能指出我如何进行这项工作?任何想法?目标

我打算通过将其标题作为文件名来保存每个图并将其保存到本地目录。有什么想法吗?谢谢

贾尔顿

它不是一个完整的MWE,但我认为以下修复程序可以为您提供帮助。

import matplotlib.pyplot as plt
import pandas as pd
import os

# Only need to set the style once (not in loop)
plt.style.use('ggplot')

outpath = r'C:/Users/plots'

def my_plotter(df, plot_type='something', ylab_nm='something', title=None):
    fig, ax1 = plt.subplots(1, 1)
    if plot_type=='something':
        # Plots on ax1, so don't save output as another ax instance
        df.plot(kind='line', ax=ax1, marker='o', ls='-', linewidth=2)
    else:
        df.loc[:, 'Total'] = df.sum(axis=1)
        # Plots on ax1, so don't save output as another ax instance
        df.div(df.Total, axis=0).iloc[:, :-1].plot(kind='line', ax=ax1, marker='o', ls='--', linewidth=4, color=colors)
        df.drop('Total', axis=1, inplace=True)
        ax1.yaxis.set_major_formatter(mtick.PercentFormatter(xmax=1, decimals=0))
        ax1.xaxis.set_major_locator(mdates.MonthLocator(bymonthday=1, interval=3))

    ax1.set(title=title)
    ax1.set_title(title, size=24, verticalalignment='bottom') 
    ax1.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))
    ax1.xaxis.label.set_visible(False)

    plt.xticks(rotation=90)
    plt.show()
    return fig, ax1

if os.path.exists(outpath):
    shutil.rmtree(outpath)
_ = os.mkdir(outpath)

for i in range(len(dfList)):
    fig, ax1 = my_plotter(dfList[i], title=dfList[i].name)
    fig.savefig(path.join(outpath,"dataname_{}.png".format(dfList[i].name)))
    plt.close()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

有没有办法在 For 循环中保存字符数组?

有没有办法在渲染器进程中保存/写入文本文件而无需在 VueJs 中打开对话框?

有没有办法循环遍历python中for循环中的列表?

有没有办法计算python中while循环中的迭代次数?

有没有办法在 C++ 中保存冻结的张量流图?

在 C# 中,有没有办法直接从数组创建 List 而无需复制?

有没有办法用输入的维度初始化二维数组中的所有项目,而无需在 C++ 中循环?

有没有办法从 Python 用户界面中创建的变量中保存输入(浮点数)?

有没有办法从python的直方图中保存垃圾箱?

有没有办法在 Flutter 中无需注册就可以保存用户的数据?

有没有办法设置多个本地 Go 模块,以便它们可以在一个 Docker 容器中运行而无需从 Github 中提取?

有没有办法覆盖 Mockito 中的 doReturn?

有没有办法编辑多对多关系船而无需在Laravel中附加-分离

有没有办法对 ansible 主机文件中的组进行排序,而无需对组内的主机进行排序?

有没有办法在Python中覆盖现有(系统)类上的方法?

有没有办法动态更改 css 属性而无需定义可能的更改量

有没有办法使用NSParagraphStyle.default.mutableCopy()而无需强制展开?

有没有办法写“ regex”而无需重复某些部分?

有没有办法对 JSON 对象进行排序而无需重新排序其中的数组?

有没有办法自动将密码传递给 sudo 而无需输入密码?

有没有办法在 cypress 运行中保存完整的 XHR 有效负载?

在 for 循环中使用参数调用 javascript 函数:有没有办法暂停循环?

有没有办法在循环中拥有更多相关的变量名?

有没有办法循环遍历python中的函数?

有没有办法在python中减少n次嵌套的for循环

有没有办法在嵌套 for 循环中访问生成器中每个元素的属性?

有没有办法在使用 map() 函数的循环中推送谷歌地球引擎中的键值对?

有没有办法在libreoffice calc中保存过滤器?

有没有办法在 TunnelBlick vpn 中保存预填的用户名