使用 Matplotlib 顯示水平條形圖


在這個程式中,我們將使用 matplotlib 庫繪製條形圖。使用 matplotlib 庫解決 matplotlib 相關問題的最重要步驟是匯入 matplotlib 庫。語法是

import matplotlib.pyplot as plt

Pyplot 是一個命令樣式函式的集合,使 Matplotlib 像 MATLAB 一樣工作。我們將使用函式 barh() 來繪製水平條形圖

演算法

Step 1: Define a list of values.
Step 2: Use the barh() function in the matplotlib.pyplot library and define different parameters like height width, etc.
Step 3: Label the axes using xlabel() and ylabel().
Step 3: Plot the graph using show().

示例程式碼

import matplotlib.pyplot as plt

data_x = ['Mumbai', 'Delhi', 'Ahmedabad', 'Banglore']
data_y = [40, 35, 29, 32]

plt.xlabel("CITY")
plt.ylabel("POPULATION")
plt.title("BAR PLOT")
plt.barh(data_x, data_y, color='red')
plt.show()

輸出

更新於: 16-3-2021

803 瀏覽

開啟你的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.