Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanest way of plotting columns of a matrix (or multiple functions) #529

Closed
juliohm opened this issue Jan 16, 2015 · 6 comments
Closed

Comments

@juliohm
Copy link

juliohm commented Jan 16, 2015

I was not able to find an answer to this simple question in the docs. Let's say I have a matrix such that the first column has the abscissas in the x axis and the other columns are functions evaluated at these abscissas. How do I plot all columns at once in different color and with a legend?

Alternatively, suppose I have three functions:

x(n) = 2n
y1(n) = n+2
y2(n) = sin(n)

How do I plot x vs. y1 and x vs. y2 in different color with a legend varying n=1:10?

@aviks
Copy link
Collaborator

aviks commented Jan 16, 2015

duplicate of #526.

Gadfly works best with stacked DataFrames, rather than two dimensional matrices. There is a workaround with layers as described in the above issue. But that is not perfect. The best way to do what you need, with the functions defined above, is to convert your data into a DataFrame.

df=DataFrame(n=repeat([1:10], outer=[3]), 
            y=vcat([x(n) for n=1:10],[y1(n) for n=1:10], [y2(n) for n=1:10]), 
            fun=repeat(["x", "y1", "y2"], inner=[10]))
plot(df, color="fun", x="n", y="y", Geom.point, Geom.line)

screen shot 2015-01-16 at 13 41 54

@aviks aviks closed this as completed Jan 16, 2015
@juliohm
Copy link
Author

juliohm commented Jan 16, 2015

I understood we don't have a clean call to achieve what I've asked with Gadfly. @aviks, why you closed the issue then?

@juliohm
Copy link
Author

juliohm commented Jan 16, 2015

The cleanest (methodical) solution I could come up with so far is:

df1 = DataFrame(x=1:10, y=f(1:10), label="f(x)")
df2 = DataFrame(x=1:10, y=g(1:10), label="g(x)")
# ...
df = vcat(df1, df2, ...)

plot(df, x=:x, y=:y, color=:label, Geom.line)

It doesn't require all those trick calls to repeat() and is extensible to multiple functions.

However, it would be nice to plot functions directly without this intermediate creation of dataframes.

@zhujinxuan
Copy link

Sorry for my previous comment from my iPhone. The format was distoried and that message was unreadable.

I recommend melt from package DataFrames. It serves a near function of vcat but requires much less codes. For example:

using Gadfly
using DataFrames
T = [1:10.0]
df = DataFrame(x = 2*T, y1= T+2, y2= sin(T) )
plot(melt(df, :x), x = :x, y = :value, color = :variable, Geom.line, Geom.point )

@aviks
Copy link
Collaborator

aviks commented Jan 17, 2015

Hi @juliohm Yes, there may not be clean way to do that, but I closed this issue because the same thing is being discussed in issue number #526 , which I'd linked to in my comment. Let me know if you disagree, and I'll re-open this.

Thanks @zhujinxuan .

@juliohm
Copy link
Author

juliohm commented Jan 17, 2015

Thanks @aviks, it's okay to close this as duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants