0

I want the plot title names as their output file name. Here what I have tried:

# -*- coding: utf-8 -*-
"""
Created on Mon Jan  3 15:46:30 2022

@author: Ghost
"""

import os
import pandas as pd
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt

#plt.style.use('ggplot')
plt.style.use('default')



obs = pd.read_excel(r'T:/Active Projects/US Army Corps of Engineers/St Louis District/LD25/Model/Model_Testing/plot/raw data/LDbase_211229_vel .xlsx','Sheet1')

sim1=pd.read_excel(r'T:/Active Projects/US Army Corps of Engineers/St Louis District/LD25/Model/Model_Testing/plot/raw data/LDitl_211229_ovl.xlsx', 'Sheet1')


pdf = PdfPages(r'T:\Active Projects\US Army Corps of Engineers\St Louis District\LD25\Model\Model_Testing\plot\raw data\LDbase_211229_LDitl_211229_vel.pdf')
for i in range(0,12): # no of rows of obs is 24 or obs.shape[1]=24, so range(0,obs.shape[1]-1)
    # for j in range(0,11):
    title=[' Pool Existing Day 2, 2265 cms', ' Pool Existing: Day 4, 3511 cms',' Pool Existing: Day 5, 8920 cms',
           ' DS Existing: Day 2, 2265 cms',' DS Existing: Day 4, 3511 cms',' DS Existing: Day 5, 8920 cms',
           ' Pool Proposed: Day 2, 2265 cms', ' Pool Proposed: Day 4, 3511 cms',' Pool Proposed: Day 5, 8920 cms',
           ' DS Proposed: Day 2, 2265 cms', ' DS Proposed: Day 4, 3511 cms',' DS Proposed: Day 5, 8920 cms']
    plt.rcParams.update({'font.size': 12})   
    fig, ax = plt.subplots(figsize=(12,6))
    
    a= 3.28084 # m to ft converter
    plt.plot(obs.iloc[:,2*i]*a,obs.iloc[:,2*i+1]*a,linestyle='-',ms='6',mfc='none') # base model
    plt.plot(sim1.iloc[:,2*i]*a,sim1.iloc[:,2*i+1]*a,linestyle='--',ms='6',mfc='none') # sim1 
    
    plt.grid(which='major',  linestyle='-',linewidth=0.3)
    plt.grid(which='minor',  linestyle='--',linewidth=0.2)
    #plt.minorticks_on()
    
    plt.xlabel("Distance (ft)")
    plt.ylabel(" Velocity (ft/s)")
    ax.legend(['LDbase_211229','LDitl_211229'])
    ax.set_title(title[i])
  

  
    plt.savefig(r'T:\Active Projects\US Army Corps of Engineers\St Louis District\LD25\Model\Model_Testing\plot\raw data\LDbase_211229_LDitl_211229_vel\{}.png'.format(i),dpi=300)
    
    pdf.savefig(fig)

pdf.close() 

It saves the file names as 0.png, 1.png etc. when I try {}.png'.format(str(title[i]))

instead of {}.png'.format(i) only the first plot Pool Existing Day 2, 2265 cms.png is created and then the following error comes

FileNotFoundError: [Errno 2] No such file or directory: 'T:\Active Projects\US Army Corps of Engineers\St Louis District\LD25\Model\Model_Testing\plot\raw data\LDbase_211229_LDitl_211229_vel\ Pool Existing: Day 4, 3511 cms.png'

ZVY545
  • 384
  • 1
  • 13
  • 1
    The colon after `Pool Existing` looks [suspicious](https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names) for a filepath. – BigBen Jan 05 '22 at 17:24
  • You are right. Is there a way to ignore the colon without changing the title list? Thanks. – ZVY545 Jan 05 '22 at 18:32
  • `replace` it perhaps? – BigBen Jan 05 '22 at 18:33
  • Thank you so much. I just tried import re, fname= [re.sub(r':', '', i) for i in title] – ZVY545 Jan 05 '22 at 18:39

0 Answers0