# Matplotlib 瀑布圖

Salman Mehmood 2024年2月15日

## 在 Python 中使用 Matplotlib 建立 2D 瀑布圖

``````pip install waterfallcharts
``````

``````python -m pip install --upgrade pip
``````

``````import matplotlib.pyplot as plot
import waterfall_chart as waterfall
``````

`rcParams` 引數採用鍵值對。使用此引數可以訪問圖形元素，例如圖形大小和麵部顏色。

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

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

``````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()
``````

## 在 Python 中使用 Matplotlib 建立 3D 瀑布圖

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

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

``````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)))
``````

`PolyCollection()` 函式將第一個引數作為資料點列表，第二個引數是幫助我們顯示特定顏色的 `facecolors`。我們已經從 Matplotlib 匯入了顏色為 `mcolors`，並且我們定義了我們的自定義函式 `colors`

`colors()` 函式使用顏色值引數呼叫，該函式返回帶有顏色和 alpha 引數的 `to_rgba()` 方法。

``````def colors(arg):
return mcolors.to_rgba(arg, alpha=0.6)

poly = PolyCollection(
verts, facecolors=[colors("r"), colors("g"), colors("b"), colors("y")]
)
``````

``````axes.set_xlim3d(0, 10)
axes.set_ylim3d(-1, 4)
axes.set_zlim3d(0, 1)
``````

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

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

def colors(arg):
return mcolors.to_rgba(arg, alpha=0.6)

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)))

poly = PolyCollection(
verts, facecolors=[colors("r"), colors("g"), colors("b"), colors("y")]
)
poly.set_alpha(0.7)

axes.set_xlabel("X")
axes.set_xlim3d(0, 10)
axes.set_ylabel("Y")
axes.set_ylim3d(-1, 4)
axes.set_zlabel("Z")
axes.set_zlim3d(0, 1)
axes.set_title("3D Waterfall plot")

plt.show()
``````

Hello! I am Salman Bin Mehmood(Baum), a software developer and I help organizations, address complex problems. My expertise lies within back-end, data science and machine learning. I am a lifelong learner, currently working on metaverse, and enrolled in a course building an AI application with python. I love solving problems and developing bug-free software for people. I write content related to python and hot Technologies.