import plotly.graph_objects as go fig = go.Figure() fig.add_trace(go.Waterfall( x = [["2016", "2017", "2017", "2017", "2017", "2018", "2018", "2018", "2018"], ["initial", "q1", "q2", "q3", "total", "q1", "q2", "q3", "total"]], measure = ["absolute", "relative", "relative", "relative", "total", "relative", "relative", "relative", "total"], y = [1, 2, 3, -1, None, 1, 2, -4, None], base = 1000 )) fig.add_trace(go.Waterfall( x = [["2016", "2017", "2017", "2017", "2017", "2018", "2018", "2018", "2018"], ["initial", "q1", "q2", "q3", "total", "q1", "q2", "q3", "total"]], measure = ["absolute", "relative", "relative", "relative", "total", "relative", "relative", "relative", "total"], y = [1.1, 2.2, 3.3, -1.1, None, 1.1, 2.2, -4.4, None], base = 1000 )) fig.update_layout( waterfallgroupgap = 0.5, ) fig.show() This tutorial will tackle how you can create a waterfall plot or chart in Python. We will use the Matplotlib and waterfall_chart library for two dimensions and three-dimension waterfall plots. Create 2D Waterfall Plot With Matplotlib in PythonThe waterfall chart is common in finance, but the waterfall is not so effective when you have part-to-whole relationships. Let’s say you want to show our project cost breakdown. How it looks like in a table takes a bit of time for people to go through the table and compare the numbers. Take note that the waterfall charts do not come with Python default packages. First, we need to install the 2 package.
If you get any error during installation, upgrade your 3 using the following command.
We need to import the required libraries to create a waterfall chart or plot.
The 4 parameter takes the key-value pair. Using this parameter is to access figure elements like the figure size and face color.
Then we create a data set for XYZ company. We are going to visualize the sale of XYZ company with corresponding months.
Full Code - 2D waterfall plot:
Output: The profit raised with a green plot and lost means the minus points go with the red plot, and the net sale of XYZ company goes with a blue plot. Create 3D Waterfall Plot With Matplotlib in PythonIn the previous example, we’ve learned how to create a 2D waterfall plot. This section will demonstrate creating a 3D waterfall plot using the 5 class from the Matplotlib library.We will import the following required libraries to create a 3D waterfall plot.
If we want to create a 3D plot, we need to call the 6 method from 7.
Now we generate some random data sets for three dimensions. The 8 variable stores the given range using the 9 numpy method, and the 0 variable will generate a random integer number in every iteration using the Numpy 1 method.
The 2 function takes the first parameter as a list of data points, and the second parameter is 3 that help us display specific colors. We have already imported colors as 4 from Matplotlib, and we have defined our custom function called 5.The 6 function is called with a color value parameter and this function returns the 7 method with color and alpha parameters. |