thomas-saigre / tikzplotly

Export plotly figures as TikZ/PGFplots for integration in LaTeX
https://pypi.org/project/tikzplotly/
MIT License
18 stars 1 forks source link

Support heatmaps #6

Closed JasonGross closed 4 months ago

JasonGross commented 7 months ago

I see "Trace type heatmap is not supported yet". Would it be possible to support this? What would it take?

JasonGross commented 7 months ago

Note that svg output on plotly just rasterizes the heatmap, so it seems reasonable to do something similar here.

thomas-saigre commented 7 months ago

I never used it, so I don't know what it should take. As you mentioned maybe not so much. I will try to look at it this week :) You can also make a pull request if you manage to do it :wink:

JasonGross commented 7 months ago

Here is an example usage:

example with matplotlib and tikzplotlib ```python import matplotlib.pyplot as plt import numpy as np import tikzplotlib data = np.random.rand(10,10) plt.figure(figsize=(8, 6)) plt.imshow(data, cmap='viridis') plt.colorbar() plt.title('Heatmap Example with Matplotlib') tikzplotlib.save('heatmap.tex') print(open('heatmap.tex', 'r').read()) ```

This generates

% This file was created with tikzplotlib v0.10.1.
\begin{tikzpicture}

\definecolor{darkgray176}{RGB}{176,176,176}

\begin{axis}[
colorbar,
colorbar style={ylabel={}},
colormap/viridis,
point meta max=0.992047081681384,
point meta min=0.00898827770104482,
tick align=outside,
tick pos=left,
title={Heatmap Example with Matplotlib},
x grid style={darkgray176},
xmin=-0.5, xmax=9.5,
xtick style={color=black},
y dir=reverse,
y grid style={darkgray176},
ymin=-0.5, ymax=9.5,
ytick style={color=black}
]
\addplot graphics [includegraphics cmd=\pgfimage,xmin=-0.5, xmax=9.5, ymin=9.5, ymax=-0.5] {heatmap-000.png};
\end{axis}

\end{tikzpicture}

So indeed it seems that the thing to do is just to create a raster image.

The corresponding code for an example usage in plotly:

import plotly.express as px
import numpy as np
data = np.random.rand(10,10)
fig = px.imshow(data, color_continuous_scale='Viridis', title='Heatmap Example with Plotly Express')
fig.show()
generated images heatmap-000.png: ![heatmap-000.png](https://github.com/thomas-saigre/tikzplotly/assets/396076/64fafe0a-c9ab-4595-a744-f411aeae7592) ![matplotlib image](https://github.com/thomas-saigre/tikzplotly/assets/396076/2dbb92e9-bd78-4b37-96e9-5ba9fda6270e) ![plotly image](https://github.com/thomas-saigre/tikzplotly/assets/396076/443a44db-5bbf-4261-91cf-55e8508177a2)
thomas-saigre commented 4 months ago

NB: in the creation of the PNG file from the heatmap, for some reason it happens that all blocks may not have the same size. During the process of reduction of the file, this lead to a wrong reduced size.

So what is currently done is that when the block do not have the same size, no reduction of the png is done at all. It takes more disk memory (1.5 ko instead of 131 o).

thomas-saigre commented 4 months ago

@JasonGross I don't think this is perfect, but I managed to add heatmaps to the exports type of the library.

exported tikz code ```tex \begin{tikzpicture} \begin{axis}[ y dir=reverse, colorbar, colormap={mycolor}{ rgb255(0.0cm)=(68,1,84); rgb255(0.1111111111111111cm)=(72,40,120); rgb255(0.2222222222222222cm)=(62,73,137); rgb255(0.3333333333333333cm)=(49,104,142); rgb255(0.4444444444444444cm)=(38,130,142); rgb255(0.5555555555555556cm)=(31,158,137); rgb255(0.6666666666666666cm)=(53,183,121); rgb255(0.7777777777777778cm)=(110,206,88); rgb255(0.8888888888888888cm)=(181,222,43); rgb255(1.0cm)=(253,231,37); }, point meta max=0.9871830820663744, point meta min=0.0019636453825200295, xmin=-0.5, xmax=9.5, ymin=-0.5, ymax=9.5, tick align=outside, tick pos=left, title=Heatmap Example with Plotly Express ] \addplot+ graphics[includegraphics cmd=\pgfimage,xmin=-0.5, xmax=9.5, ymin=9.5, ymax=-0.5] {/home/saigre/Documents/code/tikzplotly/src/tests/outputs/test_heatmap/fig11.png}; \end{axis} \end{tikzpicture} ```
generated figure ![image](https://github.com/thomas-saigre/tikzplotly/assets/60920471/a9b1c62a-2282-43af-b0d9-358e8b797a80)