有没有办法在for循环中保存多个绘图而不在python中重写?
我有一个包含不同国家贸易统计数据的汇总数据框列表。我可以通过循环这个数据帧列表来绘制相应的图。现在我打算在不覆盖的情况下本地保存这些内容,我的意思是用各自的标题名保存每个绘图。为此,我尝试了以下方法: 我有一个dataframe列表,其名称如下:有没有办法在for循环中保存多个绘图而不在python中重写?,python,for-loop,matplotlib,Python,For Loop,Matplotlib,我有一个包含不同国家贸易统计数据的汇总数据框列表。我可以通过循环这个数据帧列表来绘制相应的图。现在我打算在不覆盖的情况下本地保存这些内容,我的意思是用各自的标题名保存每个绘图。为此,我尝试了以下方法: 我有一个dataframe列表,其名称如下: [dfList[i].name for i in range(len(dfList))] ['AR fresh-Beef-E', 'AUC fresh-Beef-E', 'BR fresh-Beef-E', 'CA fresh-Beef
[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()
但是上述尝试没有将绘图保存到本地目录。我仔细研究了一下,并尝试了可能的建议,但我仍然没有得到所有应该以其绘图标题命名并保存到本地文件目录的绘图。在我的尝试中,没有任何绘图保存到本地目录
有人能告诉我怎么做吗?有什么想法吗?
目标
我打算以标题作为文件名保存每个绘图,并将其保存到本地目录。有什么想法吗?谢谢这不是一个完整的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()
这不是一个完整的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()
当我尝试以下操作时出现类型错误:TypeError:join只接受给定的一个参数2。此尝试也不会通过将绘图标题名作为文件名来保存图形。有更好的主意吗?尝试将os.path导入脚本顶部的path@Ralph。它现在已保存,但变为文件大小2kb只意味着实际生成的图形未正确保存。为什么呢?还有更好的主意吗?他们只是空的人物和斧头吗?我的\u绘图仪是做什么的?是的,它是空的。我的\u绘图仪为时间序列数据制作折线图。我尝试此操作时遇到类型错误:TypeError:join只接受给定的一个参数2。此尝试也不会通过将绘图标题名作为文件名来保存图形。有更好的主意吗?尝试将os.path导入脚本顶部的path@Ralph。它现在已保存,但变为文件大小2kb只意味着实际生成的图形未正确保存。为什么呢?还有更好的主意吗?他们只是空的人物和斧头吗?我的绘图仪是做什么的?是的,它是空的。我的绘图仪为时间序列数据制作折线图。