0

I have a function that is returning a plot from a DF to show the cluster sizes in an ML model depending on different parameters.

I want to plot each of the different parameter combinations in subplots.

my thinkng was to append each of the plots to a list and plot from there. However, when I append the plot to the list it is giving me the same plot.

function to return the plots

def ngram_range(ranges, DF, DF_col, number_clusters, lst):
  CV = CountVectorizer(ngram_range=(ranges), analyzer='char')
  CV_values = CV.fit_transform(DF_col)
  clusters = MiniBatchKMeans(n_clusters=number_clusters, init_size=1024, batch_size=2048, random_state=20).fit_predict(CV_values)
  DF['Cluster'] = clusters
  new_DF = DF.groupby('Cluster')['Cluster'].count()
  plot = new_DF.plot(kind = 'bar', title = 'Cluster Frequency Ngram range from {} to {}'.format(ranges[0],ranges[1]), xlabel = 'Cluster', ylabel = 'Freq')
  lst.append(plot)
  return(lst)

loop to items to the list

ranges = [[2,2], [2,3], [2,4], [3,3],[3,4],[4,4]]
plots = []
for R in ranges:
  ngram_range(R,df_ru,df_ru['RU_desc'],8,plots)

I tried to solve this as above by appending the list inside the function but is still produces the same result.

1 Answer 1

1

the problem here is that pandas.plot() uses the last axes available to plot, therefore before you use the plot you need to create a new axes.

def ngram_range(ranges, DF, DF_col, number_clusters, lst):
  CV = CountVectorizer(ngram_range=(ranges), analyzer='char')
  CV_values = CV.fit_transform(DF_col)
  clusters = MiniBatchKMeans(n_clusters=number_clusters, init_size=1024, batch_size=2048, random_state=20).fit_predict(CV_values)
  DF['Cluster'] = clusters
  new_DF = DF.groupby('Cluster')['Cluster'].count()
  fig, ax = plt.subplots() # create the new figure and axes
  plot = new_DF.plot(kind = 'bar', title = 'Cluster Frequency Ngram range from {} to {}'.format(ranges[0],ranges[1]), xlabel = 'Cluster', ylabel = 'Freq', ax=ax)
  lst.append([fig, plot])
  plt.show()  # use this line in case you want to plot every time the function is call
  return(lst)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.