DEV Community

Moiz Ibrar
Moiz Ibrar

Posted on • Updated on

How to Read and Visualize CSV Data in Golang using Gonum/Plot

Introduction
**
CSV (Comma Separated Values) is a popular format used for storing and exchanging data. In this blog post, we will learn how to read CSV data and visualize it using the Gonum/Plot package in Golang.
**
Prerequisites

Before we start, make sure you have Golang installed on your machine. You can download and install it from the official Golang website (https://golang.org/dl/). We will also be using the Gonum/Plot package for visualization, which you can install by running the following command:

go get -u gonum.org/v1/plot/...

Enter fullscreen mode Exit fullscreen mode

We will be using the Titanic dataset for this example, which can be downloaded from the following link: https://www.kaggle.com/c/titanic/data. Download the dataset and save it as titanic.csv in your working directory.
**
Reading CSV Data**

To read CSV data in Golang, we will be using the encoding/csv package. The following code reads the titanic.csv file and stores the data in a slice of slices:

// Open the CSV file
file, err := os.Open("titanic.csv")
if err != nil {
    panic(err)
}
defer file.Close()

// Create a new CSV reader
reader := csv.NewReader(file)

// Skip the header row
if _, err := reader.Read(); err != nil {
    panic(err)
}

// Read in all the CSV records
records, err := reader.ReadAll()
if err != nil {
    panic(err)
}



Enter fullscreen mode Exit fullscreen mode

In this code, we first open the titanic.csv file and create a new csv.Reader. We then skip the header row using the Read() function, and read in all the CSV records using the ReadAll() function. The data is stored in a slice of slices, where each slice represents a row in the CSV file.
**
Visualizing CSV Data**

To visualize the CSV data, we will be using the Gonum/Plot package. The following code creates a scatter plot of the CSV data:

// Create a new plot
p := plot.New()

// Convert the CSV data to a plotter.Values
data := make(plotter.XYs, len(records))
for i, record := range records {
    x, err := strconv.ParseFloat(record[0], 64)
    if err != nil {
        panic(err)
    }
    y, err := strconv.ParseFloat(record[1], 64)
    if err != nil {
        panic(err)
    }
    data[i].X = x
    data[i].Y = y
}

// Add a scatter plot to the plot
scatter, err := plotter.NewScatter(data)
if err != nil {
    panic(err)
}
scatter.GlyphStyle.Color = color.RGBA{R: 255, A: 255}
scatter.GlyphStyle.Radius = vg.Points(4)

// Add the scatter plot to the plot and set the axes labels
p.Add(scatter)
p.Title.Text = "Titanic Dataset"
p.X.Label.Text = "Age"
p.Y.Label.Text = "Fare"

// Save the plot to a PNG file
if err := p.Save(4*vg.Inch, 4*vg.Inch, "scatter.png"); err != nil {
    panic(err)
}

Enter fullscreen mode Exit fullscreen mode

In this code, we first create a new plot using plot.New(). We then convert the CSV data to a plotter.XYs slice, where each element represents a data point in the scatter

Apache-Age:-https://age.apache.org/
GitHub:-https://github.com/apache/age

Top comments (0)