小白教程
所有教程
关于
Search
172.70.100.162
172.70.100.162
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Matplotlib 柱形图”的源代码
本页内容
上一节:
Matplotlib_散点图
下一节:
Matplotlib_饼图
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:Matplotlib 柱形图}}[[Category:Matplotlib 教程|10]] = Matplotlib 柱形图 = 我们可以使用 pyplot 中的 bar() 方法来绘制柱形图。 bar() 方法语法格式如下: <sample title="" desc="" lang="python" hererun="1"> matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs) </sample> '''参数说明:''' '''x''':浮点型数组,柱形图的 x 轴数据。 '''height''':浮点型数组,柱形图的高度。 '''width''':浮点型数组,柱形图的宽度。 '''bottom''':浮点型数组,底座的 y 坐标,默认 0。 '''align''':柱形图与 x 坐标的对齐方式,'center' 以 x 位置为中心,这是默认值。 'edge':将柱形图的左边缘与 x 位置对齐。要对齐右边缘的条形,可以传递负数的宽度值及 align='edge'。 '''**kwargs:''':其他参数。 以下实例我们简单实用 bar() 来创建一个柱形图: <sample title="" desc="" lang="python" hererun="1"> import matplotlib.pyplot as plt import numpy as np x = np.array(["xiaoBai-1", "xiaoBai-2", "xiaoBai-3", "C-XiaoBai.Wang"]) y = np.array([12, 22, 6, 18]) plt.bar(x,y) plt.show() </sample> 显示结果如下: 垂直方向的柱形图可以使用 barh() 方法来设置: <sample title="" desc="" lang="python" hererun="1"> import matplotlib.pyplot as plt import numpy as np x = np.array(["xiaoBai-1", "xiaoBai-2", "xiaoBai-3", "C-XiaoBai.Wang"]) y = np.array([12, 22, 6, 18]) plt.barh(x,y) plt.show() </sample> 显示结果如下: 设置柱形图颜色: <sample title="" desc="" lang="python" hererun="1"> import matplotlib.pyplot as plt import numpy as np x = np.array(["xiaoBai-1", "xiaoBai-2", "xiaoBai-3", "C-XiaoBai.Wang"]) y = np.array([12, 22, 6, 18]) plt.bar(x, y, color = "#4CAF50") plt.show() </sample> 显示结果如下: 自定义各个柱形的颜色: <sample title="" desc="" lang="python" hererun="1"> import matplotlib.pyplot as plt import numpy as np x = np.array(["xiaoBai-1", "xiaoBai-2", "xiaoBai-3", "C-XiaoBai.Wang"]) y = np.array([12, 22, 6, 18]) plt.bar(x, y, color = ["#4CAF50","red","hotpink","#556B2F"]) plt.show() </sample> 显示结果如下: 设置柱形图宽度,'''bar()''' 方法使用 '''width''' 设置,'''barh()''' 方法使用 '''height''' 设置 height <sample title="" desc="" lang="python" hererun="1"> import matplotlib.pyplot as plt import numpy as np x = np.array(["xiaoBai-1", "xiaoBai-2", "xiaoBai-3", "C-XiaoBai.Wang"]) y = np.array([12, 22, 6, 18]) plt.bar(x, y, width = 0.1) plt.show() </sample> 显示结果如下: <sample title="" desc="" lang="python" hererun="1"> import matplotlib.pyplot as plt import numpy as np x = np.array(["xiaoBai-1", "xiaoBai-2", "xiaoBai-3", "C-XiaoBai.Wang"]) y = np.array([12, 22, 6, 18]) plt.barh(x, y, height = 0.1) plt.show() </sample> 显示结果如下:
返回至“
Matplotlib 柱形图
”。
上一节:
Matplotlib_散点图
下一节:
Matplotlib_饼图