Cara menggunakan 3d waterfall plot python

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 Python

The 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

python -m pip install --upgrade pip
2 package.

pip install waterfallcharts

If you get any error during installation, upgrade your

python -m pip install --upgrade pip
3 using the following command.

python -m pip install --upgrade pip

We need to import the required libraries to create a waterfall chart or plot.

import matplotlib.pyplot as plot
import waterfall_chart as waterfall

The

python -m pip install --upgrade pip
4 parameter takes the key-value pair. Using this parameter is to access figure elements like the figure size and face color.

plot.rcParams["figure.figsize"] = (8,6)
plot.rcParams["figure.facecolor"]="yellow"

Then we create a data set for XYZ company. We are going to visualize the sale of XYZ company with corresponding months.

x1,y1=["Jan","Feb","March","April","May","jun","July","Aug"],[10,25,30,50,-10,15,-5,10]
waterfall.plot(x1, y1,)

Full Code - 2D waterfall plot:

import matplotlib.pyplot as plot
import waterfall_chart as waterfall

# Set set width and height and face color as yellow
plot.rcParams["figure.figsize"] = (8,6)
plot.rcParams["figure.facecolor"]="yellow"
# dependant and independent variables
x1,y1=["Jan","Feb","March","April","May","jun","July","Aug"],[10,25,30,50,-10,15,-5,10]
waterfall.plot(x1, y1,)
# Set x labels with 45 rotation
plot.xticks(rotation=45)

plot.title("2D Waterfall plot")
# Add the padding and merging for visual elements
plot.tight_layout()
plot.show()

Output:

Cara menggunakan 3d waterfall plot python

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 Python

In the previous example, we’ve learned how to create a 2D waterfall plot. This section will demonstrate creating a 3D waterfall plot using the

python -m pip install --upgrade pip
5 class from the Matplotlib library.

We will import the following required libraries to create a 3D waterfall plot.

from matplotlib.collections import PolyCollection
import matplotlib.pyplot as plt
from matplotlib import colors as mcolors
import numpy as np

If we want to create a 3D plot, we need to call the

python -m pip install --upgrade pip
6 method from
python -m pip install --upgrade pip
7.

axes=plt.axes(projection="3d")

Now we generate some random data sets for three dimensions. The

python -m pip install --upgrade pip
8 variable stores the given range using the
python -m pip install --upgrade pip
9 numpy method, and the
import matplotlib.pyplot as plot
import waterfall_chart as waterfall
0 variable will generate a random integer number in every iteration using the Numpy
import matplotlib.pyplot as plot
import waterfall_chart as waterfall
1 method.

x1 = np.arange(0, 10, 0.4)
verts = []
z1 = [0.0, 1.0, 2.0, 3.0]
for z in z1:
    y1 = np.random.rand(len(x1))
    y1[0], y1[-1] = 0, 0
    verts.append(list(zip(x1, y1)))

The

import matplotlib.pyplot as plot
import waterfall_chart as waterfall
2 function takes the first parameter as a list of data points, and the second parameter is
import matplotlib.pyplot as plot
import waterfall_chart as waterfall
3 that help us display specific colors. We have already imported colors as
import matplotlib.pyplot as plot
import waterfall_chart as waterfall
4 from Matplotlib, and we have defined our custom function called
import matplotlib.pyplot as plot
import waterfall_chart as waterfall
5.

The

import matplotlib.pyplot as plot
import waterfall_chart as waterfall
6 function is called with a color value parameter and this function returns the
import matplotlib.pyplot as plot
import waterfall_chart as waterfall
7 method with color and alpha parameters.