Skip to content

bar plot support for Y offset#476

Merged
swharden merged 4 commits intoScottPlot:masterfrom
bclehmann:waterfall
Jul 5, 2020
Merged

bar plot support for Y offset#476
swharden merged 4 commits intoScottPlot:masterfrom
bclehmann:waterfall

Conversation

@bclehmann
Copy link
Member

New contributors should review CONTRIBUTING.md

Purpose:
#463

New functionality (code):
Provide a code example demonstrating new functionality achieved with this pull request (if applicable):

Random rand = new Random(0);
int pointCount = 10;
double[] xs = DataGen.Consecutive(pointCount);
double[] ys = DataGen.RandomNormal(rand, pointCount, 5, 10);
double[] yOffsets = Enumerable.Range(0, pointCount).Select(count => ys.Take(count).Sum()).ToArray();

plt.PlotBar(xs, ys, yOffsets: yOffsets);

New functionality (image):
image

Opinion Wanted:

I wonder if PlottableBar should take a delegate to decide the colour of the bar, something like this:

Func<double y1, double y2, System.Drawing.Color> delegate = y2 - y1 > 0 ? System.Drawing.Color.Green : System.Drawing.Color.Red;

I photoshopped it to emulate the behaviour:
image

I like this personally, however this seems niche and could cause trouble for the legend, as one plottable would be multiple colours.

Autoformat your code:
The build will fail if your code is not auto-formatted. Auto-format your code in Visual Studio, or from the console using these commands:

cd ScottPlot/src/
dotnet tool install --global dotnet-format
dotnet format

@swharden swharden changed the title waterfall plots bar plot support for Y offset Jun 25, 2020
@swharden
Copy link
Member

Hey @Benny121221, this is an excellent improvement! By adding support for Y offsets you enabled a whole new type of plot type (a waterfall plot).

This one-liner is super useful for creating waterfall plots from a series of Y values.

double[] yOffsets = Enumerable.Range(0, pointCount).Select(count => ys.Take(count).Sum()).ToArray();

What do you think about adding a little function which runs that line under the hood to create the waterfall plot so the user doesn't have to? Something like this (where most of the arguments taken are simply passed to PlotBar):

plt.PlotWaterfall(xs, ys);

Such a method could accept arguments (preferably not delegates though) to customize coloring, and not require modification of the PlotBar method. Perhaps something like:

plt.PlotWaterfall(xs, ys, Color.Green, Color.Red);

@bclehmann
Copy link
Member Author

What do you think about adding a little function which runs that line under the hood to create the waterfall plot so the user doesn't have to? Something like this (where most of the arguments taken are simply passed to PlotBar):

plt.PlotWaterfall(xs, ys);

I like it, I am busy today but I'll certainly work on this when i get home or in the coming days.

@swharden
Copy link
Member

I like it, I am busy today but I'll certainly work on this when i get home or in the coming days.

Sounds like a plan!

@bclehmann
Copy link
Member Author

I added your suggestions, it looks like this:

image

The demo code is the following:

// generate random data to plot

Random rand = new Random(0);
int pointCount = 12;
double[] xs = DataGen.Consecutive(pointCount);
double[] ys = DataGen.RandomNormal(rand, pointCount, 5, 10);

System.Drawing.Color profitColor = System.Drawing.Color.Green;
System.Drawing.Color lossColor = System.Drawing.Color.Red;
string[] monthNames = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

plt.PlotWaterfall(xs, ys, fillColor: profitColor, negativeColor: lossColor);
plt.Title("Y1 Profits");
plt.XLabel("Month");
plt.XTicks(monthNames);
plt.YLabel("Million USD");

The negativeColor parameter has been added to PlotBar as well, and a user can still manually set yOffsets with PlotBar should they wish.

@swharden swharden merged commit ae23b76 into ScottPlot:master Jul 5, 2020
swharden added a commit that referenced this pull request Jul 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants