Issue
Draw a simple graph and a red horizontal line.
import matplotlib.pyplot as plt
plt.plot([2,3,4,5])
plt.axhline(y = 3.5, color = 'r')
plt.show()
I want to add a new y tick at the right side for the new red horizontal line,how to make it as below?
Solution
You can use a secondary y-axis to position and show the tick at the right. A secondary axis differs from a "twin axis": the secondary axis stays nicely aligned with the main axis, while a twin axis is meant to add plot elements with an independent scale.
The example code below changes the "plt" interface for the "object-oriented" interface, which is more appropriate for complex plots.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([2, 3, 4, 5])
y_special = 3.5
ax.axhline(y=y_special, color='r')
sec_ax = ax.secondary_yaxis('right')
sec_ax.set_yticks([y_special], ["1.0"])
plt.show()
Answered By - JohanC
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.