Filled contour plots in Plots.jl without contour lines and smooth interpolation (GR backend)

… you can add linewidth=0 to your contourf command to disable the contour lines

6 Likes

Sorry, I meant on plotly backend. linewidth=0 did not work. I should make a separate thread for that though. Thanks for the input, it is working on GR nicely :grinning:.

Ah, I’m sorry. Earlier in the thread @sloede said this didn’t work. But it does!


Sorry to pester you, but is there a way to change behaviour beyond the color limits? I have hacked it by limiting the array values before I pass it to plot , but I can’t see if there is a native way to avoid the white-out regions my previous plot.

7 Likes

It looks amazing! But I have a question:

I note that the gray zone is not disturbed by the interpolation, which is correct.
From my result, there are quite a lot of fluctuations in small-value areas, see below:

ani_acoustics

I think this is possibly caused by the interpolation. While the issue does not occur when using the heatmap (with interpolation) in GLMakie, see the next reply (new users can only upload one media file).

As GLMakie is too much complicated for me, I wish to keep using Plots (GR). Is it possible to allow the interpolation in the heatmap function just like the heatmap in GLMakie? This was allowed in old version (in 2016 if I remember correctly), but it was later disabled by default (I don’t know why). Actually it is quite useful to visualizing the results of numerical modellings.

Makie_ani_acoustics

Hey @Songhan,

Two important plotting tips! First, this kind of data (with high and low values around a midrange) should be visualized with a diverging color scale, not a rainbow scale. Second, never include zero in your list of color levels. That is what gives you the flickers.

1 Like

Thank you @weymouth !

I have tried your suggestions:

  1. The diverging colormap :diverging_bwr_55_98_c37_n256 is used instead;

  2. I have checked the data. All of them are non-zero. The values in some areas are quite close to zero (but not strictly zero).

ani_acoustics

The output becomes much better than the previous one, but the flickers can still be observed. Is there any other issues which should be fixed from my code?

But anyhow, I am not sure if it comes from a bug in Plots, since the heatmap in GLMakie avoids the flickers even using my previous settings.

I tried to avoid the near-zero values from the following modifications on my data:

for ii = 1:5151
    for jj = 1:500
        if abs.(Ug[ii,jj]) < 1e-2
            Ug[ii,jj] = 0
        end
    end
end

This changes the small values to be exactly zero, and the plot result becomes correct (even using the colormap “jet” which is not diverging):

ani_acoustics

So, possibly the issue comes from these small values. It would be good if there is another approach to overcome this issue instead of modifying the source data.

The problem is that you are using color maps with a contour level at zero, and you have values just above and below zero in your field. Get rid of the 0 contour level.

Look at my color map compared to yours. Mine (RdBl_11) has the same grey color above and below zero, that means you won’t see the small changes above and below zero. The two you’ve picked both have different colors above and below zero.

In short - don’t change your data, change your color map.

4 Likes

Thank you @weymouth !

It comes out an error when using colormap = :RdBl_11 as you suggested, but I have tried the colormap (colormap = :cyclic_grey_15_85_c0_n256_s25), and the result becomes fine indeed.

Still, I believe that this issue should exist as well in the heatmap (for GR) when the colormap is not diverging (such as the previous one). But it is not the case. So, I guess maybe the issue comes from the discretized colormap in contour, as I note that the colorbar is divided into a number of blocks, i.e., is not smooth and continuous. Should this be the reason?

This has stopped working again in new versions of Plots. Is there a new way to do it?

The linewidth keyword argument works for me with gr() backend.
Try lw=0.

Yes, that seems to work. However, I would consider this a workaround and think of something better (in GR).

How odd. Neither of those options remove the lines from my contourf plots. Perhaps it depends on the system? I’m using Windows.

Just to be sure we are talking about the same thing.

On Win11, Julia 1.8.5, with latest versions of Plots.j and GR.jl, the folllowing MWE:

using Plots; gr()
z = Plots.GR.peaks(200)
Plots.contourf(z, lw=0)

produces:

2 Likes

Btw, using contourf(z, lw=0, levels=1000), produces a smooth plot but not a sufficiently smooth color scale:

In this case, it is better to use heatmap()

… and another colorscale. That’s awful :slight_smile:

As indicated, there is no problem with the heatmap() colorbar:

Apologies. It turns out I’m talking about a line between two essentially equal levels in the colormap. Setting levels to the same number of colors in the colormap fixed the problem.

1 Like

I apologise :slight_smile: I’ve just came to very similar 2d contour diagrams, trying to use Plots.jl to visualise flow — 2d tabulated data with hydrodynamic equations solutions.

Could you please tell how you do it without using pyplot? (Possibly in gr()?). Because my gr contour() works only with explicit function of coordinates, but what if it’s just a table, 2d array? Google helped only to find this very topic. Thank you!