使用 Matplotlib 向圖形中新增紋理


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

import matplotlib.pyplot as plt

Pyplot 是一組命令風格函式,可以讓 Matplotlib 像 MATLAB 一樣工作。除了繪製條形圖外,我們還將在圖形中新增一些紋理。bar() 函式中的 'hatch' 引數用於定義條形的紋理

演算法

Step 1: Define a list of values.
Step 2: Use the bar() function and define parameters like xaxis, yaxis, hatch.
Step 3: Plot the graph.

示例程式碼

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.bar(data_x, data_y, color='red', hatch = 'o')
plt.show()

輸出

更新於: 16-Mar-2021

481 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

入門
廣告
© . All rights reserved.