Skip to content

Commit 3455d84

Browse files
committed
Remove box_???_set_width functions in favour of BoxWidth PlotOption
1 parent 41d1e49 commit 3455d84

File tree

7 files changed

+86
-200
lines changed

7 files changed

+86
-200
lines changed

gnuplot/examples/box_and_whisker.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@ fn example(c: Common)
1010

1111
fg.axes2d()
1212
.set_title("Box and whisker", &[])
13-
.box_and_whisker_set_width(
13+
.box_and_whisker(
14+
[0.0f32, 1.0, 2.0].iter(),
15+
[-1.0f32, 0.0, 1.0].iter(),
16+
[-2.0f32, -1.0, 0.0].iter(),
17+
[2.0f32, 3.0, 4.0].iter(),
18+
[1.0f32, 2.0, 3.0].iter(),
19+
&[],
20+
)
21+
.box_and_whisker(
1422
[-0.6f32, 1.5, 2.5].iter(),
1523
[-1.0f32, 0.0, 1.0].iter(),
1624
[-2.0f32, -1.0, 0.0].iter(),
1725
[2.0f32, 3.0, 4.0].iter(),
1826
[1.0f32, 2.0, 3.0].iter(),
19-
[0.5f32, 0.25, 0.125].iter(),
2027
&[
28+
BoxWidth([0.5f64, 0.25, 0.125].into()),
2129
WhiskerBars(0.5),
2230
Color("blue".into()),
2331
LineWidth(2.0),

gnuplot/examples/example2.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
use crate::common::*;
33

44
use gnuplot::*;
5-
use std::iter::repeat;
65

76
mod common;
87

@@ -17,7 +16,6 @@ fn example(c: Common)
1716
let x2 = x2.iter2();
1817
let y2: Vec<i32> = x2.map(|&v| v * v).collect();
1918
let y2 = y2.iter2();
20-
let w = repeat(0.5f32);
2119

2220
let x3 = &[1i32, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
2321
let x3 = x3.iter2();
@@ -83,14 +81,14 @@ fn example(c: Common)
8381
LineStyle(DotDash),
8482
],
8583
)
86-
.boxes_set_width(
84+
.boxes(
8785
x,
8886
y1,
89-
w,
9087
&[
9188
LineWidth(2.0),
9289
Color("gray".into()),
9390
BorderColor("black".into()),
91+
BoxWidth([0.5, 0.4, 0.55, 0.7, 0.2].into()),
9492
],
9593
);
9694

gnuplot/examples/inverse_api.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ impl PlotElement for Lines
4242
WhiskerBars(v) => WhiskerBars(*v),
4343
FillPattern(v) => FillPattern(*v),
4444
Axes(v1, v2) => Axes(*v1, *v2),
45+
BoxWidth(v) => BoxWidth(v.to_vec()),
4546
});
4647
}
4748

gnuplot/examples/patterns.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ fn example(c: Common)
1212
ax.set_title("Patterns", &[]);
1313
ax.set_legend(Graph(1.), Graph(0.95), &[MaxRows(3)], &[]);
1414
ax.set_y_range(Auto, Fix(8.));
15+
ax.set_box_width(0.5, false);
1516
for i in 0..=8
1617
{
17-
ax.boxes_set_width(&[i], &[5], &[0.5], &[FillPattern(Auto)]);
18+
ax.boxes(&[i], &[5], &[FillPattern(Auto)]);
1819
}
1920

