rocketlaunchr / dataframe-go

DataFrames for Go: For statistics, machine-learning, and data manipulation/exploration
Other
1.19k stars 95 forks source link

Draw graphs from columns of dataframe #52

Closed padchin closed 3 years ago

padchin commented 3 years ago

Hi! At the moment I have managed to plot a separate dataframe column by this strange method:

func main() {
        // all values of df are strings representing floating point numbers
        df := df, err := imports.LoadFromCSV(ctx, r, imports.CSVLoadOptions{Comma: ';'})
    s := df.Series[2] // trying to plot column 2
    series := dataframe.NewSeriesFloat64("test_name", nil, nil)

    i := s.ValuesIterator(dataframe.ValuesOptions{InitialRow: 0, Step: 1, DontReadLock: false})
    for {
        row, vals, _ := i()
        if row == nil {
            break
        }
        val, err := strconv.ParseFloat(vals.(string), 64)
        if err != nil {
            continue
        }
        series.Append(val)
    }
    Plot(series)
}

func Plot(ser *dataframe.SeriesFloat64) {
    ctx := context.TODO()
    cs, _ := wcharczuk_chart.S(ctx, ser, nil, nil)
    graph := chart.Chart{
        Title:  "test_graph",
        Width:  640,
        Height: 480,
        Series: []chart.Series{cs},
    }
    f, err := os.Create("graph.svg")
    if err != nil {
        panic(err)
    }
    defer f.Close()

    plt := bufio.NewWriter(f)
    _ = graph.Render(chart.SVG, plt)
}

Is there any simplier or more elegant method to do this job? And another question is if I can plot several columns on one plot? And if it is possible, how can I do this? Thanks in advance.

pjebs commented 3 years ago
  1. Series string should have a function called ToFloat
  2. You can add more series to []chart.Series{cs}
  3. For plotting, everything is outsourced to the wcharczuk package, so you'll need to read it's docs
padchin commented 3 years ago
  1. Series string should have a function called ToFloat

My s (type: dataframe.Series) does not have this method

pjebs commented 3 years ago

https://pkg.go.dev/github.com/rocketlaunchr/dataframe-go#SeriesString.ToSeriesFloat64 or alternatively, when loading CSV data, use DictateDataType to configure the column as float64 so you will already have a SeriesFloat64.

pjebs commented 3 years ago

You also don't need to save plot to file. See readme on usage.

padchin commented 3 years ago

Ok, then. I understood conserning DictateDataType. This is not my case as I don't know the name of columns in advance. But I cannot still understand how to extract SeriesString form dataframe's columns which have type Series.

pjebs commented 3 years ago

you need to type assert

padchin commented 3 years ago

I would really appreciate it if you could share a small example. Because this construction is not correct:

v := df.Series[2]
v1 := dataframe.NewSeriesString("name", nil, v)

I cannot imagine what should I do now.

pjebs commented 3 years ago

https://medium.com/@rocketlaunchr.cloud/type-conversions-casting-type-assertions-fb295430e387

padchin commented 3 years ago

https://pkg.go.dev/github.com/rocketlaunchr/dataframe-go#SeriesString.ToSeriesFloat64

Thanks for your assistance. Finally I was able to figure out the type assertions. Everything works now.