Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

Azaya89
Copy link
Contributor

@Azaya89 Azaya89 commented Sep 2, 2025

fixes #6644

This PR:

  • Resolves the y-axis label mismatch by reversing the labelling, matching the origin='upper' policy used in other image-like plots in mpl, e.g, raster
    style['origin'] = 'upper'
    This ensures that the index 0 label of a plot with categorical y-axes now appears at the top rather than at the bottom.
  • Add test.
import pandas as pd
import holoviews as hv
import numpy as np

hv.extension('matplotlib')

data = pd.DataFrame({
    'depth_class': ['Shallow', 'Shallow', 'Shallow',
                    'Intermediate', 'Intermediate',
                    'Deep', 'Deep', 'Deep'],
    'mag_class': ['Light', 'Moderate', 'Strong',
                  'Light', 'Moderate',
                  'Light', 'Moderate', 'Major'],
    'count': [301, 37, 2, 212, 8, 34, 1, 1]
})

data['depth_class'] = pd.Categorical(data['depth_class'], categories=['Shallow', 'Intermediate', 'Deep'], ordered=True)
data['mag_class'] = pd.Categorical(data['mag_class'], categories=['Light', 'Moderate', 'Strong', 'Major'], ordered=True)

heatmap = hv.HeatMap(data, ['mag_class', 'depth_class'])
heatmap = heatmap.aggregate(function=np.mean)
heatmap
Screenshot 2025-09-03 at 7 13 58 PM

@Azaya89 Azaya89 self-assigned this Sep 2, 2025
@Azaya89 Azaya89 requested a review from hoxbro September 2, 2025 15:56

This comment was marked as outdated.

@Azaya89 Azaya89 reopened this Sep 3, 2025
@hoxbro
Copy link
Member

hoxbro commented Sep 4, 2025

Something still seems off when looking at invert_yaxis=True and with an inverted DataFrame (see code)

Code

import holoviews as hv
import numpy as np
import pandas as pd

hv.extension("bokeh")


def hm(inverse=False):
    data = pd.DataFrame(
        {
            "depth_class": [
                "Shallow",
                "Shallow",
                "Shallow",
                "Intermediate",
                "Intermediate",
                "Deep",
                "Deep",
                "Deep",
            ],
            "mag_class": [
                "Light",
                "Moderate",
                "Strong",
                "Light",
                "Moderate",
                "Light",
                "Moderate",
                "Major",
            ],
            "count": [301, 37, 2, 212, 8, 34, 1, 1],
        }
    )
    if inverse:
        data = data[::-1]

    data["depth_class"] = pd.Categorical(
        data["depth_class"], categories=["Shallow", "Intermediate", "Deep"], ordered=True
    )
    data["mag_class"] = pd.Categorical(
        data["mag_class"], categories=["Light", "Moderate", "Strong", "Major"], ordered=True
    )

    heatmap = hv.HeatMap(data, ["mag_class", "depth_class"])
    heatmap = heatmap.aggregate(function=np.mean)
    return heatmap


(hm().opts(tools=["hover"]) + hm(True).opts(tools=["hover"])).opts(shared_axes=False)

hv.extension("matplotlib")
(hm() + hm(True)).opts(shared_axes=False)

image

@Azaya89
Copy link
Contributor Author

Azaya89 commented Sep 4, 2025

It appears when you invert the data, the y-axis labels are not inverted, and therefore becomes wrong again ☹️

Will continue to investigate...

@hoxbro hoxbro marked this pull request as draft September 4, 2025 14:43
@Azaya89
Copy link
Contributor Author

Azaya89 commented Sep 12, 2025

Still working on resolving for an inverted dataframe (plot B) but invert_axes=True options seems to work well now. Same with invert_xaxis and invert_yaxis options.

Screenshot 2025-09-12 at 12 45 47 PM

@hoxbro hoxbro changed the title fix: Fix matplotlib heatmap categorical y-axis flip fix(matplotlib): Heatmap y-axis wrong ordering Sep 12, 2025
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

Successfully merging this pull request may close these issues.

y-axis ordering flipped in hv.HeatMap with Matplotlib backend
2 participants