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

**URL:** <https://discourse.julialang.org/t/possible-to-plot-ylabels-and-yticks-on-left-and-right-using-twinx-and-pyplot/75549>\
**Category:** General Usage\
**Tags:** question, pyplot, plots\
**Created:** [January 31, 2022, 10:33pm UTC](https://discourse.julialang.org/t/possible-to-plot-ylabels-and-yticks-on-left-and-right-using-twinx-and-pyplot/75549 "2022-01-31T22:33:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![g2g](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@g2g](https://discourse.julialang.org/u/g2g)\
**Post date:** [January 31, 2022, 10:33pm UTC](https://discourse.julialang.org/t/possible-to-plot-ylabels-and-yticks-on-left-and-right-using-twinx-and-pyplot/75549/1 "2022-01-31T22:33:56Z")

</div>

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](https://discourse.julialang.org/t/show-right-y-axis-using-twinx-with-plots-pyplot/30750) 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:

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/5/3/53bca4c557e3bd2ebb3ca529f35ac663da350a81.png)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 31, 2022, 11:41pm UTC](https://discourse.julialang.org/t/possible-to-plot-ylabels-and-yticks-on-left-and-right-using-twinx-and-pyplot/75549/2 "2022-01-31T23:41:54Z")

</div>

Try this:

 ![tstplt6](https://global.discourse-cdn.com/julialang/original/3X/3/4/34f3eceda6513b641860ea3ea85410e00855b0d2.png)

> **Code**
>
> ```julia
> 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()
> 
> ```

---

<div class="post-metadata">

**Author:** ![g2g](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@g2g](https://discourse.julialang.org/u/g2g)\
**Post date:** [February 1, 2022, 12:10am UTC](https://discourse.julialang.org/t/possible-to-plot-ylabels-and-yticks-on-left-and-right-using-twinx-and-pyplot/75549/3 "2022-02-01T00:10:22Z")

</div>

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.
