I would like to show the y values every 0.5 units, but leave the ticks frequency as it is now. How can I do it?
This is my code:
#plotting
ax1.errorbar('Sampling', 'y', data=df3_sub_DP)
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
ax2.errorbar('Sampling', 'DES', data=df3_sub_DP)
ax2.set(yticklabels=[])
ax2.set(ylabel=None)
# tidy up the figure
ax1.set_ylim((0, 2))
ax2.set_ylim((0, 0.7))
#### some attempts but not working
#ax1.yaxis.set_ticks(np.arange(0, 2, 0.5))
#ax1.yaxis.set_major_locator(plt.MaxNLocator(6))
plt.show()
Thank you
You can make use of major and minor ticks:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# Some data and the plot
x = np.linspace(0, 2, 20)
y = .5 * x + 1
ax = sns.lineplot(x=x, y=y)
# The labels you want
minor_ticks = np.arange(1, 2.1, .5) # y values for every .5 units
# The ticks you want
major_ticks = [1.2, 1.7, 1.9] # I've choosen some. You use yours
# Joining things together
g.set_yticks(minor_ticks, minor=True)
g.set_yticklabels(minor_ticks, minor=True)
g.set_yticks(major_ticks, minor=False)
g.set_yticklabels([], minor=False) # No labels
# And here's the trick: we set minor ticks length to zero,
# so that only the labels are shown:
ax.tick_params(which="minor", axis="y", length=0)
plt.show()