Issue
I am writing code for my internship and I am adding the latitude and longitude of the area we are viewing. The section of code giving me trouble looks like this. I want to be able to have the number and then a W to show it is degrees west for my X axis and a N for my Y axis to show that it is degrees north. I have tried to use plt.ylabel("Degrees North") and that did not work as well. Any help would be greatly appreciated.
ax.set_xticks([-94, -96, -98, -100, -102, -104])
ax.set_yticks([28, 30, 32, 34, 36])
secax = ax.secondary_yaxis(1.0)
secax.set_yticks([28, 30, 32, 34, 36])
This is what the output of my code looks like
Solution
Once you plot your graph, you can get the labels using get_xticks()
. Add a ' W' or ' N' and use set_yticklabels
to update the labels. As your labels are int, you will need to convert them to str just before plotting. As the map is that of Texas, assuming there only North and West to be added.
Sample below has a blank matplotlib plot which gets updated.
Code
fig, ax=plt.subplots(figsize=(6,6))
plt.setp(ax, xlim = (-104, -94))
plt.setp(ax, ylim = (28, 36))
ax.set_xticks([-94, -96, -98, -100, -102, -104])
ax.set_yticks([28, 30, 32, 34, 36])
secax = ax.secondary_yaxis(1.0)
secax.set_yticks([28, 30, 32, 34, 36])
#For x-axis
labels=ax.get_xticks().tolist()
new_labels = [str(l) + " W" for l in labels]
ax.set_xticklabels(new_labels)
#For y-axis (left)
labels=ax.get_yticks().tolist()
new_labels = [str(l) + " N" for l in labels]
ax.set_yticklabels(new_labels)
#For y-axis (right)
labels=secax.get_yticks().tolist()
new_labels = [str(l) + " N" for l in labels]
secax.set_yticklabels(new_labels)
Output plot
Answered By - Redox
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.