2021
for (i, &pattern) in [
@@ -31,10 +32,9 @@ fn example(c: Common)
3132
.iter()
3233
.enumerate()
3334
{
34-
ax.boxes_set_width(
35+
ax.boxes(
3536
&[i],
3637
&[-5],
37-
&[0.5],
3838
&[
3939
FillPattern(Fix(pattern)),
4040
Caption(&format!("{:?}", pattern)),

gnuplot/src/axes2d.rs

Lines changed: 26 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -667,8 +667,12 @@ impl Axes2D
667667
self
668668
}
669669

670-
/// Plot a 2D scatter-plot using boxes of automatic width. Box widths are set so that there are no gaps between successive boxes (i.e. each box may have a different width).
670+
/// Plot a 2D box-plot.
671+
/// Box widths are, by default set so that there are no gaps between successive boxes
672+
/// (i.e. each box may have a different width). This may be adjusted with (set_box_width())[Axes2D::set_box_width()]
673+
/// or by using the `BoxWidth` option.
671674
/// Boxes start at the x-axis and go towards the y value of the datapoint.
675+
///
672676
/// # Arguments
673677
/// * `x` - x values (center of the box)
674678
/// * `y` - y values
@@ -679,6 +683,8 @@ impl Axes2D
679683
/// * `BorderColor` - Sets the color of the border
680684
/// * `Color` - Sets the color of the box fill
681685
/// * `FillAlpha` - Sets the transparency of the box fill
686+
/// * `BoxWidth` - Sets the width of each box. If not supplied, the width will use the
687+
/// previously set box width, or be set to the spacing of the boxes
682688
pub fn boxes<
683689
'l,
684690
Tx: DataType,
@@ -696,42 +702,13 @@ impl Axes2D
696702
self
697703
}
698704

699-
/// Plot a 2D scatter-plot using boxes of set (per box) width.
700-
/// Boxes start at the x-axis and go towards the y value of the datapoint.
701-
/// # Arguments
702-
/// * `x` - x values (center of the box)
703-
/// * `y` - y values
704-
/// * `w` - Box width values
705-
/// * `options` - Array of PlotOption<&str> controlling the appearance of the plot element. The relevant options are:
706-
/// * `Caption` - Specifies the caption for this dataset. Use an empty string to hide it (default).
707-
/// * `LineWidth` - Sets the width of the border
708-
/// * `LineStyle` - Sets the style of the border
709-
/// * `BorderColor` - Sets the color of the border
710-
/// * `Color` - Sets the color of the box fill
711-
/// * `FillAlpha` - Sets the transparency of the box fill
712-
pub fn boxes_set_width<
713-
'l,
714-
Tx: DataType,
715-
X: IntoIterator<Item = Tx>,
716-
Ty: DataType,
717-
Y: IntoIterator<Item = Ty>,
718-
Tw: DataType,
719-
W: IntoIterator<Item = Tw>,
720-
>(
721-
&'l mut self, x: X, y: Y, w: W, options: &[PlotOption<&str>],
722-
) -> &'l mut Self
723-
{
724-
let (data, num_rows, num_cols) = generate_data!(options, x, y, w);
725-
self.common.elems.push(PlotElement::new_plot(
726-
Boxes, data, num_rows, num_cols, options,
727-
));
728-
self
729-
}
730-
731705
/// Plot a 2D box-plot with error bars using boxes of automatic width.
732-
/// Box widths are set so that there are no gaps between successive boxes (i.e. each box may have a different width).
706+
/// Box widths are, by default set so that there are no gaps between successive boxes
707+
/// (i.e. each box may have a different width). This may be adjusted with (set_box_width())[Axes2D::set_box_width()]
708+
/// or by using the `BoxWidth` option.
733709
/// Boxes start at the x-axis and go towards the y value of the datapoint.
734710
/// Each box has an error bar from y - y_delta to y + y_delta.
711+
///
735712
/// # Arguments
736713
/// * `x` - x values (center of the box)
737714
/// * `y` - y values
@@ -743,6 +720,8 @@ impl Axes2D
743720
/// * `BorderColor` - Sets the color of the border
744721
/// * `Color` - Sets the color of the box fill
745722
/// * `FillAlpha` - Sets the transparency of the box fill
723+
/// * `BoxWidth` - Sets the width of each box. If not supplied, the width will use the
724+
/// previously set box width, or be set to the spacing of the boxes
746725
pub fn box_error_delta<
747726
'l,
748727
Tx: DataType,
@@ -766,49 +745,10 @@ impl Axes2D
766745
self
767746
}
768747

769-
/// Plot a 2D box-plot with error bars using boxes of specified width.
770-
/// Box widths are set so that there are no gaps between successive boxes (i.e. each box may have a different width).
771-
/// Boxes start at the x-axis and go towards the y value of the datapoint.
772-
/// Each box has an error bar from y - y_delta to y + y_delta.
773-
/// # Arguments
774-
/// * `x` - x values (center of the box)
775-
/// * `y` - y values
776-
/// * `y_delta` - errors in y (error bars are plotted from y - y_delta to y + y_delta)
777-
/// * `x_delta` - errors in x (interpreted as box width)
778-
/// * `options` - Array of PlotOption<&str> controlling the appearance of the plot element. The relevant options are:
779-
/// * `Caption` - Specifies the caption for this dataset. Use an empty string to hide it (default).
780-
/// * `LineWidth` - Sets the width of the border
781-
/// * `LineStyle` - Sets the style of the border
782-
/// * `BorderColor` - Sets the color of the border
783-
/// * `Color` - Sets the color of the box fill
784-
/// * `FillAlpha` - Sets the transparency of the box fill
785-
pub fn box_error_delta_set_width<
786-
'l,
787-
Tx: DataType,
788-
X: IntoIterator<Item = Tx>,
789-
Ty: DataType,
790-
Y: IntoIterator<Item = Ty>,
791-
Tye: DataType,
792-
YE: IntoIterator<Item = Tye>,
793-
Tw: DataType,
794-
W: IntoIterator<Item = Tw>,
795-
>(
796-
&'l mut self, x: X, y: Y, y_error: YE, x_delta: W, options: &[PlotOption<&str>],
797-
) -> &'l mut Self
798-
{
799-
let (data, num_rows, num_cols) = generate_data!(options, x, y, y_error, x_delta);
800-
self.common.elems.push(PlotElement::new_plot(
801-
BoxErrorBars,
802-
data,
803-
num_rows,
804-
num_cols,
805-
options,
806-
));
807-
self
808-
}
809-
810-
/// Plot a 2D box-plot with error bars using boxes of automatic width.
811-
/// Box widths are set so that there are no gaps between successive boxes (i.e. each box may have a different width).
748+
/// Plot a 2D box-plot with error bars.
749+
/// Box widths are, by default set so that there are no gaps between successive boxes
750+
/// (i.e. each box may have a different width). This may be adjusted with (set_box_width())[Axes2D::set_box_width()]
751+
/// or by using the `BoxWidth` option.
812752
/// Boxes start at the x-axis and go towards the y value of the datapoint.
813753
/// Each box has an error bar from y - y_low to y + y_high.
814754
/// # Arguments
@@ -823,6 +763,8 @@ impl Axes2D
823763
/// * `BorderColor` - Sets the color of the border
824764
/// * `Color` - Sets the color of the box fill
825765
/// * `FillAlpha` - Sets the transparency of the box fill
766+
/// * `BoxWidth` - Sets the width of each box. If not supplied, the width will use the
767+
/// previously set box width, or be set to the spacing of the boxes
826768
pub fn box_error_low_high<
827769
'l,
828770
Tx: DataType,
@@ -851,51 +793,11 @@ impl Axes2D
851793
self
852794
}
853795

854-
/// Plot a 2D box-plot with error bars using boxes of specified width.
855-
/// Box widths are set so that there are no gaps between successive boxes (i.e. each box may have a different width).
856-
/// Boxes start at the x-axis and go towards the y value of the datapoint.
857-
/// Each box has an error bar from y - y_low to y + y_high.
858-
/// # Arguments
859-
/// * `x` - x values (center of the box)
860-
/// * `y` - y values
861-
/// * `y_low` - minimum of error bar
862-
/// * `y_high` - maximum of error bar
863-
/// * `x_delta` - errors in x (interpreted as box width)
864-
/// * `options` - Array of PlotOption<&str> controlling the appearance of the plot element. The relevant options are:
865-
/// * `Caption` - Specifies the caption for this dataset. Use an empty string to hide it (default).
866-
/// * `LineWidth` - Sets the width of the border
867-
/// * `LineStyle` - Sets the style of the border
868-
/// * `BorderColor` - Sets the color of the border
869-
/// * `Color` - Sets the color of the box fill
870-
/// * `FillAlpha` - Sets the transparency of the box fill
871-
pub fn box_error_low_high_set_width<
872-
'l,
873-
Tx: DataType,
874-
X: IntoIterator<Item = Tx>,
875-
Ty: DataType,
876-
Y: IntoIterator<Item = Ty>,
877-
Tyl: DataType,
878-
YL: IntoIterator<Item = Tyl>,
879-
Tyh: DataType,
880-
YH: IntoIterator<Item = Tyh>,
881-
Tw: DataType,
882-
W: IntoIterator<Item = Tw>,
883-
>(
884-
&'l mut self, x: X, y: Y, y_low: YL, y_high: YH, x_delta: W, options: &[PlotOption<&str>],
885-
) -> &'l mut Self
886-
{
887-
let (data, num_rows, num_cols) = generate_data!(options, x, y, y_low, y_high, x_delta);
888-
self.common.elems.push(PlotElement::new_plot(
889-
BoxErrorBars,
890-
data,
891-
num_rows,
892-
num_cols,
893-
options,
894-
));
895-
self
896-
}
897-
898-
/// Plot a 2D box-and-whisker plot using boxes of automatic width.
796+
/// Plot a 2D box-and-whisker plot.
797+
///
798+
/// Box widths are, by default set so that there are no gaps between successive boxes
799+
/// (i.e. each box may have a different width). This may be adjusted with (set_box_width())[Axes2D::set_box_width()]
800+
/// or by using the `BoxWidth` option.
899801
///
900802
/// # Arguments
901803
/// * `x` - x values (center of the box)
@@ -911,6 +813,8 @@ impl Axes2D
911813
/// * `Color` - Sets the color of the box fill
912814
/// * `FillAlpha` - Sets the transparency of the box fill
913815
/// * `WhiskerBars` - Sets the width of the whisker bars
816+
/// * `BoxWidth` - Sets the width of each box. If not supplied, the width will use the
817+
/// previously set box width, or be set to the spacing of the boxes
914818
pub fn box_and_whisker<
915819
'l,
916820
Tx: DataType,
@@ -940,61 +844,6 @@ impl Axes2D
940844
self
941845
}
942846

