Possible to plot ylabels and yticks on left and right using twinx() and pyplot?

I’m having trouble making a plot with labels and ticks on the left and right sides. Perhaps this capability has stopped working? According to this question from a couple years ago, it looks like it was working then. However, that code does not produce the secondary label on the right side for me, using Julia 1.7.1. Looking at the available attributes using plotattr(:Axis) and plotattr(:Plot), I thought maybe tick_direction would help but it didn’t. And the layout attribute looked promising but it appears that is for laying out subplots not labels and ticks. Here’s my code and the resulting plot:

using Plots
pyplot()
function tstplt6()
	a = 1:3
	b = [1, 2, 7]
	plot(a, b, 
		label = "randData", 
		xlabel = "numbers", 
		ylabel = "Rand data",
		color = :red, 
		legend = :topleft,
		tick_direction = :in,
		grid = :on)
	p = twinx()
	plot!(p, a, log.(a), 
		label = "log(x)", 
		ylabel = "The right Y label",
		color = :green,
		legend = :topright,
		tick_direction = :out,
		grid = :on,
		box = :on)
	savefig("tstplt6.png")
end

tstplt6

Try this:

Code
using Measures, Plots; pyplot(dpi=600)

function tstplt6()
	a = 1:3
	b = [1, 2, 7]
	plot(a, b, 
		label = "randData", 
		xlabel = "numbers", 
		ylabel = "Rand data",
		color = :red, 
		legend = :topleft,
		tick_direction = :in,
		grid = :on, left_margin=2cm, right_margin=8cm, top_margin=2cm)
	plot!(twinx(), a, log.(a), 
		label = "log(x)", 
		ylabel = "The right Y label",
		color = :green,
		legend = :bottomright,
        grid = :on, left_margin=2cm, right_margin=8cm, top_margin=2cm)
	savefig("tstplt6.png")
end

tstplt6()
2 Likes

I wasn’t aware of the capability to set the plot margins and, even if I were aware, I would have guessed that not having enough right margin would have caused right truncation not moving the right labels into the left margin. Thanks for your help.