2
$\begingroup$

I am trying to make a graph of k (as in k-means) vs error, and I can't get it to show the actual number of k on the x-axis (1-15, not just the even numbers), nor any any numbers at all if I add a title to X.

I have looked at the data frame documentation and the matplotlib axes documentation, and I couldn't find anything telling me how to get it to show the numbers? Even if I have to manually specify them?

Please see images below:

rdf.plot.line(title='Number of clusters Vs mean accuracy')

graph with bad xaxis numbers

rdf.plot.line(title='Number of clusters Vs mean accuracy',
                x='parameters',
                y='mean_validation_score')

enter image description here

I don't understand how to get the x axis to show all 15 numbers (1-15).

$\endgroup$
  • $\begingroup$ Are the values of $k$ in your DataFrame? $\endgroup$ – Adrian Keister Sep 12 '18 at 15:49
  • 1
    $\begingroup$ They are, in the form of a column of dicts: 0 {'n_clusters': 1} 1 {'n_clusters': 2} 2 {'n_clusters': 3} etc. $\endgroup$ – Chor Hatara Hud'u Keturi Sep 12 '18 at 15:53
  • $\begingroup$ I would collapse that column into a purely numeric column, so that you can set it as the x= part of your plot command. $\endgroup$ – Adrian Keister Sep 12 '18 at 15:56
  • 1
    $\begingroup$ Thanks @AdrianKeister , turns out that was (most of) the answer I used in the end. I extracted a list of the numbers and added them as the xtick parameter. $\endgroup$ – Chor Hatara Hud'u Keturi Sep 12 '18 at 20:25
  • $\begingroup$ You're quite welcome! $\endgroup$ – Adrian Keister Sep 12 '18 at 20:27
2
$\begingroup$

Use xticks. e.g.

x=np.arange(1,16)
y = -60000*(3+np.log(1/x))
plt.plot(x,y,'b')
plt.xticks(x)
plt.show()

example of xticks

$\endgroup$
  • $\begingroup$ Thanks, I would upvote it but I don't have enough popularity points. $\endgroup$ – Chor Hatara Hud'u Keturi Sep 12 '18 at 20:13
  • $\begingroup$ @ChorHataraHud'uKeturi No worries. Glad it helps. $\endgroup$ – user12075 Sep 12 '18 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.