How to rotate the secondary label of the y axis so that it does not overlap with y-ticks, matplotlib

I try to rotate my secondary y-label by 270 degrees, but when I do this by passing an argument rotate=270, it overlaps my y-tick text , Any ideas how to fix this?

fig, ax = plt.subplots()

ax.plot(df.index,df.tripTime,label='Fishing Effort', marker='D')
ax2=ax.twinx()
ax2.plot(tr.index,tr.cost, 'g',label='Fuel Expenditure', marker='d')

lines = ax.get_lines() + ax2.get_lines()
ax.legend(lines,[line.get_label() for line in lines], loc='lower left')
ax.set_ylim((0, 18))

ax2.set_ylabel('Cost ($)',color='g', rotation=270)

for tl in ax2.get_yticklabels():
    tl.set_color('g')

ax.set_ylabel('Fishing Effort (hrs)')
ax.set_xlabel('Time (days)')
plt.show()

enter image description here

+4
source share
1 answer

UPDATE: this answer is not very good, look at the comments!

This seems like a bug, and you should probably report this to the matplotlib issue tracker .

While it is being fixed, a quick solution is to set the complement of shortcuts to a higher value:

ax2.set_ylabel('Cost ($)', color='g', rotation=270, labelpad=15)

Result

+6
source

Source: https://habr.com/ru/post/1626041/


All Articles