Matplotlib怎么為圖表元素添加紋理填充

小億
114
2024-05-21 14:32:24
欄目: 編程語言

在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ù)的值來改變填充紋理的樣式。

0