https://github.com/jborrow/mplgo
A small golang package for using matplotlib colour maps
https://github.com/jborrow/mplgo
Last synced: 3 months ago
JSON representation
A small golang package for using matplotlib colour maps
- Host: GitHub
- URL: https://github.com/jborrow/mplgo
- Owner: JBorrow
- License: mit
- Created: 2024-08-14T18:53:34.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-08-14T20:59:52.000Z (9 months ago)
- Last Synced: 2024-12-27T06:41:27.113Z (5 months ago)
- Language: Go
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
mplgo
=====A small package for using `matplotlib` colour maps in `golang`.
Works by shelling out to `python` and extracting a color map directly from matplotlib,
to a `golang` struct. NaN values map to transparent white.Example Usage
-------------Download the package:
```
go get github.com/jborrow/mplgo
``````go
package mainimport (
"image/png"
"log"
"os"
"github.com/jborrow/mplgo"
)func MapArrayToPNG(m mplgo.ColorMap, in [][]float64, file_name string) error {
f, err := os.Create(file_name)
if err != nil {
return err
}
defer f.Close()// Encode the image to PNG and save it to the file
if err := png.Encode(f, m.MapArrayToImage(in)); err != nil {
panic(err)
}return nil
}func main() {
colorMap, err := mplgo.GetCmap("viridis", 128)if err != nil {
log.Fatal(err)
}// Example data
data := make([][]float64, 128)for i := range data {
line := make([]float64, 128)
for j := range line {
line[j] = (float64(i) / 128.0 * float64(j) / 128.0)
}
data[i] = line
}// Write to the world
err = MapArrayToPNG(colorMap, data, "hello_world.png")if err != nil {
log.Fatal(err)
}return
}
```