mplgo

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2024 License: MIT Imports: 7 Imported by: 0

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
package main

import (
	"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
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BAD_COLOR = color.RGBA{1.0, 1.0, 1.0, 0.0}
View Source
var PY_EXTRACTOR = "import matplotlib.pyplot as plt; print(plt.get_cmap('CMAP_NAME')([float(x) / float(STEPS - 1) for x in range(STEPS)]))"

Functions

This section is empty.

Types

type ColorMap

type ColorMap struct {
	// contains filtered or unexported fields
}

func GetCmap

func GetCmap(cmapName string, steps int) (ColorMap, error)

func GetCmapCustom

func GetCmapCustom(baseExtractor string, cmapName string, steps int, cols int) (ColorMap, error)

func (ColorMap) Map

func (m ColorMap) Map(in float64) color.RGBA

func (ColorMap) MapArray

func (m ColorMap) MapArray(in [][]float64) [][]color.RGBA

func (ColorMap) MapArrayToImage

func (m ColorMap) MapArrayToImage(in [][]float64) *image.RGBA

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