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

fix colorbar corner cases (singleton) #3511

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

t-bltg
Copy link
Collaborator

@t-bltg t-bltg commented Dec 25, 2023

Description

Fix #3470.
Fix #3174.

With this PR:

using CairoMakie

x, y = [0, 1], [0, 2]
z = zeros(2, 2)

fig, ax, pl = heatmap(x, y, z)
Colorbar(fig[1, 2], pl)
save("zero-heatmap.png", fig)
display(fig)

fig, ax, pl = contourf(x, y, z)
Colorbar(fig[1, 2], pl)
save("zero-contourf.png", fig)
display(fig)

x, y = randn(10), randn(10)
z = zero(x)
fig, ax, pl = tricontourf(x, y, z)
Colorbar(fig[1, 2], pl)
save("zero-tricontourf.png", fig)
display(fig)

heatmap
zero-heatmap

contourf
zero-contourf

tricontourf
zero-tricontourf

Type of change

Delete options that do not apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

  • Added an entry in NEWS.md (for new features and breaking changes)
  • Added or changed relevant sections in the documentation
  • Added unit tests for new algorithms, conversion methods, etc.
  • Added reference image tests for new plotting functions, recipes, visual options, etc.

@t-bltg t-bltg added the bug label Dec 25, 2023
@t-bltg t-bltg changed the title fix colorbar corner cases fix colorbar corner cases (all zeros) Dec 25, 2023
@jkrumbiegel
Copy link
Member

Ah that's good to fix all of those. What do you think, my feeling is that the super small limits are a bit confusing. My strategy so far, whenever I've thought about it specifically, was to either do the 0 to x interval when x is not zero, or 0 to 1 when x is 0. This does mean that the color you get is the top one when your value is positive, or the bottom one when it's negative or zero. But then at least you don't think long about what kind of range you've gotten I think.

@t-bltg
Copy link
Collaborator Author

t-bltg commented Dec 26, 2023

Ah that's good to fix all of those. What do you think, my feeling is that the super small limits are a bit confusing.

Yeah, the more I think about this, I'm neither satisfied with small values or the arbitrary (-0.5, 0.5) implemented in #1739.

either do the 0 to x interval when x is not zero, or 0 to 1 when x is 0

Good idea, here is the updated PR, IIUC:

using CairoMakie

for v  (0, 1)
  x, y = [0, 1], [0, 2]
  z = fill(float(v), length(x), length(y))
  fig, ax, pl = heatmap(x, y, z)
  Colorbar(fig[1, 2], pl)
  save("$v-heatmap.png", fig)
  display(fig)

  fig, ax, pl = contourf(x, y, z)
  Colorbar(fig[1, 2], pl)
  save("$v-contourf.png", fig)
  display(fig)
end

for v  (0, 1)
  x, y = randn(10), randn(10)
  z = fill(float(v), length(x))
  fig, ax, pl = tricontourf(x, y, z)
  Colorbar(fig[1, 2], pl)
  save("$v-tricontourf.png", fig)
  display(fig)
end

0-heatmap
0-heatmap

0-contourf
0-contourf

0-tricontourf
0-tricontourf

1-heatmap
1-heatmap

1-contourf
1-contourf

1-tricontourf
1-tricontourf

@t-bltg t-bltg changed the title fix colorbar corner cases (all zeros) fix colorbar corner cases (singleton) Dec 28, 2023
@jkrumbiegel
Copy link
Member

Hm I don't really like the contourf and tricontourf behavior. We have these colorbars with only one color (because they are categorical with one category but you don't easily understand that) but a range of values mapped to that color. So we don't know which uniform value we're plotting. While with heatmap, we can still recover the value, it's just that the range has to be arbitrarily chosen.

So far, I've opted for behavior in Makie where you get an error when a singular value is supposed to be treated as a range. I find that more explicit (even though some error messages might be better) and it forces users to be clear what kind of bins they want. I do get that it's a bit inconvenient now and then. What do matplotlib and ggplot do here?

@t-bltg
Copy link
Collaborator Author

t-bltg commented Jan 7, 2024

This is what pyplot does:

import pylab as plt
import numpy as np

x, y = [0, 1], [0, 2]
for v in (0, 1):
    z = np.full((len(x), len(y)), float(v))

    fig, ax = plt.subplots()
    ct = ax.contourf(x, y, z)
    fig.colorbar(ct)
    fig.savefig(f'{v}-contourf.png')

    fig, ax = plt.subplots()
    ct = ax.pcolormesh(x, y, z)
    fig.colorbar(ct)
    fig.savefig(f'{v}-heatmap.png')

    # fig, ax = plt.subplots()
    # ct = ax.tricontourf(x, y, z)
    # fig.colorbar(ct)
    # fig.savefig(f'{v}-tricontourf.png')

0-contourf
0-heatmap

1-contourf
1-heatmap

i.e. the singleton is extended from both side by an epsilon: (val - eps, val] ∪ (val, val + eps] to form the colormap.

Not sure what the right choice here is ...

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.

Cannot create Colorbar from contourf of zeros Colorbar breaks with min limit == max limit
3 participants