JuliaPlots / InspectDR.jl

Fast, interactive Julia/GTK+ plots (+Smith charts +Gtk widget +Cairo-only images)
MIT License
68 stars 9 forks source link

Additional examples #6

Open skanskan opened 7 years ago

skanskan commented 7 years ago

Hello.

It would be great to have more examples, maybe simple ones such as scatter plots, histograms, etc. and see how they look in a pdf file. Trying to reproduce some of the examples provided in https://learnr.files.wordpress.com/2009/08/latbook.pdf or in Gadly examples in order to compare their syntax, looking and speed.

ma-laforge commented 7 years ago

I could probably add a few more examples, especially simple ones.

latbook.pdf

I probably don't have time to create something as elaborate as what is in latbook.pdf in the forseeable future. Maybe you have specific examples you would like to see?

Gadfly examples

Not a bad idea. I might try to look at this.

scatter plots

This should not be difficult. In InspectDR, scatter plot are just regular 2D plots with no line, and a "glyph" (or marker).

I will give you a small demo as soon as I get a chance.

Histogram

InspectDR does not natively support histograms - but it does if used as a backend to Plots.jl. InspectDR is mostly designed to address large datasets (GB range) - because most plotting packages are excruciatingly slow in these situations.

That said, InspectDR has facilities allowing Plots.jl to draw histograms through the "shape" mechanism (see below).

Plots.jl

The best way to compare InspectDR with other plotting packages is using the Plots.jl: https://github.com/JuliaPlots/Plots.jl

The Plots.jl module has better documentation than InspectDR. Also, many new users would probably prefer the API of Plots.jl. The downside of using InspectDR through Plots.jl is that the compile-time & time-to-first plot are observably longer at the moment.

I even included a few sample plots generated using Plots.jl: https://github.com/ma-laforge/FileRepo/blob/master/InspectDR/sampleplots_Plots/README.md

Note: Test 25 is a scatter plot (generated using the Plots.jl API, of course).

ma-laforge commented 7 years ago

Ok, here is an example for a simple scatter plot:

#Simple demo 1: Simple scatter plot
#-------------------------------------------------------------------------------
using InspectDR
using Colors

red = RGB24(1, 0, 0)
green = RGB24(0, 1, 0)
blue = RGB24(0, 0, 1)

#==Input
===============================================================================#

x = collect(0:10) #Must vectorize using collect - ranges not yet supported
y = rand(length(x))

#==Generate plot object
===============================================================================#
plot = InspectDR.Plot2D(:lin, :lin,
    title = "Sample Scatter Plot",
    xlabel = "X-Values",
    ylabels = ["Y-Values"]
)
plot.layout.legend.enabled = true
plot.layout.legend.width = 150 #Default: 100

#Set grid:
graph = plot.strips[1]
graph.grid = InspectDR.GridRect(vmajor=true, vminor=true, hmajor=true)

wfrm = add(plot, x, y, id="Random Sample")
    wfrm.line = line(width=3, style=:none) #No line on a scatter plot; width used for glyph
    wfrm.glyph = glyph(shape=:o, size=10, color=red, fillcolor=blue)

#==Render plot
===============================================================================#
#Show Gtk GUI
gplot = display(InspectDR.GtkDisplay(), plot)

#Don't need to show GUI if simple plot image is desired:
InspectDR.write_png("simpledemo1.png", plot)
#InspectDR.write_svg("simpledemo1.svg", plot)
#InspectDR.write_eps("simpledemo1.eps", plot)
#InspectDR.write_pdf("simpledemo1.pdf", plot)
ma-laforge commented 7 years ago

@skanskan: Is the above example simple enough? I ask because I could add it to the sample directory if you find it useful.

Comment

InspectDR was not really designed to have a simple one-liner interface. That was more the goal of Plots.jl. Alot of thought went into that module. I would rather just make use of it instead of re-inventing the wheel.

When I don't want to use Plots.jl

What I tend to do, is wrap some pre-defined "template" into a function similar to what I did for transientplot() - located in the following file: https://github.com/ma-laforge/InspectDR.jl/blob/master/src/templates.jl

In other words, you would define your own scatterplot function - which would result in a more succinct call, like the following:

scatterplot(x, y, "Plot Title", "X-Title", "Y-Title")

In your function, you could decide what to use as the default glyph/symbol size/color/etc, which grids are displayed, ...

skanskan commented 7 years ago

Then, What package or procedure would you suggest If I just wanted to use a "simple one-liner" to quickly plot a scatterplot with 100000 points? (without previously creating templates)

ma-laforge commented 7 years ago

Plots.jl solution

using Plots

inspectdr() #Select InspectDR backend

x = 0:100_000
y = rand(length(x))

#The one-liner command itself:
display(plot(x, y, seriestype=:scatter))
ma-laforge commented 7 years ago

Alternative: CData.jl / EasyPlot.jl

NOTE: Plots.jl is still in the process of migrating to Julia v0.6.

An alternative is to use my own simplified plotting interface (not quite as elaborate as Plots.jl): https://github.com/ma-laforge/EasyPlot.jl

It is part of the CData.jl suite (Installation instructions on readme page): https://github.com/ma-laforge/CData.jl

The CData/EasyPlot solution looks as follows

#Simple demo 1: Simple scatter plot
#-------------------------------------------------------------------------------
using EasyPlotInspect #Simpler interface
using EasyPlot #Exports symbols
using MDDatasets #Required data structures

#==Input
===============================================================================#
x = collect(0:100_000) #Must vectorize using collect - ranges not yet supported
y = rand(length(x))
rnddata = DataF1(x, y) #EasyPlot requires "DataF1" object to keep x & y together

#==Generate plot
===============================================================================#
plot = EasyPlot.new(title = "Sample Plot")
    subplot = add(plot, title = "Scatter Plot") #Could also add x/y scale & labels here
    wfrm = add(subplot, rnddata, id="Random Sample")
    set(wfrm, line(style=:none), glyph(shape=:o, color=:blue, size=2))
display(plot)

It is not quite as succinct as what you get with Plots.jl (previous post)

skanskan commented 7 years ago

What advantage has Easyplot over Plots.jl+inspectdr() ?

ma-laforge commented 7 years ago

Plots.jl + inspectdr():

EasyPlot/EasyPlotInspect:

I should also probably mention that the CData/EasyPlot solution relies on a collection of modules that are not registered in METADATA (somewhat Julia-curated).

Since I have not figured out the proper way to tag/version these modules for different Julia distributions, the solution as a whole is much more likely to be broken when I start updating packages/migrating to new versions of Julia.

Though this might not be a big deal in certain applications, I can easily see people finding it unacceptable for practical use. However, I might try to improve things if there were sufficient demand.

skanskan commented 7 years ago

I'm having some related problems with the plots. I'm opening a new question here https://discourse.julialang.org/t/strange-things-with-plots-inspectdr/4937