2
votes

I am completely new to Julia. I am using the PyPlot package in Julia and am just trying to set my x and y axis origin to 0 and 0 respectively. At the moment it just chooses where the origin will be based on the values of the points I'm plotting.

plot(x1,y1,".")
xlabel("X1")
ylabel("Y1")
title("First Line")
grid("on")

I have tried the following but it doesn't work. change matplotlib axis settings

2
It would be helpful if you gave a matplotlib command together with the Julia equivalent that you actually tried, following the syntax description in the PyCall.jl documentation.David P. Sanders
I am a 100% beginner here and have no idea what you are talking about. What I did is given above. Help!lara
I've started using PyPlot.axis([xmin,xmax,ymin,ymax]) for this:PyPlot.plot(spacerdata[:Width],spacerdata[:Curvature],".") PyPlot.xlabel("Width") PyPlot.ylabel("Curvature") PyPlot.title("Plot") PyPlot.grid("on") PyPlot.axis([0,2,0,4])lara

2 Answers

3
votes

Using PyPlot in Julia is different than matplotlib in Python. You are looking for the Julia equivalent of setting limits on axis. I've found this useful github repo that might be of use to you.

Here is how you can add custom limits to the y axis:

using PyPlot

x1 = rand(50, 1) .* 30 .+ 50
y1 = rand(50, 1) .* 30 .+ 100 

plot(x1, y1)

# get the current axis argument of the plot
ax = gca()

# add new limits from 0 - 100
ax[:set_ylim]([0,100])
2
votes

The syntax for using PyPlot in Julia is a bit different from Python. This is a result of the fact that (currently) you cannot use obj.f() to access the method (function) f() inside an object obj in Julia, which PyPlot uses heavily.

To get around this, obj.f() in Python is replaced by obj[:f]() in Julia; note the :.

So ax.spines in Python becomes

ax[:spines]

in Julia.

As noted by the other poster, you must first do ax = gca() to store the current axis object in the variable ax.

If you do this at the Julia REPL or in a Jupyter notebook, you will see

julia> ax = gca()
PyObject <matplotlib.axes._subplots.AxesSubplot object at 0x31f854550>

julia> ax[:spines]
Dict{Any,Any} with 4 entries:
  "left"   => PyObject <matplotlib.spines.Spine object at 0x31f854c10>
  "bottom" => PyObject <matplotlib.spines.Spine object at 0x31f854f50>
  "right"  => PyObject <matplotlib.spines.Spine object at 0x31f854dd0>
  "top"    => PyObject <matplotlib.spines.Spine object at 0x31f86e110>

showing that ax[:spines] is a dictionary. (I did not previously know about this. This is the advantage of having an interactive session -- you can just ask Julia what you need to know. This is called "introspection".)

Following the Python answer linked to in the original question, you then need to replace ax.spines['left'].set_position('zero') in Python by the following in Julia:

ax[:spines]["left"][:set_position]("zero")

Again, the set_position method inside the object ax[:spines]["left"] is called. Note also that strings in Julia must have ", not '.