Home > Net >  How to set tick frequency of ylim in matplotlib
How to set tick frequency of ylim in matplotlib

Time:11-26

I have the following for graph blot. I need the ylim to be step size of 10 and should start from 10 till 70 as shown in figure.

plt.figure(figsize=(3,2))
no = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]


measured_tech_1= [37.506, 37.506, 37.506, 37.506, 37.506, 37.506, 37.506, 37.506, 37.506, 37.506, 37.506,37.506, 37.506]
predicted_tech_1= [38.811, 38.811, 38.811, 38.811, 38.811, 38.811, 38.811, 38.811, 38.811, 38.811, 38.811,38.811, 38.811]
measured_tech_2 = [4.735, 8.451, 10.702, 12.857, 14.900, 16.908, 19.233, 21.328, 23.351, 25.403, 27.580, 29.939,32.832]
predicted_tech_2 = [2.945, 6.989, 8.294, 10.968, 13.643, 16.506, 19.261, 21.658, 24.056, 26.454, 29.175, 32.240,34.795]
measured_tech_3 = [38.495, 38.495, 38.495, 38.495, 38.495, 38.771, 38.771, 38.771, 38.771, 38.616, 38.616, 38.616,38.616]
predicted_tech_3 = [35.255, 35.351, 35.463, 35.492, 35.181, 35.410, 35.483, 35.453, 35.329, 35.146, 34.6130,35.407, 35.500]

plt.plot(no,measured_tech_1 , color='orange', marker='^',linestyle='dashed',label='Measured-Technique 1')
plt.plot(no,predicted_tech_1 , color='orange', marker='*',linestyle='dashed',label='Predicted-Technique 1')

plt.plot(no, measured_tech_2, color='purple', marker='^', linestyle='dashed',label='Measured-Technique 2')
plt.plot(no, predicted_tech_2, color='purple', marker='*'linestyle='dashed',label='Predicted-Technique 2')

plt.plot(no, measured_tech_3, color='green', marker='^',linestyle='dashed',label='Measured-Technique 3')
plt.plot(no, predicted_tech_3, color='green', marker='*',linestyle='dashed',label='Predicted-Technique 3')

plt.ylim(10, 70)

plt.xticks(no,fontsize='8')
plt.legend(loc='upper right',prop={'size': 3.5})
plt.xlabel('No.')
plt.ylabel('Latency')

Figure

CodePudding user response:

Use one of the

  • FixedLocator for explicit range

    plt.gca().yaxis.set_major_locator(ticker.FixedLocator(range(10, 80, 10)))
    
  • LinearLocator for total ticks

    plt.gca().yaxis.set_major_locator(ticker.LinearLocator(7))
    
  • MaxNLocator for max ticks

    plt.gca().yaxis.set_major_locator(ticker.MaxNLocator(7))
    
    • Related