Skip to content

Commit

Permalink
plot: add a flag --scale
Browse files Browse the repository at this point in the history
  • Loading branch information
shenwei356 committed Oct 29, 2024
1 parent 72c49c5 commit 6e4f5c5
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 34 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@
- do not show progress.
- `csvtk fix-quotes`:
- new flag `-b, --buffer-size`.
- `csvtk plot`:
- new flag `--scale` for scaling the image width/height, tick, axes, line/point and font sizes proportionally, adviced by @tseemann.
- `csvtk plot line`:
- only add legend for more than one group. [#279](https://github.com/shenwei356/csvtk/issues/279)
- sort points by X for plotting lines. [#280](https://github.com/shenwei356/csvtk/issues/280)
- `csvtk hist`:
- new flags: `--line-width`.
- `csvtk box`:
- plots of different groups have different colors now.
- new flags: `--line-width`, `--point-size`, and `color-index`.
- [csvtk v0.30.0](https://github.com/shenwei356/csvtk/releases/tag/v0.30.0)
[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.30.0/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.30.0)
- `csvtk`:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ Examples
group information is the "Group" column.

csvtk -t plot box testdata/grouped_data.tsv.gz -g "Group" \
-f "GC Content" --width 3 | display
-f "GC Content" --width 3 --title "Box plot" | display

![boxplot.png](testdata/figures/boxplot.png)

Expand Down
28 changes: 28 additions & 0 deletions csvtk/cmd/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/spf13/cobra"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/plotutil"
"gonum.org/v1/plot/vg"
)

Expand Down Expand Up @@ -67,6 +68,14 @@ Notes:
if skipNA && len(naValues) == 0 {
log.Errorf("the value of --na-values should not be empty when using --skip-na")
}

lineWidth := vg.Points(getFlagPositiveFloat64(cmd, "line-width") * plotConfig.scale)
pointSize := vg.Length(getFlagPositiveFloat64(cmd, "point-size") * plotConfig.scale)
colorIndex := getFlagPositiveInt(cmd, "color-index")
if colorIndex > 7 {
checkError(fmt.Errorf("unsupported color index"))
}

naMap := make(map[string]interface{}, len(naValues))
for _, na := range naValues {
naMap[strings.ToLower(na)] = struct{}{}
Expand Down Expand Up @@ -148,6 +157,7 @@ Notes:
}

groupNames := make([]string, len(groupOrders))
j := colorIndex - 1
for i, group := range groupOrders {
groupNames[i] = group.Key
b, err := plotter.NewBoxPlot(w, float64(i), groups[group.Key])
Expand All @@ -156,6 +166,20 @@ Notes:
b.Horizontal = true
}
p.Add(b)

b.BoxStyle.Color = plotutil.Color(j)
b.BoxStyle.Width = lineWidth

b.MedianStyle.Color = plotutil.Color(j)
b.MedianStyle.Width = lineWidth

b.WhiskerStyle.Color = plotutil.Color(j)
b.WhiskerStyle.Width = lineWidth

b.GlyphStyle.Color = plotutil.Color(j)
b.GlyphStyle.Radius = pointSize

j++
}

if !horiz {
Expand Down Expand Up @@ -236,4 +260,8 @@ func init() {

boxCmd.Flags().Float64P("box-width", "", 0, "box width")
boxCmd.Flags().BoolP("horiz", "", false, "horize box plot")

boxCmd.Flags().Float64P("line-width", "", 1.5, "line width")
boxCmd.Flags().Float64P("point-size", "", 3, "point size")
boxCmd.Flags().IntP("color-index", "", 1, `color index, 1-7`)
}
5 changes: 5 additions & 0 deletions csvtk/cmd/hist.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ Notes:
config := getConfigs(cmd)
plotConfig := getPlotConfigs(cmd)

lineWidth := vg.Points(getFlagPositiveFloat64(cmd, "line-width") * plotConfig.scale)

files := getFileListFromArgsAndFile(cmd, args, true, "infile-list", true)
if len(files) > 1 {
checkError(fmt.Errorf("no more than one file should be given"))
Expand Down Expand Up @@ -137,6 +139,7 @@ Notes:

// h.Normalize(1)
h.FillColor = plotutil.Color(colorIndex - 1)
h.LineStyle.Width = lineWidth
p.Add(h)

p.Title.Text = plotConfig.title
Expand Down Expand Up @@ -193,4 +196,6 @@ func init() {
histCmd.Flags().IntP("color-index", "", 1, `color index, 1-7`)
histCmd.Flags().BoolP("percentiles", "", false, `calculate percentiles`)

histCmd.Flags().Float64P("line-width", "", 1, "line width")

}
5 changes: 3 additions & 2 deletions csvtk/cmd/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ Notes:
}
runtime.GOMAXPROCS(config.NumCPUs)

lineWidth := vg.Points(getFlagPositiveFloat64(cmd, "line-width"))
pointSize := vg.Length(getFlagPositiveFloat64(cmd, "point-size"))
scatter := getFlagBool(cmd, "scatter")
lineWidth := vg.Points(getFlagPositiveFloat64(cmd, "line-width") * plotConfig.scale)
pointSize := vg.Length(getFlagPositiveFloat64(cmd, "point-size") * plotConfig.scale)
colorIndex := getFlagPositiveInt(cmd, "color-index")
if colorIndex > 7 {
checkError(fmt.Errorf("unsupported color index"))
Expand Down Expand Up @@ -206,6 +206,7 @@ Notes:
if !scatter {
lines, points, err := plotter.NewLinePoints(v)
checkError(err)
fmt.Fprintln(os.Stderr, i)
lines.Color = plotutil.Color(i)
lines.LineStyle.Dashes = plotutil.Dashes(i)
lines.LineStyle.Width = lineWidth
Expand Down
21 changes: 14 additions & 7 deletions csvtk/cmd/plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func init() {
plotCmd.PersistentFlags().Float64P("axis-width", "", 1.5, "axis width")
plotCmd.PersistentFlags().Float64P("tick-width", "", 1.5, "axis tick width")
plotCmd.PersistentFlags().IntP("tick-label-size", "", 12, "tick label font size")
plotCmd.PersistentFlags().Float64P("scale", "", 1, "scale the image width/height, tick, axes, line/point and font sizes proportionally")

plotCmd.PersistentFlags().StringP("format", "", "png", `image format for stdout when flag -o/--out-file not given. available values: eps, jpg|jpeg, pdf, png, svg, and tif|tiff.`)

Expand Down Expand Up @@ -105,13 +106,18 @@ func getPlotConfigs(cmd *cobra.Command) *plotConfigs {
}

config.title = getFlagString(cmd, "title")
config.titleSize = vg.Length(getFlagPositiveInt(cmd, "title-size"))
config.labelSize = vg.Length(getFlagPositiveInt(cmd, "label-size"))
config.width = vg.Length(getFlagPositiveFloat64(cmd, "width"))
config.height = vg.Length(getFlagPositiveFloat64(cmd, "height"))
config.axisWidth = vg.Length(getFlagPositiveFloat64(cmd, "axis-width"))
config.tickWidth = vg.Length(getFlagPositiveFloat64(cmd, "tick-width"))
config.tickLabelSize = vg.Length(getFlagPositiveInt(cmd, "tick-label-size"))

scale := getFlagPositiveFloat64(cmd, "scale")
config.scale = scale

config.titleSize = vg.Length(int(float64(getFlagPositiveInt(cmd, "title-size")) * scale))
config.labelSize = vg.Length(int(float64(getFlagPositiveInt(cmd, "label-size")) * scale))
config.width = vg.Length(getFlagPositiveFloat64(cmd, "width") * scale)
config.height = vg.Length(getFlagPositiveFloat64(cmd, "height") * scale)
config.axisWidth = vg.Length(getFlagPositiveFloat64(cmd, "axis-width") * scale)
config.tickWidth = vg.Length(getFlagPositiveFloat64(cmd, "tick-width") * scale)
config.tickLabelSize = vg.Length(int(float64(getFlagPositiveInt(cmd, "tick-label-size")) * scale))

config.xlab = getFlagString(cmd, "xlab")
config.ylab = getFlagString(cmd, "ylab")

Expand Down Expand Up @@ -161,6 +167,7 @@ type plotConfigs struct {
title, xlab, ylab string
titleSize, labelSize, tickLabelSize vg.Length
width, height, axisWidth, tickWidth vg.Length
scale float64
xmin, xmax, ymin, ymax float64
xminStr, xmaxStr, yminStr, ymaxStr string
format string
Expand Down
59 changes: 35 additions & 24 deletions doc/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -4180,27 +4180,29 @@ Available Commands:
line line plot and scatter plot
Flags:
--axis-width float axis width (default 1.5)
-f, --data-field string column index or column name of data (default "1")
--format string image format for stdout when flag -o/--out-file not given. available
values: eps, jpg|jpeg, pdf, png, svg, and tif|tiff. (default "png")
-g, --group-field string column index or column name of group
--height float Figure height (default 4.5)
-h, --help help for plot
--label-size int label font size (default 14)
--na-values strings NA values, case ignored (default [,NA,N/A])
--skip-na skip NA values in --na-values
--axis-width float axis width (default 1.5)
-f, --data-field string column index or column name of data (default "1")
--format string image format for stdout when flag -o/--out-file not given. available
values: eps, jpg|jpeg, pdf, png, svg, and tif|tiff. (default "png")
-g, --group-field string column index or column name of group
--height float Figure height (default 4.5)
-h, --help help for plot
--label-size int label font size (default 14)
--na-values strings NA values, case ignored (default [,NA,N/A])
--scale float scale the image width/height, tick, axes, line/point and font sizes
proportionally (default 1)
--skip-na skip NA values in --na-values
--tick-label-size int tick label font size (default 12)
--tick-width float axis tick width (default 1.5)
--title string Figure title
--title-size int title font size (default 16)
--width float Figure width (default 6)
--x-max string maximum value of X axis
--x-min string minimum value of X axis
--xlab string x label text
--y-max string maximum value of Y axis
--y-min string minimum value of Y axis
--ylab string y label text
--tick-width float axis tick width (default 1.5)
--title string Figure title
--title-size int title font size (default 16)
--width float Figure width (default 6)
--x-max string maximum value of X axis
--x-min string minimum value of X axis
--xlab string x label text
--y-max string maximum value of Y axis
--y-min string minimum value of Y axis
--ylab string y label text
```

Expand Down Expand Up @@ -4236,8 +4238,11 @@ Usage:
csvtk plot hist [flags]
Flags:
--bins int number of bins (default 50)
--color-index int color index, 1-7 (default 1)
--bins int number of bins (default 50)
--color-index int color index, 1-7 (default 1)
-h, --help help for hist
--line-width float line width (default 1)
--percentiles calculate percentiles
```

Expand Down Expand Up @@ -4284,8 +4289,12 @@ Usage:
csvtk plot box [flags]
Flags:
--box-width float box width
--horiz horize box plot
--box-width float box width
--color-index int color index, 1-7 (default 1)
-h, --help help for box
--horiz horize box plot
--line-width float line width (default 1.5)
--point-size float point size (default 3)
```

Expand Down Expand Up @@ -4329,8 +4338,10 @@ Usage:
csvtk plot line [flags]
Flags:
--color-index int color index, 1-7 (default 1)
-x, --data-field-x string column index or column name of X for command line
-y, --data-field-y string column index or column name of Y for command line
-h, --help help for line
--legend-left locate legend along the left edge of the plot
--legend-top locate legend along the top edge of the plot
--line-width float line width (default 1.5)
Expand Down
Binary file modified testdata/figures/boxplot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified testdata/figures/boxplot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6e4f5c5

Please sign in to comment.