绘图仪:条形图
 

条形图

可以从一系列值创建简单的条形图。默认情况下,值被放置在X位置0、1、2等处。

var plt = new ScottPlot.Plot(600, 400);

// create sample data
double[] values = { 26, 20, 23, 7, 16 };

// add a bar graph to the plot
plt.AddBar(values);

// adjust axis limits so there is no padding below the bar graph
plt.SetAxisLimits(yMin: 0);

plt.SaveFig("bar_quickstart.png");
 

定义位置的条形图

可以手动定义每个杆的水平位置。如果定义了条的位置,可能还需要定义条的宽度。

var plt = new ScottPlot.Plot(600, 400);

// create sample data
double[] values = { 26, 20, 23, 7, 16 };
double[] positions = { 10, 20, 30, 40, 50 };

// add a bar graph to the plot
var bar = plt.AddBar(values, positions);

// customize the width of bars (80% of the inter-position distance looks good)
bar.BarWidth = (positions[1] - positions[0]) * .8;

// adjust axis limits so there is no padding below the bar graph
plt.SetAxisLimits(yMin: 0);

plt.SaveFig("bar_positions.png");
 

酒吧标签

放置在特定位置的条可以通过为这些位置设置勾号标签进行标记。

var plt = new ScottPlot.Plot(600, 400);

double[] values = { 26, 20, 23, 7, 16 };
double[] positions = { 0, 1, 2, 3, 4 };
string[] labels = { "PHP", "JS", "C++", "GO", "VB" };
plt.AddBar(values, positions);
plt.XTicks(positions, labels);
plt.SetAxisLimits(yMin: 0);

plt.SaveFig("bar_labels.png");
 

带误差条的条形图

Errorbars可以添加到任何条形图中。

var plt = new ScottPlot.Plot(600, 400);

// add a bar graph to the plot
double[] values = { 26, 20, 23, 7, 16 };
var bar = plt.AddBar(values);

// add errorbars to the bar graph and customize styling as desired
double[] errors = { 3, 2, 5, 1, 3 };
bar.ValueErrors = errors;
bar.ErrorCapSize = .1;

// adjust axis limits so there is no padding below the bar graph
plt.SetAxisLimits(yMin: 0);

plt.SaveFig("bar_error.png");
 

堆叠条形图

钢筋可以重叠,以呈现堆叠的外观。

var plt = new ScottPlot.Plot(600, 400);

// create sample data
double[] valuesA = { 1, 2, 3, 2, 1, 2, 1 };
double[] valuesB = { 3, 3, 2, 1, 3, 2, 1 };

// to simulate stacking B on A, shift B up by A
double[] valuesB2 = new double[valuesB.Length];
for (int i = 0; i < valuesB.Length; i++)
    valuesB2[i] = valuesA[i] + valuesB[i];

// plot the bar charts in reverse order (highest first)
plt.AddBar(valuesB2);
plt.AddBar(valuesA);

// adjust axis limits so there is no padding below the bar graph
plt.SetAxisLimits(yMin: 0);

plt.SaveFig("bar_stacked.png");
 

条形以上的值

每个条的值都可以显示在其上方。

var plt = new ScottPlot.Plot(600, 400);

// create sample data
double[] values = { 27.3, 23.1, 21.2, 16.1, 6.4, 19.2, 18.7, 17.3, 20.3, 13.1 };

// add a bar graph to the plot and enable values to be displayed above each bar
var bar = plt.AddBar(values);
bar.ShowValuesAboveBars = true;

// adjust axis limits so there is no padding below the bar graph
plt.SetAxisLimits(yMin: 0);

plt.SaveFig("bar_values.png");
 

自定义值格式化程序

可以使用自定义格式设置程序,使用条形图本身的数值在每个条形图上方生成标签。

var plt = new ScottPlot.Plot(600, 400);

Func<double, string> customFormatter = y => $"Y={y:N2}";

double[] values = { 27.3, 23.1, 21.2, 16.1, 6.4, 19.2, 18.7, 17.3, 20.3, 13.1 };
var bar = plt.AddBar(values);
bar.ShowValuesAboveBars = true;
bar.ValueFormatter = customFormatter;

