1

How to set equal bandwidth between xticks in matplotlib.

I am plotting graph and for x value ticks and i want my bin to be exact as related to my values.

my xticks are = [0.001,0.01,0.1,1,10,100]

sns.set_style("whitegrid")

plt.plot(tuned_parameter,accuracy_list)

plt.xlabel("Hyperparameter")

plt.ylabel("accuracy")

plt.xticks(tuned_parameter) 

plt.title("Plot for accuracy with different hyperparameter")
DavidG
  • 24,279
  • 14
  • 89
  • 82
Vishal Suryavanshi
  • 355
  • 1
  • 4
  • 15
  • Possible duplicate of [Changing the "tick frequency" on x or y axis in matplotlib?](https://stackoverflow.com/questions/12608788/changing-the-tick-frequency-on-x-or-y-axis-in-matplotlib) – Sheldore Jan 22 '19 at 18:20
  • If I understood this question correctly, you want to have equal spacing between the mentioned ticks, which are not equally spaced. I think what you are looking for is a log scale on the x-axis, which you find here: https://stackoverflow.com/questions/773814/plot-logarithmic-axes-with-matplotlib-in-python – krm Jan 23 '19 at 12:02

2 Answers2

0

If you simply want to set the ticks as the ones you mention, why don't you simply use:

plt.xticks([0.001,0.01,0.1,1,10,100])

Remember, the plt.xticks() takes as an argument a list. An empty list will remove the ticks altogether.

If you have the plt.xlim() up to 10 for example, this command will increase your limit to include the ticks.

And since I sense a semilog plot, why don't you use plt.semilogx(...) instead?

Alex Rpd
  • 75
  • 5
0

You can simply use your defined variable inside plt.xticks(my_variable). As it takes list as an argument. More details about ticks can be found here,

https://matplotlib.org/api/_as_gen/matplotlib.pyplot.xticks.html

Tanvir
  • 154
  • 14
  • but they are not coming in equal range of bins as my values are [0.001,0.01,0.1,1,10,100] , os its plotting 0.001 at extreme left and 100 at extreme right .. – Vishal Suryavanshi Jan 22 '19 at 18:47
  • @VishalSuryavanshi: To get more help, please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Sheldore Jan 22 '19 at 18:58
  • @VishalSuryavanshi please edit your post with an example that we can run and see the outcome. Because generally using above method we can plot what you are expecting. – Tanvir Jan 22 '19 at 20:27