Using Python to create Custom Graphs - Waterfall Diagram

Using Python to create Custom Graphs - Waterfall Diagram

A waterfall chart is a form of data visualization that helps in understanding the cumulative effect of sequentially introduced positive or negative values. These intermediate values can either be time based or category based. The waterfall chart is also known as a flying bricks chart or Mario chart due to the apparent suspension of columns (bricks) in mid-air. Often in finance, it will be referred to as a bridge.

To create waterfall diagram we first need to setup the following prerequisites:

  • Ploty

No alt text provided for this image

Just type this to check if you have python properly install. If it says “python is not recognized as an internal or external command, operable program or batch file.”

Install python properly.

Then check if pip command is installed properly.

No alt text provided for this image

Then write this command to install plotly.

Go to cmd for windows user or linux command line.

Write python, then type import plotly to check whether it is properly installed.

No alt text provided for this image

(You can also go to your directory where your python is installed to see plotly.py file)

So now open your code editor.

Type the following code.

No alt text provided for this image

You will get the following graph plot for following inputs:

Input:

No alt text provided for this image

Output:

No alt text provided for this image

This graph is a special case where we specially treat the case discussed in this scenario.

If we wish to plot arbitrary waterfall graph like placing total revenue at front changing colour sequences, we need to understand various parameters in plotly object_creator.

Let us understand them one by one:

1. Waterfall: Its self-inbuilt object creator in plotly to create waterfall object.

Important parameters

arg

Dict of properties compatible with this constructor or an instance of :class: ”plotly.graph_objs.Waterfall”.

alignmentgroup

Set several traces linked to the same position axis or matching axes to the same alignmentgroup. This controls whether bars compute their positional range dependently or independently.

base

Sets where the bar base is drawn (in position axis units).

cliponaxis

Determines whether the text nodes are clipped about the subplot axes. To show the text nodes above axis lines and tick labels, make sure to set `xaxis.layer` and `yaxis.layer` to *below traces*.

connector

:class:`plotly.graph_objects.waterfall.Connector` instance or dict with compatible properties

constraintext

Constrain the size of text inside or outside a bar to be no larger than the bar itself.

customdata

Assigns extra data each datum. This may be useful when listening to hover, click and selection events. Note that, "scatter" traces also appends customdata items in the markers DOM elements.

customdatasrc

Sets the source reference on Chart Studio Cloud for `customdata`.

decreasing

:class:`plotly.graph_objects.waterfall.Decreasing` instance or dict with compatible properties

dx

Sets the x coordinate step. See `x0` for more info.

dy

Sets the y coordinate step. See `y0` for more info.

hoverinfo

Determines which trace information appear on hover. If `none` or `skip` are set, no information is displayed upon hovering. But, if `none` is set, click and hover events are still fired.

outsidetextfont

Sets the font used for `text` lying outside the bar.

textfont

Sets the font used for `text`.

textinfo

Determines which trace information appear on the graph. In the case of having multiple waterfalls, totals are computed separately (per trace).

textposition

Specifies the location of the `text`. "inside" positions `text` inside, next to the bar end (rotated and scaled if needed). "outside" positions `text` outside, next to the bar end (scaled if needed), unless there is another bar stacked on this one, then the text gets pushed inside. "auto" tries to position `text` inside the bar, but if the bar is too small and no bar is stacked on this one the text is moved outside. If "none", no text appears.

x

Sets the x coordinates.

x0

Alternate to `x`. Builds a linear space of x coordinates. Use with `dx` where `x0` is the starting coordinate and `dx` the step.

xaxis

Sets a reference between this trace's x coordinates and a 2D cartesian x axis. If "x" (the default value), the x coordinates refer to `layout.xaxis`. If "x2", the x coordinates refer to `layout.xaxis2`, and so on.

y

Sets the y coordinates.

y0

Alternate to `y`. Builds a linear space of y coordinates. Use with `dy` where `y0` is the starting coordinate and `dy` the step

yaxis

Sets a reference between this trace's y coordinates and a 2D cartesian y axis. If "y" (the default value), they coordinates refer to `layout.yaxis`. If "y2", the y coordinates refer to `layout.yaxis2`, and so on.

measure

An array containing types of values. By default the values are considered as 'relative'. However; it is possible to use 'total' to compute the sums. Also 'absolute' could be applied to reset the computed total or to declare an initial value where needed. The 'measure' property is an array that may be specified as a tuple, list, numpy array, or pandas Series

As you can see in the code itself, we have used parameters measure, x and y:

Where we have set measure value as n-d array/ vector of elements consisting of “relative”, here relative is measure of difference in terms of previous value where default value is set to be 0.

If you haven’t numpy configured in your system plotly would raise exception. It was pre-assumed you have matplotlib and numpy installed on your system if not please go through the next section carefully.

For windows users write this following command on cmd and for linux write this in the linux shell.

No alt text provided for this image

NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.

Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits like Tkinter, wxPython, Qt, or GTK. There is also a procedural "pylab" interface based on a state machine (like OpenGL), designed to closely resemble that of MATLAB, though its use is discouraged.[3] SciPy makes use of Matplotlib.

You can also set values in measure array as “total” which measures from 0. So measure helps you to put your plot according to your needs.

x is an array to name your specified rectangular block as you can see in figure; and y is used to specify required length of blocks; absolute or relative according to your needs.

Some Usage of waterfall chart:

A waterfall chart can be used for analytical purposes, especially for understanding or explaining the gradual transition in the quantitative value of an entity that is subjected to increment or decrement. Often, a waterfall or cascade chart is used to show changes in revenue or profit between two time periods.

Waterfall charts can be used for various types of quantitative analysis, ranging from inventory analysis to performance analysis.

Waterfall charts are also commonly used in financial analysis to display how a net value is arrived at through gains and losses over time or between actual and budgeted amounts. Changes in cash flows or income statement line items can also be shown via a waterfall chart. Other non-business applications include tracking demographic and legal activity changes over time.

By Ansh Mishra

要查看或添加评论,请登录

Shorthills AI的更多文章

社区洞察

其他会员也浏览了