蟒。属性错误:'Text'对象没有属性'plot'



我在互联网https://python4astronomers.github.io/plotting/advanced.html上遵循这个指南,但是fig.add_subplot创建的轴对象不起作用。

给出的错误是:

ax_correl_1。情节(correlation_x_axis energy_correl_array’。$beta$ = {val:}'.format(val=i))AttributeError: 'Text'对象没有属性'plot'

import matplotlib.pyplot as plt
import scipy
from scipy.optimize import curve_fit
from matplotlib import rc
rc('text', usetex=True)
import numpy as np
import math
from ctypes import *
import sys
np.finfo(np.dtype("float64"))
correlation_elements = 5
correlation_x_axis = np.linspace(0, correlation_elements-1, correlation_elements)
energy_correl_array = [1,2,3,4,5]
abs_mag_correl_array = [1,2,3,4,5]
mag_correl_array = [1,2,3,4,5]
i = 0.3
fig_correl = plt.figure("correlations")
fig_correl.suptitle('Correlations')
ax_correl_1 = fig_correl.add_subplot(2, 2, 1).set_title('Energy')
ax_correl_2 = fig_correl.add_subplot(2, 2, 2).set_title('abs(Magnetization')
ax_correl_3 = fig_correl.add_subplot(2, 1, 2).set_title('Magnetization')
ax_correl_1.plot(correlation_x_axis, energy_correl_array,'.', label=r'$beta$ = {val:}'.format(val=i))
ax_correl_2.plot(correlation_x_axis, abs_mag_correl_array,'.', label=r'$beta$ = {val:}'.format(val=i))
ax_correl_3.plot(correlation_x_axis, mag_correl_array,'.', label=r'$beta$ = {val:}'.format(val=i))
y_max = np.max(mag_correl_array)
y_min = np.min(mag_correl_array)
ax_correl_1.set_ylim(y_min, y_max)
y_max = np.max(abs_mag_correl_array)
y_min = np.min(abs_mag_correl_array)
ax_correl_2.set_ylim(y_min, y_max)
y_max = np.max(energy_correl_array)
y_min = np.min(energy_correl_array)
ax_correl_3.set_ylim(y_min, y_max)
plt.show()

我建议你试试:

ax_correl_1 = fig_correl.add_subplot(2, 2, 1)
ax_correl_2 = fig_correl.add_subplot(2, 2, 2)
ax_correl_3 = fig_correl.add_subplot(2, 1, 2)
ax_correl_1.set_title('Energy')
ax_correl_2.set_title('abs(Magnetization')
ax_correl_3.set_title('Magnetization')

…而不是:

ax_correl_1 = fig_correl.add_subplot(2, 2, 1).set_title('Energy')
ax_correl_2 = fig_correl.add_subplot(2, 2, 2).set_title('abs(Magnetization')
ax_correl_3 = fig_correl.add_subplot(2, 1, 2).set_title('Magnetization')

我猜你的目的是为了成为一个"斧头";对象,其"情节";稍后尝试调用的方法。但是当你调用set_title()时当你实例化坐标轴时,你实际上不会得到一个"坐标轴"。对象返回,你会得到新的标题-即此"文本";对象-没有"plot()"方法。

希望你明白?

最新更新