plt.SetAxisLimits(yMin: 0);

plt.SaveFig("bar_values_custom_formatter.png");
 

条形填充图案

条形图填充图案可以定制。

var plt = new ScottPlot.Plot(600, 400);

var bar1 = plt.AddBar(new double[] { 10, 13, 15 }, new double[] { 1, 5, 9 });
bar1.HatchStyle = Drawing.HatchStyle.StripedUpwardDiagonal;
bar1.FillColor = Color.Gray;
bar1.FillColorHatch = Color.Black;
bar1.Label = "Series 1";

var bar2 = plt.AddBar(new double[] { 14, 15, 9 }, new double[] { 2, 6, 10 });
bar2.HatchStyle = Drawing.HatchStyle.StripedWideDownwardDiagonal;
bar2.FillColor = Color.DodgerBlue;
bar2.FillColorHatch = Color.DeepSkyBlue;
bar2.Label = "Series 2";

var bar3 = plt.AddBar(new double[] { 13, 6, 14 }, new double[] { 3, 7, 11 });
bar3.HatchStyle = Drawing.HatchStyle.LargeCheckerBoard;
bar3.FillColor = Color.SeaGreen;
bar3.FillColorHatch = Color.DarkSeaGreen;
bar3.Label = "Series 3";

// add a legend to display each labeled bar plot
plt.Legend(location: Alignment.UpperRight);

// adjust axis limits so there is no padding below the bar graph
plt.SetAxisLimits(yMin: 0, yMax: 20);

plt.SaveFig("bar_pattern.png");
 

水平条形图

条形图通常显示为列,但也可以将条形图显示为行。

var plt = new ScottPlot.Plot(600, 400);

// create sample data
double[] values = { 26, 20, 23, 7, 16 };
double[] errors = { 3, 2, 5, 1, 3 };
double[] positions = { 1, 2, 3, 4, 5 };

// add a bar graph to the plot and customize it to render horizontally
var bar = plt.AddBar(values, errors, positions);
bar.Orientation = Orientation.Horizontal;

// adjust axis limits so there is no padding to the left of the bar graph
plt.SetAxisLimits(xMin: 0);

plt.SaveFig("bar_horizontal.png");
 

分组条形图

通过自定义多个条形图的位置,可以实现分组条形图的外观。AddBarGroups()方法旨在简化此过程。更高级的分组和条形图样式可以使用人口图类型。

var plt = new ScottPlot.Plot(600, 400);

// generate random data to plot
int groupCount = 5;
Random rand = new(0);
double[] values1 = DataGen.RandomNormal(rand, groupCount, 20, 5);
double[] values2 = DataGen.RandomNormal(rand, groupCount, 20, 5);
double[] values3 = DataGen.RandomNormal(rand, groupCount, 20, 5);
double[] errors1 = DataGen.RandomNormal(rand, groupCount, 5, 2);
double[] errors2 = DataGen.RandomNormal(rand, groupCount, 5, 2);
double[] errors3 = DataGen.RandomNormal(rand, groupCount, 5, 2);

// group all data together
string[] groupNames = { "Group A", "Group B", "Group C", "Group D", "Group E" };
string[] seriesNames = { "Series 1", "Series 2", "Series 3" };
double[][] valuesBySeries = { values1, values2, values3 };
double[][] errorsBySeries = { errors1, errors2, errors3 };

// add the grouped bar plots and show a legend
plt.AddBarGroups(groupNames, seriesNames, valuesBySeries, errorsBySeries);
plt.Legend(location: Alignment.UpperRight);

// adjust axis limits so there is no padding below the bar graph
plt.SetAxisLimits(yMin: 0);

plt.SaveFig("bar_group.png");
 

具有Y偏移的条形图

默认情况下,条形图从0开始,但不一定是这样。可以为每个条形定义Y偏移。使用Y偏移时,值表示钢筋的高度(相对于其偏移)。

var plt = new ScottPlot.Plot(600, 400);

double[] values = { 23, 17, 19, 24, 22 };
double[] yOffsets = { -100, -100, -100, -100, -100 };

