How to generate subplots with 2D and 3D plots?

1,095 views
Skip to first unread message

Markus Roth

unread,
Nov 23, 2014, 3:11:10 PM11/23/14
to julia...@googlegroups.com
Hey guys,

I am a Julia beginner and I try to learn the language by writing simple sample code. Currently I want to generate a simple figure with subplots where the first [1,1] is a contour plot and the second one [1,2] should be a 3D Surface plot. I managed to generate a subplot with 2 2D plots but not a mixed one. I have some experience with Matlab but it seems to me that plotting works a bit different compared to Matlab. I use the PyPlot/Matplotlib package. Can someone help me out? Maybe someone can help me to better understand plotting in Julia (with PyPlot/Matplotlib).

Best Markus

p.s. I attached some sample code generating the figures.
sample.jl

Pileas

unread,
Nov 23, 2014, 3:21:09 PM11/23/14
to julia...@googlegroups.com
I think you should have PyPlot in front of the commands and at the very end PyPlot.show().
I was able to create a simple graph using the following code:

# -------------------------------------------------------------------------------------------------------------
import PyPlot

x = linspace(0, 3, 200)        
y = zeros(length(x))          
for i = 1:length(x)            
    j = x[i]                   
    y[i] = j*cos(j^2)
end

PyPlot.figure(1)
PyPlot.plot(x, y)
PyPlot.xlabel("value of x")
PyPlot.ylabel("value of function")
PyPlot.savefig("golden.png")

PyPlot.show()   # Always at the end
# ---------------------------------------------------------

To do that I had to look at Python examples and adjust then for Julia purposes. mathplot lib has many examples for Python. I think you should start there.

Daniel Høegh

unread,
Nov 23, 2014, 3:27:22 PM11/23/14
to julia...@googlegroups.com
This works:
ax = fig[:add_subplot](2,1,1, projection = "3d")
xgrid = repmat(x',n,1)
ygrid = repmat(y,1,n)
ax[:plot_surface](xgrid, ygrid, z, rstride=2,edgecolors="k", cstride=2, cmap=ColorMap("gray"), alpha=0.8, linewidth=0.25)
xlabel("X")
ylabel("Y")
subplot(212)
ax = fig[:add_subplot](2,1,2)
cp = ax[:contour](xgrid, ygrid, z, colors="black", linewidth=2.0)
ax[:clabel](cp, inline=1, fontsize=10)
xlabel("X")
ylabel("Y")

My greatest source of good examples is always: http://matplotlib.org/gallery.html, and there is a good gist that shows how to use PyPlot in julia: https://gist.github.com/gizmaa/7214002

Markus Roth

unread,
Nov 24, 2014, 2:39:37 AM11/24/14
to julia...@googlegroups.com
Thank you very much. I think I will try your suggestion later this day.

Markus Roth

unread,
Nov 24, 2014, 4:52:51 PM11/24/14
to julia...@googlegroups.com
Thank you Daniel,

I just tried your suggestion. Seems to work fine. However the font sizes (of axes ticks) do not automatically turn smaller but have to be adjusted manually. I think I will manage to fix this ;).

Best,
Markus


Am Sonntag, 23. November 2014 21:27:22 UTC+1 schrieb Daniel Høegh:
Reply all
Reply to author
Forward
0 new messages