在Matplotlib中為圖表元素添加紋理填充可以使用hatch
參數(shù)。hatch
參數(shù)可以接受以下值來指定不同的填充紋理:
'/'
: 斜線'\'
: 反斜線'|'
: 豎線'-'
: 橫線'+'
: 十字形'x'
: 叉號(hào)'o'
: 圓圈以下是一個(gè)示例代碼,演示如何使用hatch
參數(shù)為柱形圖添加斜線填充紋理:
import matplotlib.pyplot as plt
# 創(chuàng)建數(shù)據(jù)
height = [3, 5, 2, 7, 4]
bars = ['A', 'B', 'C', 'D', 'E']
# 創(chuàng)建柱形圖
plt.bar(bars, height, hatch='/')
# 添加標(biāo)題和標(biāo)簽
plt.title('Bar Chart with Hatch Pattern')
plt.xlabel('Categories')
plt.ylabel('Values')
# 顯示圖表
plt.show()
運(yùn)行上述代碼,將會(huì)得到一個(gè)帶有斜線填充紋理的柱形圖。您可以根據(jù)需要修改hatch
參數(shù)的值來改變填充紋理的樣式。