How to add extra sign to already existing x-ticks label matplotlib?
Question:
Currently, my histogram plot’s x-tick labels are [200. 400. 600. 800. 1000. 1200. 1400.]. It represents the rate in dollars. I want to add $ in prefix of these ticks like [$200 $400 $600 $800 $1000 $1200 $1400].
I tried this axs.xaxis.get_majorticklocs()
to fetch x-tick labels and axs.xaxis.set_ticks([f"${amount:.0f}" for amount in axs[0, 1].xaxis.get_majorticklocs()])
to set the updated one. I understand that It will throw error as x-tick labels are now string.
Need help! kindly assist how can I do this.
Answers:
You might be looking for set_xticklabels
, which says:
Set the xaxis’ labels with list of string labels.
Here you want to set the ticks to [200. 400. 600. 800. 1000. 1200. 1400.]
and the labels to your string values to ensure it is all aligned.
Currently, my histogram plot’s x-tick labels are [200. 400. 600. 800. 1000. 1200. 1400.]. It represents the rate in dollars. I want to add $ in prefix of these ticks like [$200 $400 $600 $800 $1000 $1200 $1400].
I tried this axs.xaxis.get_majorticklocs()
to fetch x-tick labels and axs.xaxis.set_ticks([f"${amount:.0f}" for amount in axs[0, 1].xaxis.get_majorticklocs()])
to set the updated one. I understand that It will throw error as x-tick labels are now string.
Need help! kindly assist how can I do this.
You might be looking for set_xticklabels
, which says:
Set the xaxis’ labels with list of string labels.
Here you want to set the ticks to [200. 400. 600. 800. 1000. 1200. 1400.]
and the labels to your string values to ensure it is all aligned.