Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib set_xticklabels misaligned

I have a double y-axis plot and I can't get the xticks labels working.
This is when I don't modify the xticks: enter image description here This is when I do: enter image description here

neurons = [4, 8, 16, 32, 64, 128]
fig, ax = plt.subplots(figsize=(13,7))
ax2 = ax.twinx()
# adding the plots
ax.grid(False)
ax2.grid(False)
ax.set_xticklabels(neurons)
ax.set_yticklabels(np.arange(250, 2500,250),fontsize=14)
ax2.set_yticklabels(np.arange(0, 120,20),fontsize=14)
ax.set_ylabel('Training time [s]', fontsize=18)
ax2.set_ylabel('Model avg accuracy [%]', fontsize=18)
ax.set_xlabel('Number of neurons', fontsize=18)
ax2.legend(fontsize=15)
plt.show()
like image 1000
Alessandro Gaballo Avatar asked Sep 27 '26 06:09

Alessandro Gaballo


1 Answers

You are setting your new x tick labels onto the automatically generated xticks. You probably want to set the tick locations to the values of neruons:

ax.set_xticks(neurons)
ax.set_xticklabels(neurons)

which gives something like

enter image description here

like image 60
DavidG Avatar answered Sep 28 '26 21:09

DavidG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!