Matplotlib Python MatPlot Bar函数参数介绍
在本文中,我们将介绍Matplotlib Python MatPlot中的Bar函数参数。Bar函数可以用来绘制柱状图,常见于统计分析等领域,下面将详细介绍各种参数的用途和示例。
阅读更多:Matplotlib 教程
参数介绍
Bar函数有很多参数,其中主要包括以下几个:
x和height
x是横坐标,height是纵坐标,它们用于指定柱状图中柱子的位置和高度。x可以是数值型、字符型的数组。
例如以下代码和结果:
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["A", "B", "C", "D"])
height = np.array([10, 20, 30, 40])
plt.bar(x, height)
plt.show()
width
width指定每个柱子的宽度,可以是浮点数或整数,默认值是0.8。例如以下代码和结果:
x = np.array(["A", "B", "C", "D"])
height = np.array([10, 20, 30, 40])
plt.bar(x, height, width=0.5)
plt.show()
color
color指定柱子的填充颜色,可以是字符串、元组、列表或ndarray。
例如以下代码和结果:
x = np.array(["A", "B", "C", "D"])
height = np.array([10, 20, 30, 40])
color = ["red", "green", "blue", "yellow"]
plt.bar(x, height, color=color)
plt.show()
hatch
hatch指定柱子的斜纹图案,可以是字符串。例如以下代码和结果:
x = np.array(["A", "B", "C", "D"])
height = np.array([10, 20, 30, 40])
color = ["red", "green", "blue", "yellow"]
hatch = ["/", "*", "+", "\\"]
plt.bar(x, height, color=color, hatch=hatch)
plt.show()
edgecolor和linewidth
edgecolor指定柱子的边框颜色,linewidth指定柱子的边框宽度。例如以下代码和结果:
x = np.array(["A", "B", "C", "D"])
height = np.array([10, 20, 30, 40])
color = ["red", "green", "blue", "yellow"]
edgecolor = ["black", "black", "black", "black"]
linewidth = [1, 2, 3, 4]
plt.bar(x, height, color=color, hatch=hatch, edgecolor=edgecolor, linewidth=linewidth)
plt.show()
tick_label和align
tick_label是指定每个柱子的标签,align指定柱子的对齐方式。align的取值可以是center、edge等。例如以下代码和结果:
x = np.array(["A", "B", "C", "D"])
height = np.array([10, 20, 30, 40])
color = ["red", "green", "blue", "yellow"]
tick_label = ["apple", "banana", "carrot", "dog"]
plt.bar(x, height, color=color, hatch=hatch, edgecolor=edgecolor, linewidth=linewidth, tick_label=tick_label, align="edge")
plt.show()
总结
本文介绍了Matplotlib Python MatPlot中Bar函数的各种参数,包括x、height、width、color、hatch、edgecolor、linewidth、tick_label和align等,希望对大家能有所帮助。