0

here is my code

import matplotlib.pyplot as plt
import numpy as np
fig, (ax1, ax2) = plt.subplots(2,1)
fig.subplots_adjust(hspace=0.5)
data1 = np.loadtxt('val_acc_1.txt')
data2 = np.loadtxt('val_acc_2.txt')

ax1=plt.subplot(2,1,1)
plt.plot(data1[:,0],data1[:,1],'y')
ax1.set_xlim(0,20)
ax1.set_xlabel('epoch')
ax1.set_ylabel('accuarcy')
ax1.set_title('transfer learning')

plt.subplot(2,1,2)
ax2=plt.plot(data2[:,0],data2[:,1],'r')
ax2.set_xlim(0,20)
ax2.set_xlabel('epoch')
ax2.set_ylabel('accuarcy')
ax2.set_title('no transfer learning')


plt.show()

the figure should be like thisenter image description here When I comment out

#ax2.set_xlim(0,20)
#ax2.set_xlabel('epoch')
#ax2.set_ylabel('accuarcy')
#ax2.set_title('no transfer learning')

the code could work partly, and the ERROR still here.

but the ERROR is this enter image description here thank you

  • the error is AttributeError:'list' object has no attribute 'set_xlim' , and I could not figure this, please help me, thank you. – hfkagayaki Dec 28 '20 at 02:00
  • `ax2=plt.plot(data2[:,0],data2[:,1],'r')` is wrong. It should be `ax2.plot(data2[:,0],data2[:,1],'r')`. Also, you shouldn't use `plt.subplot()` if you used `plt.subplots()`. You are mixing the "old" matplotlib interface with the "new" object oriented interface. See [this introduction](https://matplotlib.org/matplotblog/posts/pyplot-vs-object-oriented-interface/) – JohanC Dec 28 '20 at 02:14
  • thanks for your help, the introduction said use “ax2=ax1.twins()”, but I want to plot figures into 2 subplots, how to do this? – hfkagayaki Dec 28 '20 at 02:33
  • `ax1.twinx()` ending with "x", creates a second y-axis in the same subplot ([doc with links to examples](https://matplotlib.org/3.3.3/api/_as_gen/matplotlib.axes.Axes.twinx.html). To create two subplots, use `fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1)` and remove the calls `ax1=plt.subplot(2,1,1)` and `plt.subplot(2,1,2)`. Use `ax1.plot(...)` and `ax2.plot(...)` to draw onto the subplots. – JohanC Dec 28 '20 at 02:46
  • this problem has been solved by your help, Thank you very much! have a good day friend. :) – hfkagayaki Dec 29 '20 at 02:44

0 Answers0