So I am a fairly new python programmer and have been stuck on this minor issue for way longer then I should be. I have this plot:
and as you can see only select values appear as scientific notation.
I would like all values to appear in scientific notation (i.e. 1e1, 1e2 ... and so on) Here is one of the attempts (of many) which I have tried:
fig2 = plt.figure(2)
ax = plt.gca()
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
plt.xlabel('gamma')
plt.ylabel('C')
plt.imshow(scores, interpolation='nearest', cmap=plt.cm.hot,
norm=MidpointNormalize(vmin=0.2, midpoint=0.7))
plt.colorbar()
ax.set_yticks(range(len(C_range)))
ax.set_yticklabels(C_range)
ax.set_xticks(range(len(gamma_range)))
ax.set_xticklabels((gamma_range), rotation=45)
plt.title('Validation accuracy')
plt.tight_layout()
fig2.show()
Line #3 in the code above doesnt appear to be doing anything, so I thought i could move it after the set_xticks
portion, since I am assuming those lines overwrite the ticklabel_format
command. So tired the following:
fig2 = plt.figure(2)
ax = plt.gca()
plt.xlabel('gamma')
plt.ylabel('C')
plt.imshow(scores, interpolation='nearest', cmap=plt.cm.hot,
norm=MidpointNormalize(vmin=0.2, midpoint=0.7))
plt.colorbar()
ax.set_yticks(range(len(C_range)))
ax.set_yticklabels(C_range)
ax.set_xticks(range(len(gamma_range)))
ax.set_xticklabels((gamma_range), rotation=45)
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
plt.title('Validation accuracy')
plt.tight_layout()
fig2.show()
Which results in:
AttributeError: This method only works with the
ScalarFormatter
.
I have read numerous examples on how to do something like this, but everytime I follow what has been done, either nothing happens or I get the same error.
Could someone explain to what exactly is going on here? Does ax.set_yticks
change the formatter? I have not explictly chosen a formatter, is that something I need to do? And how can I force the out put to what I am looking for?
So I found a work around for my needs, although exactly why xticks/yticks overwrites the formatter is beyond me. If someone wanted to clear that up a bit for me, I am all ears since I am rather interested in how it works.
I simply changed my C_range and gamma_range arrays to lists of strings and pass them into xticks/yticks
fmt_C = ['%.0e' % i for i in C_range.tolist()]
fmt_gamma = ['%.0e' % i for i in gamma_range.tolist()]
plt.xticks(np.arange(len(gamma_range)), fmt_gamma, rotation=45)
plt.yticks(np.arange(len(C_range)), fmt_C)