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