var bar = plt.AddBar(values);
bar.ValueOffsets = yOffsets;

// adjust axis limits so there is no padding below the bar graph
plt.SetAxisLimits(yMin: -100);

plt.SaveFig("bar_yoffset.png");
 

负条颜色

具有负值的条形图的颜色可能与正值的不同。

var plt = new ScottPlot.Plot(600, 400);

double[] values = { 23, -17, 19, -24, 22 };

var bar = plt.AddBar(values);
bar.FillColor = Color.Green;
bar.FillColorNegative = Color.Red;

plt.SaveFig("bar_yNegColor.png");
 

瀑布条形图

瀑布条形图使用条形图表示上一级别的值变化。该样式图可以通过将每个条与前面的所有条之和进行偏移来创建。这种效果类似于另一节中描述的金融情节(OHLC和烛台)。

var plt = new ScottPlot.Plot(600, 400);

double[] values = DataGen.RandomNormal(0, 12, 5, 10);
double[] offsets = Enumerable.Range(0, values.Length).Select(x => values.Take(x).Sum()).ToArray();

var bar = plt.AddBar(values);
bar.ValueOffsets = offsets;
bar.FillColorNegative = Color.Red;
bar.FillColor = Color.Green;

plt.SaveFig("bar_waterfall.png");
 

棒棒糖情节快速入门

棒棒糖图传达的信息与条形图相同,但外观不同。

var plt = new ScottPlot.Plot(600, 400);

double[] values = { 26, 20, 23, 7, 16 };
plt.AddLollipop(values);

plt.SaveFig("bar_lollipop.png");
 

棒棒糖情节定制

棒棒糖图可以广泛定制。

var plt = new ScottPlot.Plot(600, 400);

double[] values = { 26, 20, 23, 7, 16 };
var lollipop = plt.AddLollipop(values);
lollipop.Orientation = Orientation.Horizontal;
lollipop.LollipopRadius = 3;
lollipop.BorderColor = Color.Green;
lollipop.LollipopColor = Color.Blue;
lollipop.LollipopRadius = 10;

plt.SaveFig("bar_lollipop_custom.png");
 

克利夫兰点图

克利夫兰点图允许在条形图可能拥挤的情况下比较两个类别。

var plt = new ScottPlot.Plot(600, 400);

// Data from https://footystats.org/england/premier-league/home-away-league-table
double[] homeWins = { 12, 17, 16, 18, 18 };
double[] awayWins = { 11, 13, 16, 14, 14 };
string[] labels = { "2015/16", "2016/17", "2017/18", "2018/19", "2019/20" };

var clevelandDot = plt.AddClevelandDot(homeWins, awayWins);
clevelandDot.SetPoint1Style(label: "Home Wins");
clevelandDot.SetPoint2Style(label: "Away Wins", markerShape: MarkerShape.triUp);

plt.XTicks(labels);
plt.Title("British Premier League Champion Home vs Away Wins");
plt.Legend();

plt.SaveFig("bar_cleveland_dot.png");
 

日期时间条形图

条形图的默认宽度为1.0,但使用DateTime轴时,这意味着条形图的宽度为一天。要绘制日期时间数据,您可能需要手动将条形图的宽度设置为所需的大小(以一天的分数为单位)。

var plt = new ScottPlot.Plot(600, 400);

// let's plot 24 hours of data
int pointCount = 24;

// generate some random values
var rand = new Random(0);
double[] values = DataGen.Random(rand, pointCount, 1, 2);

// space every time point by 1 hour from a starting point
DateTime start = new DateTime(2021, 09, 24, 0, 0, 0);
double[] positions = new double[pointCount];
for (int i = 0; i < pointCount; i++)
    positions[i] = start.AddHours(i).ToOADate();

// display the bar plot using a time axis
var bar = plt.AddBar(values, positions);
plt.XAxis.DateTimeFormat(true);

// indicate each bar width should be 1/24 of a day then shrink sligtly to add spacing between bars
bar.BarWidth = (1.0 / 24) * .8;

// adjust axis limits so there is no padding below the bar graph
plt.SetAxisLimits(yMin: 0);

plt.SaveFig("bar_dateTime.png");