943-
/// Plot a 2D box-and-whisker plot using boxes of set width.
944-
///
945-
/// # Arguments
946-
/// * `x` - x values (center of the box)
947-
/// * `box_min` - minimum box y value
948-
/// * `whisker_min` - minimum whisker y value
949-
/// * `whisker_max` - maximum whisker y value
950-
/// * `box_max` - maximum box y value
951-
/// * `box_width` - width of the box (in x axis units)
952-
/// * `options` - Array of PlotOption<&str> controlling the appearance of the plot element. The relevant options are:
953-
/// * `Caption` - Specifies the caption for this dataset. Use an empty string to hide it (default).
954-
/// * `LineWidth` - Sets the width of the border
955-
/// * `LineStyle` - Sets the style of the border
956-
/// * `BorderColor` - Sets the color of the border
957-
/// * `Color` - Sets the color of the box fill
958-
/// * `FillAlpha` - Sets the transparency of the box fill
959-
/// * `WhiskerBars` - Sets the width of the whisker bars
960-
pub fn box_and_whisker_set_width<
961-
'l,
962-
Tx: DataType,
963-
X: IntoIterator<Item = Tx>,
964-
TBoxMin: DataType,
965-
BoxMin: IntoIterator<Item = TBoxMin>,
966-
TWhiskerMin: DataType,
967-
WhiskerMin: IntoIterator<Item = TWhiskerMin>,
968-
TWhiskerMax: DataType,
969-
WhiskerMax: IntoIterator<Item = TWhiskerMax>,
970-
TBoxMax: DataType,
971-
BoxMax: IntoIterator<Item = TBoxMax>,
972-
TBoxWidth: DataType,
973-
BoxWidth: IntoIterator<Item = TBoxWidth>,
974-
>(
975-
&'l mut self, x: X, box_min: BoxMin, whisker_min: WhiskerMin, whisker_max: WhiskerMax,
976-
box_max: BoxMax, box_width: BoxWidth, options: &[PlotOption<&str>],
977-
) -> &'l mut Self
978-
{
979-
let (data, num_rows, num_cols) = generate_data!(
980-
options,
981-
x,
982-
box_min,
983-
whisker_min,
984-
whisker_max,
985-
box_max,
986-
box_width
987-
);
988-
self.common.elems.push(PlotElement::new_plot(
989-
BoxAndWhisker,
990-
data,
991-
num_rows,
992-
num_cols,
993-
options,
994-
));
995-
self
996-
}
997-
998847
/// Plot 2D rectangular boxes - usually used for error bars - using specified by width (x_delta) and height (y_delta).
999848
///
1000849
/// # Arguments

gnuplot/src/options.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ pub enum PlotOption<T>
7474
WhiskerBars(f64),
7575
/// Which axis pair to use for the plot element.
7676
Axes(XAxis, YAxis),
77+
/// Box width set per box for box plots: each element is the width of one box
78+
BoxWidth(Vec<f64>),
7779
}
7880

7981
impl<'l> OneWayOwned for PlotOption<&'l str>
@@ -97,6 +99,7 @@ impl<'l> OneWayOwned for PlotOption<&'l str>
9799
WhiskerBars(v) => WhiskerBars(v),
98100
FillPattern(v) => FillPattern(v),
99101
Axes(x, y) => Axes(x, y),
102+
BoxWidth(ref d) => BoxWidth(d.clone()),
100103
}
101104
}
102105
}

0 commit comments

Comments
 (0)