# Plots; pythonplot(): approximate heatmap as gradients to save space

**URL:** <https://discourse.julialang.org/t/plots-pythonplot-approximate-heatmap-as-gradients-to-save-space/108293>\
**Category:** General Usage\
**Tags:** plotting, plots, data-compression\
**Created:** [January 3, 2024, 10:48am UTC](https://discourse.julialang.org/t/plots-pythonplot-approximate-heatmap-as-gradients-to-save-space/108293 "2024-01-03T10:48:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![maltee1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maltee1/32/204977_2.png) [@maltee1](https://discourse.julialang.org/u/maltee1)\
**Post date:** [January 3, 2024, 10:48am UTC](https://discourse.julialang.org/t/plots-pythonplot-approximate-heatmap-as-gradients-to-save-space/108293/1 "2024-01-03T10:48:58Z")

</div>

Hi,

Is it possible to turn a heatmap from individual pixels into something that’s a little easier on storage? Especially, if nothing very exciting is happening in most places of the heatmap, a collection of gradients could convey the same data (minus ugly pixelation). The idea is to get something that looks smooth and pretty rather than ugly and pixely, but at the same time doesn’t need to store a large number of rectangles of fixed color for each data point.  
As a simple example, a linear combination of x and y as a heatmap would be sufficiently described by a single rectangle with a color gradient. Of course, my heatmap isn’t _that_ simple, so I’d need multiple shapes with multiple gradients.

---

<div class="post-metadata">

**Author:** ![maltee1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maltee1/32/204977_2.png) [@maltee1](https://discourse.julialang.org/u/maltee1)\
**Post date:** [January 3, 2024, 1:32pm UTC](https://discourse.julialang.org/t/plots-pythonplot-approximate-heatmap-as-gradients-to-save-space/108293/3 "2024-01-03T13:32:34Z")

</div>

Thanks for your reply. However, I need a heatmap plot with axes etc. and export it as an image (preferably vector graphics). I suppose just embedding the heatmap as pixel graphics in the svg export would already be an improvement in terms of storage.

---

<div class="post-metadata">

**Author:** ![maltee1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maltee1/32/204977_2.png) [@maltee1](https://discourse.julialang.org/u/maltee1)\
**Post date:** [January 3, 2024, 4:56pm UTC](https://discourse.julialang.org/t/plots-pythonplot-approximate-heatmap-as-gradients-to-save-space/108293/5 "2024-01-03T16:56:43Z")

</div>

Seems the gr backend stores the heatmap as a bitmap, so instead of a 1.5 MB image, I just have a 71 kB image.

The problem is that I’m using the pythonplot backend for all my other plots because of a bug in the gr backend that doesn’t support logarithmic scaling of the colorbar. Using different backends is not pretty at all. So if you could tell me how to make pythonplot store heatmaps as bitmaps, I’d be happy I guess 🙂

though some complex gradient would be even better, because there’d be no pixels at all.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 3, 2024, 4:56pm UTC](https://discourse.julialang.org/t/plots-pythonplot-approximate-heatmap-as-gradients-to-save-space/108293/6 "2024-01-03T16:56:58Z")

</div>

> [@maltee1](#):
>
> The idea is to get something that looks smooth and pretty rather than ugly and pixely, but at the same time doesn’t need to store a large number of rectangles of fixed color for each data point.

i.e. you want to compress an image by dividing it into blocks and representing each block approximately by a small number of basis functions (e.g. degree-1 polynomials). This is, of course, exactly what JPEG does (albeit with cosines rather than polynomial basis functions).

> [@maltee1](#):
>
> However, I need a heatmap plot with axes etc. and export it as an image (preferably vector graphics).

In principle, SVG can embed JPEG images, but I don’t know how to make Matplotlib rasterize a heatmap in an SVG using JPEG compression while keeping vector graphics for the rest?

---

<div class="post-metadata">

**Author:** ![maltee1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maltee1/32/204977_2.png) [@maltee1](https://discourse.julialang.org/u/maltee1)\
**Post date:** [January 4, 2024, 8:00am UTC](https://discourse.julialang.org/t/plots-pythonplot-approximate-heatmap-as-gradients-to-save-space/108293/7 "2024-01-04T08:00:02Z")

</div>

> [@stevengj](#):
>
> In principle, SVG can embed JPEG images, but I don’t know how to make Matplotlib rasterize a heatmap in an SVG using JPEG compression while keeping vector graphics for the rest?

It wouldn’t have to be jpeg, a simple, uncompressed bitmap would be better than each pixel being represented by its four corners and a color - all in ascii-converted floats.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 4, 2024, 8:21am UTC](https://discourse.julialang.org/t/plots-pythonplot-approximate-heatmap-as-gradients-to-save-space/108293/8 "2024-01-04T08:21:37Z")

</div>

In the absence of a MWE with code, it might be helpful to include an image of your heatmap, with all the scales (x, y and values).

---

<div class="post-metadata">

**Author:** ![maltee1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maltee1/32/204977_2.png) [@maltee1](https://discourse.julialang.org/u/maltee1)\
**Post date:** [January 4, 2024, 9:41am UTC](https://discourse.julialang.org/t/plots-pythonplot-approximate-heatmap-as-gradients-to-save-space/108293/9 "2024-01-04T09:41:51Z")

</div>

OK, maybe this is an easy one, but I just cannot figure out how to convert a matrix to an image using a color gradient.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 4, 2024, 10:40am UTC](https://discourse.julialang.org/t/plots-pythonplot-approximate-heatmap-as-gradients-to-save-space/108293/10 "2024-01-04T10:40:35Z")

</div>

Not easy to understand.

See if this example means something:

```julia
using Plots
x = range(-20, 20, 100); y = 2*x
m = sin.(x .* y')
c = cgrad([:red,:yellow,:green])
heatmap(x, y, m, color=c, clims=(-1,1))
savefig("Plots_heatmap_cgrad.png")

```

---

<div class="post-metadata">

**Author:** ![maltee1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maltee1/32/204977_2.png) [@maltee1](https://discourse.julialang.org/u/maltee1)\
**Post date:** [January 10, 2024, 8:02am UTC](https://discourse.julialang.org/t/plots-pythonplot-approximate-heatmap-as-gradients-to-save-space/108293/11 "2024-01-10T08:02:44Z")

</div>

Sorry for my late reply and thanks for yours. I would have to include the raster image in a plot made by the pythonplot backend, just exporting to an image is not sufficient.

However, I have figured that a contour plot probably fits my use case better, because the change of z is gradual and strictly increasing across both axes so I only have one line per level. It’s also easier to read because matching two colors is harder than reading a value.
