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

[FR] NaN-separated series with changing colors #2959

Closed
ignacioq opened this issue Sep 7, 2020 · 3 comments
Closed

[FR] NaN-separated series with changing colors #2959

ignacioq opened this issue Sep 7, 2020 · 3 comments
Labels
enhancement improving existing functionality

Comments

@ignacioq
Copy link

ignacioq commented Sep 7, 2020

I would like to have a NaN separated series for paths, in which color changes for each part of the path.
For instance x, y, z are vectors, with some NaN separating the different lines of the tree; say x = [0.1, 0.2, 0.3, NaN, 0.1, 0.2], y = [0.5, 0.4, 0.5, NaN, 0.5, 0.6] and z = [0.2, 0.8, 0.6, NaN, 0.1, 0.9]. I would like each of the series to have the corresponding path colored according to z. This would work if there are no NaN separating the paths, but it does not work when NaN's are included. I raised the issue on slack (https://julialang.slack.com/archives/C8JKM4JQ6/p1599257009014800) and I got an answer to use the @series macro, but it becomes prohibitively slow.
It is fast If I do not put NaN's (though this is not what I want, the diagonal lines should not appear). Note that the color changes along the branches of the tree:
t1
or if I don't try to color the segments:
t0
Any help of making this possible would be very appreciated! Thanks.
My current recipe is (note the commented section which does what I want but takes very long using the @series macro):

@recipe function f(tree::iTgbm, zfun::Function)

  x = Float64[]
  y = Float64[]
  z = Float64[]
  rplottree(tree, treeheight(tree), 1:sntn(tree), zfun, x, y, z)
  pop!(x)
  pop!(y)
  pop!(z)

  # plot defaults
  line_z          --> z
  linecolor       --> :inferno
  legend          --> :none
  colorbar        --> true
  xguide          --> "time"
  fontfamily      --> font(2, "Helvetica")
  xlims           --> (0, treeheight(tree))
  ylims           --> (0, sntn(tree)+1)
  xflip           --> true
  xtickfont       --> font(8, "Helvetica")
  grid            --> :off
  xtick_direction --> :out
  yshowaxis       --> false

  # nan_inds = findall(x -> isnan(x),z)
  # for i in eachindex(nan_inds)
  #     start = 1 + (i == 1 ? 0 : nan_inds[i - 1])
  #     stop = nan_inds[i] - 1
  #     @series begin
  #         line_z --> z[start:stop]
  #         x[start:stop], y[start:stop]
  #     end
  # end
  return x, y
end
@ignacioq ignacioq changed the title [FR] NaN-seperated series with changing colors [FR] NaN-separated series with changing colors Sep 7, 2020
@daschw daschw added the enhancement improving existing functionality label Sep 8, 2020
@yha
Copy link
Member

yha commented Mar 2, 2021

@ignacioq Can you check if the problem is solved in Plots master?
The code you pasted here doesn't seem to be complete so I can't test it, but I think this should be fixed by #3320.

@ignacioq
Copy link
Author

ignacioq commented Mar 3, 2021

Thanks, I tried this but it still does not work. You could check just by plotting the following vectors:

  x = [0.1, 0.2, 0.3, 0.4, NaN, 0.2, 0.1, NaN, 0.5, 0.4, 0.3, 0.1]
  y = [0.5, 0.5, 0.5, 0.5, NaN, 3.0, 3.0, NaN, 5.0, 5.0, 5.0, 5.0] 
  z = [0.2, 0.8, 0.6, 1.0, NaN, 0.1, 0.9, NaN, 0.1, 0.9, 0.2, 0.3]

Where x and y are the coordinates for the lines and z should be the color. However, the lines are plotted only with the first color of the segment. If you remove the NaN, then the colors do change within each segment but then they are connected to each other.

Using Julia 1.5 and Plots v1.10.6

@yha
Copy link
Member

yha commented Mar 3, 2021

Sorry, I should have emphasized that this fix is not yet in the released version.
Your example,

x = [0.1, 0.2, 0.3, 0.4, NaN, 0.2, 0.1, NaN, 0.5, 0.4, 0.3, 0.1]
y = [0.5, 0.5, 0.5, 0.5, NaN, 3.0, 3.0, NaN, 5.0, 5.0, 5.0, 5.0] 
z = [0.2, 0.8, 0.6, 1.0, NaN, 0.1, 0.9, NaN, 0.1, 0.9, 0.2, 0.3]

plot(x, y, line_z=z, marker=true, marker_z=z, lw=5)

on 1.0.6:
tmp-1 10 6
on master:
tmp-master

so I believe this can be closed.

@yha yha closed this as completed Mar 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement improving existing functionality
Projects
None yet
Development

No branches or pull requests

3 participants