Skip to content

ValueError: operands could not be broadcast together with shapes (256,4) (256,3)  #600

Open
@JasonGross

Description

@JasonGross

colab

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import tikzplotlib
from matplotlib.colors import LinearSegmentedColormap

# Define the custom colormap
colors = [(0, '#ff0000'),
          (0.25, '#ff8247'),
          (0.5, '#ffffff'),
          (0.75, '#ffc100'),
          (1, '#ff9c05')]
cmap_name = 'my_custom_cmap'
cmap = LinearSegmentedColormap.from_list(cmap_name, colors, N=256)

# Generate some sample data for the heatmap
data = np.random.rand(10, 10)

fig, ax = plt.subplots()
sns.heatmap(
    data,
    ax=ax,
    center=0.0,
    cmap=cmap,
    cbar_kws={"label": "Scale"},
)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_title("title")
plt.show()
tikzplotlib.save("heatmap_custom_colormap.tex", fig)

gives an error

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-9-e79bfff78935> in <cell line: 31>()
     29 ax.set_title("title")
     30 plt.show()
---> 31 tikzplotlib.save("heatmap_custom_colormap.tex", fig)
     32 

6 frames

/usr/local/lib/python3.10/dist-packages/tikzplotlib/_save.py in save(filepath, encoding, *args, **kwargs)
    260     :returns: None
    261     """
--> 262     code = get_tikz_code(*args, filepath=filepath, **kwargs)
    263     with open(filepath, "w", encoding=encoding) as f:
    264         f.write(code)

/usr/local/lib/python3.10/dist-packages/tikzplotlib/_save.py in get_tikz_code(figure, filepath, axis_width, axis_height, textsize, tex_relative_path_to_data, externalize_tables, override_externals, externals_search_path, strict, wrap, add_axis_environment, extra_axis_parameters, extra_groupstyle_parameters, extra_tikzpicture_parameters, extra_lines_start, dpi, show_info, include_disclaimer, standalone, float_format, table_row_sep, flavor)
    211 
    212     # gather the file content
--> 213     data, content = _recurse(data, figure)
    214 
    215     # Check if there is still an open groupplot environment. This occurs if not

/usr/local/lib/python3.10/dist-packages/tikzplotlib/_save.py in _recurse(data, obj)
    337 
    338         if isinstance(child, mpl.axes.Axes):
--> 339             ax = _axes.Axes(data, child)
    340 
    341             if ax.is_colorbar:

/usr/local/lib/python3.10/dist-packages/tikzplotlib/_axes.py in __init__(self, data, obj)
    157         colorbar = _find_associated_colorbar(obj)
    158         if colorbar:
--> 159             self._colorbar(colorbar, data)
    160 
    161         if self.is_subplot:

/usr/local/lib/python3.10/dist-packages/tikzplotlib/_axes.py in _colorbar(self, colorbar, data)
    420             colorbar_styles.append("ylabel={" + colorbar_ylabel + "}")
    421 
--> 422         mycolormap, is_custom_cmap = _mpl_cmap2pgf_cmap(
    423             colorbar.mappable.get_cmap(), data
    424         )

/usr/local/lib/python3.10/dist-packages/tikzplotlib/_axes.py in _mpl_cmap2pgf_cmap(cmap, data)
    670         cmap, mpl.colors.ListedColormap
    671     ), "Only LinearSegmentedColormap and ListedColormap are supported"
--> 672     return _handle_listed_color_map(cmap, data)
    673 
    674 

/usr/local/lib/python3.10/dist-packages/tikzplotlib/_axes.py in _handle_listed_color_map(cmap, data)
    792     }
    793     for mpl_cm, pgf_cm in cm_translate.items():
--> 794         if cmap.colors == plt.get_cmap(mpl_cm).colors:
    795             is_custom_colormap = False
    796             return (pgf_cm, is_custom_colormap)

ValueError: operands could not be broadcast together with shapes (256,4) (256,3) 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions