До конца задачи осталось совсем немного - надо просто сложить методы для двух областей с методами для двух графиков вместе.
Если это решать последовательным методом то вот код:
# верхняя область
plt.subplot(211)
plt.plot( x,y, label='Model', marker='x')
plt.plot( x,yr ,color = 'green', marker='D', linestyle='dashed', label='Real')
plt.xlabel('X')
plt.ylabel('Y', color='red', fontsize=14)
plt.title('First Subplot', color='green', fontsize=14)
plt.legend(loc ='lower right')
plt.gca().twinx()
plt.plot( x,y2,color = 'black',label='Model Y2', marker='x')
plt.plot( x,yr2 ,color = 'red', marker='o', linestyle='dashed', label='Real Y2')
plt.ylabel('Y2')
plt.legend(loc = 'upper center')
# нижняя область
plt.subplot(212)
plt.plot( x,y, label='Model 2 ', marker='x')
plt.plot( x,yr ,color = 'green', marker='D', linestyle='dashed', label='Real 2')
plt.xlabel('X')
plt.ylabel('Y', color='red', fontsize=14)
plt.title('Second Subplot', color='green', fontsize=14)
plt.legend(loc ='lower right')
plt.gca().twinx()
plt.plot( x,y2,color = 'black',label='Model 2 Y2', marker='x')
plt.plot( x,yr2 ,color = 'red', marker='o', linestyle='dashed', label='Real 2 Y2')
plt.ylabel('Y2')
plt.legend(loc = 'upper center')
plt.subplots_adjust(hspace=0.70)
Результат выполнения:
Если это решать путем создания переменных для холста и для осей то вот код:
fig, ( ax_up, ax_btm) = plt.subplots(2,1)
# верхняя область
ax_up.plot( x,yr ,color = 'green', marker='D', linestyle='dashed', label='Real')
ax_up.plot( x,y, label='Model', marker='x')
ax_up.set_xlabel('X')
ax_up.set_ylabel('Y', color='red', fontsize=14)
ax_up.set_title('First Subplot', color='green', fontsize=14)
ax_up.legend(loc ='lower right')
ax_up2 = ax_up.twinx()
ax_up2.plot( x,y2,color = 'black',label='Model Y2', marker='x')
ax_up2.plot( x,yr2 ,color = 'red', marker='o', linestyle='dashed', label='Real Y2')
ax_up2.set_ylabel('Y2')
ax_up2.legend(loc = 'upper center')
# нижняя область
ax_btm.plot( x,yr ,color = 'green', marker='D', linestyle='dashed', label='Real')
ax_btm.plot( x,y, label='Model', marker='x')
ax_btm.set_xlabel('X')
ax_btm.set_ylabel('Y', color='red', fontsize=14)
ax_btm.set_title('Second Subplot', color='green', fontsize=14)
ax_btm.legend(loc ='lower right')
ax_btm2 = ax_btm.twinx()
ax_btm2.plot( x,y2,color = 'black',label='Model Y2', marker='x')
ax_btm2.plot( x,yr2 ,color = 'red', marker='o', linestyle='dashed', label='Real Y2')
ax_btm2.set_ylabel('Y2')
ax_btm2.legend(loc = 'upper center')
plt.subplots_adjust(hspace=0.70)
При этом получается такая же картинка.
Задача решена.
No comments:
Post a Comment