0
import matplotlib.pyplot as plt
import pandas as pd


#750k_mesh
df1=pd.read_excel("Filelocation/tcdata_750K_mesh.xlsx", "tcdata_750K_mesh", usecols='B:E',skiprows=5 )
df1_x1 = pd.DataFrame(df1, columns= ['Unnamed: 1'])
df1_y1 = pd.DataFrame(df1, columns= ['Unnamed: 2'])
df1_y2 = pd.DataFrame(df1, columns= ['Unnamed: 3'])
df1_y3 = pd.DataFrame(df1, columns= ['Unnamed: 4'])

#1.5M_mesh
df2=pd.read_excel("Filelocation//tcdata_1.5M_mesh.xlsx", "tcdata_1.5M_mesh", usecols='B:E',skiprows=5 )
df2_x1 = pd.DataFrame(df2, columns= ['Unnamed: 1'])
df2_y1 = pd.DataFrame(df2, columns= ['Unnamed: 2'])
df2_y2 = pd.DataFrame(df2, columns= ['Unnamed: 3'])
df2_y3 = pd.DataFrame(df2, columns= ['Unnamed: 4'])

#3M_mesh
df3=pd.read_excel("Filelocation//tcdata_3M_mesh.xlsx", "tcdata_3M_mesh", usecols='B:E',skiprows=5 )
df3_x1 = pd.DataFrame(df3, columns= ['Unnamed: 1'])
df3_y1 = pd.DataFrame(df3, columns= ['Unnamed: 2'])
df3_y2 = pd.DataFrame(df3, columns= ['Unnamed: 3'])
df3_y3 = pd.DataFrame(df3, columns= ['Unnamed: 4'])

#9M_mesh
df4=pd.read_excel("Filelocation//tcdata_9M_mesh.xlsx", "tcdata_9M_mesh", usecols='B:E',skiprows=5 )
df4_x1 = pd.DataFrame(df4, columns= ['Unnamed: 1'])
df4_y1 = pd.DataFrame(df4, columns= ['Unnamed: 2'])
df4_y2 = pd.DataFrame(df4, columns= ['Unnamed: 3'])
df4_y3 = pd.DataFrame(df4, columns= ['Unnamed: 4'])

linewidth=1
markersize=0.25
markernumber=10

ax1 = plt.plot(df1_x1,df1_y1,'b-', linewidth=linewidth,markevery=markernumber,label='750K'),
ax1 = plt.plot(df2_x1,df2_y1,'g-', linewidth=linewidth,markevery=markernumber,label='1.5M'),
ax1 = plt.plot(df3_x1,df3_y1,'r-',linewidth=linewidth,markevery=markernumber,label='3M'),
ax1 = plt.plot(df4_x1,df4_y1,'c-', linewidth=linewidth,markevery=markernumber,label='9M'),


ax1 = plt.ylabel('Temperature (''$^\circ$''C)'),
ax1 = plt.xlabel('Time (s)'),

ax1 = plt.axis([-1,17, 1200,1550])
ax1 = plt.xticks(fontsize=10)
ax1 = plt.yticks(fontsize=10)

ax1 = leg = plt.legend(loc=1,fontsize=10)
ax1 = leg.get_frame().set_edgecolor('k') 
ax1 = leg.get_frame().set_facecolor('w')



# this is an inset axes over the main axes
ax2 = plt.axes([.22, .22, .28, .28], facecolor='w') #x1,y1, x2, y2 location of the subplot in the current figure
ax2 = plt.plot(df1_x1,df1_y1,'b-', linewidth=linewidth,markevery=markernumber,label='750K'),
ax2 = plt.plot(df2_x1,df2_y1,'g-', linewidth=linewidth,markevery=markernumber,label='1.5M'),
ax2 = plt.plot(df3_x1,df3_y1,'r-',linewidth=linewidth,markevery=markernumber,label='3M'),
ax2 = plt.plot(df4_x1,df4_y1,'c-', linewidth=linewidth,markevery=markernumber,label='9M'),
ax2 = ax3=plt.axis([12,14, 1280,1320])
ax2 = plt.xticks(fontsize=7)
ax2 = plt.yticks(fontsize=7)


fig_1 = plt.figure(1)
fig_2 = fig_1.add_subplot(111)
fig_2.grid(color='#929591', linestyle='--', linewidth=0.5)

plt.show()
plt.draw()
fig_1.savefig('grid_independency_test.png', dpi=1000)

The code is able to plot the figure I am looking for. However, it does not save the image file to the directory. Here is the image:

enter image description here

Here is the warning it gives:

MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.

fig_2 = fig_1.add_subplot(111)
<Figure size 432x288 with 0 Axes>

I tried to look online at what I might be doing wrong but did not find any solution. I would appreciate any help with where I am doing wrong.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • Hi there! thanks for replying. Nope, it did not solve the problem. Still showing the same warning: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance. fig_2 = fig_1.add_subplot(111) – Moinuddin Shuvo Feb 13 '23 at 19:27
  • 1
    The error tells you that you're overwriting the plot instance each time you do `ax = plt.plot(...)`. You should create an instance with `fig, ax1 = plt.subplots()` and then use the instance in subsequent plots with `ax1.plot(...)`. – Trenton McKinney Feb 13 '23 at 20:52
  • Hi, Thank you for your answer and help. I did edit the plots as in fig, ax1 = plt.subplots() as you mentioned. But it still not saving the image. – Moinuddin Shuvo Feb 14 '23 at 06:24

0 Answers0