Skip to content

Conversation

@pearlzli
Copy link
Contributor

@pearlzli pearlzli commented Oct 28, 2020

I took a crack at adding a fillstyle argument which allows a few different hatching patterns, as suggested on Discourse and Gitter. I (kind of arbitrarily) followed the PyPlot convention in letting the possible values of fillstyle be :/, :\, :|, :-, :+, :x, and nothing (the default, which keeps the solid fill).

In PyPlot:

using Plots, PyPlot
pyplot()
y = rand(10)
plot(y .+ 1, fillrange = y, fillstyle = :/)



In GR, I unfortunately can't figure out how to change the color of the hatching from black:

using Plots
gr()
y = rand(10)
plot(y .+ 1, fillrange = y, fillstyle = :/)

Would love to get your feedback and suggestions!

Fix #1337.

@mkborregaard
Copy link
Member

I love this!

@daschw
Copy link
Member

daschw commented Oct 28, 2020

That's amazing!

@daschw
Copy link
Member

daschw commented Oct 28, 2020

I tried with GR.setlinecolorind, GR.setfillcolorind, GR.setmarkercolorind and GR.settextcolorind, but nothing seems to change the hatch color. Is this not possible @jheinen?

@jheinen
Copy link
Member

jheinen commented Oct 28, 2020

I tried with GR.setlinecolorind, GR.setfillcolorind, GR.setmarkercolorind and GR.settextcolorind, but nothing seems to change the hatch color. Is this not possible @jheinen?

Seems to be a bug in GR. Will check ...

@pearlzli
Copy link
Contributor Author

pearlzli commented Oct 28, 2020

I pushed some commits which fix setting the fill transparency level in PyPlot:

using Plots
pyplot()
y = rand(10)
plot(y .+ 1, fillrange = y, fillstyle = :/, fillalpha = 1)
plot(y .+ 1, fillrange = y, fillstyle = :/, fillalpha = 0.25)

This was already working in GR:

@pearlzli
Copy link
Contributor Author

pearlzli commented Oct 28, 2020

(Starting a new post for a separate thought.)

I also noticed (and haven't yet figured out how to address) a slightly bad thing in PyPlot. When using fillrange, you can set the line color/alpha independently from the fill color/alpha, but it doesn't show up in the legend, and it also doesn't work for plotting Shapes.

using Plots, PyPlot
pyplot()
y = rand(10)
Plots.plot(y .+ 1, fillrange = y, fillstyle = :/, linewidth = 2, linecolor = 2)
verts = [(1,0), (0,1), (-1,0), (0,-1), (1,0)]
Plots.plot(Shape(verts), fillstyle = :/, linewidth = 2, linecolor = 2)

It seems like this isn't a problem in GR, though it'll be easier to tell once we can change the hatch fill color:

@jheinen
Copy link
Member

jheinen commented Nov 6, 2020

The GR problems should be fixed in GR.jl v0.53.0

@pearlzli
Copy link
Contributor Author

Thanks, @jheinen! I've been a little swamped lately, but will try to make more progress on this next week.

@BeastyBlacksmith BeastyBlacksmith marked this pull request as draft July 5, 2021 15:48
@t-bltg t-bltg added the WIP Work in progress, do not merge! label Jul 6, 2021
@t-bltg
Copy link
Member

t-bltg commented Jul 31, 2021

@pearlzli, would you be able to find time to finish this ? (or is it maybe ready for review ?).

That would allow me to add support to filled pattern for the Gaston (gnuplot) backend ...

@pearlzli
Copy link
Contributor Author

pearlzli commented Aug 3, 2021

Hi @t-bltg and everyone - sorry for totally dropping the ball on this! I'll try to take a crack at this again over the weekend.

@pearlzli
Copy link
Contributor Author

Sorry again for my delay on this! Using an updated version of GR.jl, changing the hatch color works perfectly:

using Plots
gr()
y = rand(10)
hatchstyles = [:/ :\ :| :- :+ :x nothing]
hatchnames = [":/" ":\\" ":|" ":-" ":+" ":x" "nothing"]
plot(repeat(y .+ 1, outer = (1,7)), fillrange = repeat(y, outer = (1,7)),
     fillstyle = hatchstyles, label = "fillstyle = " .* hatchnames,
     layout = (4,2))

I think I've also addressed the problem with setting the line color independently of the fill color in PyPlot that I mentioned in #3107 (comment):

using Plots
pyplot()
y = rand(10)
verts = [(1,0), (0,1), (-1,0), (0,-1), (1,0)]
p1 = plot(repeat(y .+ 1, outer = (1,2)), fillrange = repeat(y, outer = (1,2)),
          fillstyle = [:/ nothing], linecolor = 2, label = "fillrange", layout = (1,2))
p2 = plot([Shape(verts) Shape(verts)], fillstyle = [:/ nothing],
          linecolor = 2, label = "shape", layout = (1,2))
plot(p1, p2, layout = (2,1))

@codecov
Copy link

codecov bot commented Aug 25, 2021

Codecov Report

Merging #3107 (fdf33d1) into master (0742d47) will increase coverage by 0.44%.
The diff coverage is 69.23%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3107      +/-   ##
==========================================
+ Coverage   62.97%   63.41%   +0.44%     
==========================================
  Files          28       28              
  Lines        7070     7372     +302     
==========================================
+ Hits         4452     4675     +223     
- Misses       2618     2697      +79     
Impacted Files Coverage Δ
src/args.jl 74.02% <0.00%> (+0.91%) ⬆️
src/backends/gr.jl 89.39% <80.00%> (+0.18%) ⬆️
src/utils.jl 54.89% <100.00%> (+0.10%) ⬆️
src/plot.jl 66.20% <0.00%> (-1.22%) ⬇️
src/animation.jl 37.36% <0.00%> (-0.42%) ⬇️
src/themes.jl 13.95% <0.00%> (-0.34%) ⬇️
src/axes.jl 85.06% <0.00%> (-0.11%) ⬇️
src/Plots.jl 31.42% <0.00%> (ø)
src/plotattr.jl 0.00% <0.00%> (ø)
src/backends/gaston.jl 0.00% <0.00%> (ø)
... and 15 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0742d47...fdf33d1. Read the comment docs.

@pearlzli pearlzli marked this pull request as ready for review August 25, 2021 23:21
@pearlzli pearlzli changed the title [WIP] Add hatched fill for GR and PyPlot Add hatched fill for GR and PyPlot Aug 25, 2021
Copy link
Member

@t-bltg t-bltg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for finalizing 👍

@t-bltg t-bltg merged commit ded8084 into JuliaPlots:master Aug 26, 2021
@BeastyBlacksmith BeastyBlacksmith removed the WIP Work in progress, do not merge! label Aug 26, 2021
@jeanpauphilet
Copy link

Hi,
Thanks for adding this cool feature. I have noticed an issue when saving the figure as a pdf file. I am using Plots and GR. I am using the same code as @pearlzli.

using Plots
gr()
y = rand(10)
hatchstyles = [:/ :\ :| :- :+ :x nothing]
hatchnames = [":/" ":\\" ":|" ":-" ":+" ":x" "nothing"]
plot(repeat(y .+ 1, outer = (1,7)), fillrange = repeat(y, outer = (1,7)),
      fillstyle = hatchstyles, label = "fillstyle = " .* hatchnames,
      layout = (4,2))

The output looks fine on a Jupyter notebook. However when I save it as a PDF file, the hatch color is back to black. To save it, I do:

Plots.savefig("test.pdf")

Using GRUtils.savefig instead produced a corrupted pdf file.

Thanks for your help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fill pattern

7 participants