# Location of the legend in PyPlot

**URL:** <https://discourse.julialang.org/t/location-of-the-legend-in-pyplot/1311>\
**Category:** New to Julia\
**Tags:** question, pyplot\
**Created:** [January 5, 2017, 6:42pm UTC](https://discourse.julialang.org/t/location-of-the-legend-in-pyplot/1311 "2017-01-05T18:42:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aleksandr\_Mikheev](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@Aleksandr\_Mikheev](https://discourse.julialang.org/u/Aleksandr_Mikheev)\
**Post date:** [January 5, 2017, 6:42pm UTC](https://discourse.julialang.org/t/location-of-the-legend-in-pyplot/1311/1 "2017-01-05T18:42:20Z")

</div>

Hello, I am currently using PyPlot package for visualization. I tried to pull my legend outside the box like in the [Legend guide](http://matplotlib.org/users/legend_guide.html#controlling-the-legend-entries):

```julia
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

```

However, the result cuts my legend. Here is the part of my figure which contains a legend:

![](https://global.discourse-cdn.com/julialang/original/3X/2/5/255d94bc674ead42dcf47330f4ea56b797bcc739.png)

What should I do in order to have a legend, say, like this

 ![](https://global.discourse-cdn.com/julialang/original/3X/3/a/3a988e0e3b49a244f4e94ba7c19687a27974d0d1.png)

(this is the example from the [same topic](http://stackoverflow.com/questions/4700614/how-to-put-the-legend-out-of-the-plot) on stackoverflow, but I cannot repeat the result from there, too)

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![Daneel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daneel/32/175_2.png) [@Daneel](https://discourse.julialang.org/u/Daneel)\
**Post date:** [January 6, 2017, 7:57am UTC](https://discourse.julialang.org/t/location-of-the-legend-in-pyplot/1311/2 "2017-01-06T07:57:10Z")

</div>

It doesn’t automatically adjust the axis dimensions unfortunately.

```julia
x = 0:0.1:3*pi
y = sin(x)
z = cos(x)
ax = axes()
plot(x,y,label="sin(x)")
plot(x,z,label="cos(x)")
grid("on")
legend(bbox_to_anchor=[1.05,1],loc=2,borderaxespad=0)
ax[:set_position]([0.06,0.06,0.71,0.91])

```

 ![](https://global.discourse-cdn.com/julialang/original/3X/3/7/37edf0a9fec3411af09f6ce45e1bbe8d30fe2fbe.png)

---

<div class="post-metadata">

**Author:** ![Aleksandr\_Mikheev](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@Aleksandr\_Mikheev](https://discourse.julialang.org/u/Aleksandr_Mikheev)\
**Post date:** [January 6, 2017, 9:59am UTC](https://discourse.julialang.org/t/location-of-the-legend-in-pyplot/1311/3 "2017-01-06T09:59:23Z")

</div>

Thank you! Also, I have additional question if you don’t mind. Is there any convenient way to control the font size in PyPlot? The problems is that if you insert your image in .pdf, the letters are usually too small. Right now I adjust the font size manually every time, i.e. use

```julia
xlabel("t",fontsize = 20)
ylabel("x",fontsize = 20)
title("Something", fontsize = 20)

```

etc.  
The problem is that this is very inconvenient. Also, I cannot change the yticks size for some reason:

```julia
yticks(fontsize = 20)

```

returns

```julia
ERROR: function Int64 does not accept keyword arguments in kwfunc(::Any) at .\boot.jl:236

```

for some reason.

It works with xticks, though.

I have seen in [this post](http://stackoverflow.com/questions/12444716/how-do-i-set-the-figure-title-and-axes-labels-font-size-in-matplotlib) that in Python there is the way to do this simpler with

```julia
params = {'legend.fontsize': 'x-large',
         'axes.labelsize': 'x-large',
         'axes.titlesize':'x-large',
         'xtick.labelsize':'x-large',
         'ytick.labelsize':'x-large'}

```

Is it possible to do something like this with Julia language?

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![Daneel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daneel/32/175_2.png) [@Daneel](https://discourse.julialang.org/u/Daneel)\
**Post date:** [January 6, 2017, 10:18am UTC](https://discourse.julialang.org/t/location-of-the-legend-in-pyplot/1311/4 "2017-01-06T10:18:47Z")

</div>

What’s your goal exactly? Are you trying to change the axis number font size or the label font size?

```julia
x = 0:0.1:3*pi
y = sin(x)
z = cos(x)
ax = axes()
plot(x,y,label="sin(x)")
plot(x,z,label="cos(x)")
grid("on")
myfont = Dict("fontname"=>"Sans","weight"=>"semibold","fontsize"=>22)
xlabel("x",fontdict=myfont)
ylabel("y",fontdict=myfont)
title("Test",fontdict=myfont)

```

 ![](https://global.discourse-cdn.com/julialang/original/3X/9/7/97e58c25371576c4c77793fa460a7fe5c7fd3889.png)

Take a look at this [PyPlot Cookbook](https://gist.github.com/gizmaa/7214002) for more reference. Search for **font** to see other usages.

---

<div class="post-metadata">

**Author:** ![Aleksandr\_Mikheev](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@Aleksandr\_Mikheev](https://discourse.julialang.org/u/Aleksandr_Mikheev)\
**Post date:** [January 6, 2017, 10:39am UTC](https://discourse.julialang.org/t/location-of-the-legend-in-pyplot/1311/5 "2017-01-06T10:39:25Z")

</div>

I would like to change both. Tried to find this in [PyPlot Cookbook](https://gist.github.com/gizmaa/7214002), but it seems it is not mentioned there.

---

<div class="post-metadata">

**Author:** ![Daneel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daneel/32/175_2.png) [@Daneel](https://discourse.julialang.org/u/Daneel)\
**Post date:** [January 6, 2017, 10:56am UTC](https://discourse.julialang.org/t/location-of-the-legend-in-pyplot/1311/6 "2017-01-06T10:56:15Z")

</div>

Add `labelsize=16` to line 49 of the [majorminor example](https://github.com/gizmaa/Julia_Examples/blob/master/pyplot_majorminor.jl).

```julia
ax[:xaxis][:set_tick_params](which="major",length=10,width=2,labelsize=16)

```

For more tick properties see this [matplotlib documentation](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.tick_params).

It is rather irritating that it is so inconsistent.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 25, 2017, 12:21am UTC](https://discourse.julialang.org/t/location-of-the-legend-in-pyplot/1311/7 "2017-01-25T00:21:15Z")

</div>

You should be able to use one of the matplotlibrc global settings.
