gpt4 book ai didi

python-3.x - ValueError : The number of FixedLocator locations (5), 通常来自对 set_ticks 的调用,与刻度标签的数量不匹配 (12)

转载 作者:行者123 更新时间:2023-12-04 11:11:48 24 4
gpt4 key购买 nike

这段代码之前可以工作,但是,在创建新环境后,它停止为该行工作

plt.xticks(x, months, rotation=25,fontsize=8)
如果我注释此行,则没有错误,放置此行后会抛出错误
ValueError: The number of FixedLocator locations (5), usually from a call to set_ticks, does not match the number of ticklabels (12).
import numpy as np
import matplotlib.pyplot as plt

dataset = df
dfsize = dataset[df.columns[0]].size
x = []
for i in range(dfsize):
x.append(i)

dataset.shape
# dataset.dropna(inplace=True)
dataset.columns.values
var = ""
for i in range(dataset.shape[1]): ## 1 is for column, dataset.shape[1] calculate length of col

y = dataset[dataset.columns[i]].values
y = y.astype(float)
y = y.reshape(-1, 1)
y.shape
from sklearn.impute import SimpleImputer

missingvalues = SimpleImputer(missing_values=np.nan, strategy='mean', verbose=0)
missingvalues = missingvalues.fit(y)
y = missingvalues.transform(y[:, :])

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer

labelencoder_x = LabelEncoder()
x = labelencoder_x.fit_transform(x)

from scipy.interpolate import *

p1 = np.polyfit(x, y, 1)
# from matplotlib.pyplot import *
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

plt.figure()
plt.xticks(x, months, rotation=25,fontsize=8)
#print("-->"+dataset.columns[i])
plt.suptitle(dataset.columns[i] + ' (xyz)', fontsize=10)
plt.xlabel('month', fontsize=8)
plt.ylabel('Age', fontsize=10)
plt.plot(x, y, y, 'r-', linestyle='-', marker='o')
plt.plot(x, np.polyval(p1, x), 'b-')
y = y.round(decimals=2)
for a, b in zip(x, y):
plt.text(a, b, str(b), bbox=dict(facecolor='yellow', alpha=0.9))

plt.grid()
# plt.pause(2)
# plt.grid()

var = var + "," + dataset.columns[i]
plt.savefig(path3 + dataset.columns[i] + '_1.png')
plt.close(path3 + dataset.columns[i] + '_1.png')
plt.close('all')

最佳答案

我正在使用子图并遇到相同的错误。我注意到如果轴被重新标记(在我的例子中是 y 轴)显示所有标签,错误就会消失。如果没有,则会出现您标记的错误。我建议增加图表高度,直到默认显示所有 y 轴标签(请参见下面的屏幕截图)。
或者,我建议使用 ax.set_xticks(...)定义 FixedLocator勾选位置,然后 ax.set_xticklables(...)设置标签。
Ever 2nd y-axis label drawn
Every y-axis label drawn and over-written with custom labels

关于python-3.x - ValueError : The number of FixedLocator locations (5), 通常来自对 set_ticks 的调用,与刻度标签的数量不匹配 (12),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62953704/

24 4 0