Reputation: 995
this piece of code was working before, however, after creating a new environment , it stopped working for the line
plt.xticks(x, months, rotation=25,fontsize=8)
if i comment this line then no error, after putting this line error is thrown
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')
Upvotes: 35
Views: 95050
Reputation: 606
Happens when matplotlib failed to pack columns names in gist. Previously I had 5 columns and it was OK, next time I had 100+ columns and it caused an error.
Error in line:
g.set_xticklabels(df['code'], rotation=15, fontdict={'fontsize': 16})
Upvotes: 0
Reputation: 189
The questions seems to be a bit older but I just stumbled across a similar problem: the very same error occurred for me with the line
ax.set(xticklabels=..., xticks=...)
after an update to matplotlib version 3.2.2 which worked OK until then.
The set
method of an axis seems to call other methods according to the order of the arguments of set
. In older matplotlib versions, after unpacking the set-arguments there seems to have been some sort of ordering.
Rearranging the order of the parameter such that first the new number of xticks are set solved the problem for me:
ax.set(xticks=..., xticklabels=...)
Upvotes: 0
Reputation: 186
I also stumbled across the error and found that making both your xtick_labels and xticks a list of equal length works. So in your case something like :
def month(num):
# returns month name based on month number
num_elements = len(x)
X_Tick_List = []
X_Tick_Label_List=[]
for item in range (0,num_elements):
X_Tick_List.append(x[item])
X_Tick_Label_List.append(month(item+1))
plt.xticks(ticks=X_Tick_List,labels=X_Tick_LabeL_List, rotation=25,fontsize=8)
Upvotes: 17
Reputation: 101
I am using subplots and came across the same error. I've noticed that the error disappears if the axis being re-labelled (in my case the y-axis) shows all labels. If it does not, then the error you have flagged appears. I suggest increasing the chart height until all the y-axis labels are shown by default (See screenshots below).
Alternatively, I suggest using ax.set_xticks(...)
to define the FixedLocator
tick positions and then ax.set_xticklables(...)
to set the labels.
Every y-axis label drawn and over-written with custom labels
Upvotes: 10