python调用matplotlib模块绘制柱状图

yipeiwu_com6年前Python基础

我们可以调用matplotlib 绘制我们的柱状图,柱状图可以是水平的也可以是竖直的。

在这里我先记录下竖直的柱状图怎么绘制

在这里一般用到的函数就是bar

# bar(left, height, width=0.8, bottom=None, hold=None, **kwargs) 
# 绘制柱形图 
# left:柱形图的x坐标 
# height柱形图的高度,以0.0为基准 
# width:柱形图的宽度,默认0.8 
# facecolor:颜色 
# edgecolor:边框颜色n 
# bottom:表示底部从y轴的哪个刻度开始画 
# yerr:应该是对应的数据的误差范围,加上这个参数,柱状图头部会有一个蓝色的范围标识,标出允许的误差范围,在水平柱状图中这个参数为xerr 

在这里我一般特别喜欢将柱状图的边缘颜色设置为白色,因为这样画出来比较好看

eg.

plt.bar(x,+y1,width=0.8,facecolor="#9999ff",edgecolor="white",yerr=error)

下面来说一下画bar chart 的步骤

首先我们需要引入两个模块:

import numpy as np 
import matplotlib.pyplot as plt 

import numpy as np 
import matplotlib.pyplot as plt 
n = 12 
# 生成一个1-12的列表,不包括12,[ 0 1 2 3 4 5 6 7 8 9 10 11] 
x = np.arange(n) 
# np.random.uniform(0.5,1.0,n),生成n个0.5-1.0之间的随机数 
y1 = 3 * np.random.uniform(0.5,1.0,n) 
y2 = 3 * np.random.uniform(0.5,1.0,n) 
# 在这里我们是使用一个随机生成函数生成了两组y的值,生成的这个随机数是服从均匀分布的
# 如果我们的数值比较少我们可以直接给y赋值
# y = [5,7,3]

# 生成一个包含有n个值,均为0.2的list,表示允许的误差范围[-0.2,0.2] 
error = [0.2,] * n 

# bar(left, height, width=0.8, bottom=None, hold=None, **kwargs) 
# 绘制柱形图 
# left:柱形图的x坐标 
# height柱形图的高度,以0.0为基准 
# width:柱形图的宽度,默认0.8 
# facecolor:颜色 
# edgecolor:边框颜色n 
# bottom:表示底部从y轴的哪个刻度开始画 
# yerr:应该是对应的数据的误差范围,加上这个参数,柱状图头部会有一个蓝色的范围标识,标出允许的误差范围,在水平柱状图中这个参数为xerr 
plt.bar(x,+y1,width=0.8,facecolor="#9999ff",edgecolor="white",yerr=error) 
plt.bar(x,-y2,facecolor="#ff9999",edgecolor="white") 
# 绘制文字,显示柱状图形的值 
for x,y1,y2 in zip(x,y1,y2): 
 plt.text(x+0.4,y1+0.05,'%.2f' % y1,ha='center',va='bottom') 
 plt.text(x+0.4,-(y2+0.05),'%.2f' % y2,ha='center',va='top') 

plt.ylim(-3.5,3.5) 
plt.show() 

如果我们需要的是给我们柱状图绘制一些标记,比如横坐标和纵坐标的值,这个时候我们可以像下面这样做。这个例子我用的是官网上的代码。

# Credit: Josh Hemann

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from collections import namedtuple


n_groups = 5

means_men = (20, 35, 30, 35, 27)
std_men = (2, 3, 4, 1, 2)

means_women = (25, 32, 34, 20, 25)
std_women = (3, 5, 2, 3, 3)

fig, ax = plt.subplots()

index = np.arange(n_groups)
bar_width = 0.35

opacity = 0.4
error_config = {'ecolor': '0.3'}

rects1 = ax.bar(index, means_men, bar_width,
    alpha=opacity, color='b',
    yerr=std_men, error_kw=error_config,
    label='Men')

rects2 = ax.bar(index + bar_width, means_women, bar_width,
    alpha=opacity, color='r',
    yerr=std_women, error_kw=error_config,
    label='Women')

ax.set_xlabel('Group')
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(index + bar_width / 2)
ax.set_xticklabels(('A', 'B', 'C', 'D', 'E'))
ax.legend()

fig.tight_layout()
plt.show()

在这里我们设置的X的坐标以及上边的标签,我们主要的代码是:

ax.bar(index, means_men, bar_width,
    alpha=opacity, color='b',
    yerr=std_men, error_kw=error_config,
    label='Men')

ax.set_xticks(index + bar_width / 2) # 设置坐标的其实坐标
ax.set_xticklabels(('A', 'B', 'C', 'D', 'E'))   

这里的bar函数的参数和我们开始介绍的是一样的,只是我们在设置坐标的时候,一般是我们的条形图的中间所以我们要把宽度除以2

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

使用Python生成200个激活码的实现方法

题目:使用 Python 生成 200 个不重复的激活码 编写思路 # 激活码一般是由26个大写字母和10个数字任意组合而成 # 长度为12位或者16位的居多激活码 # 一个激活码里的字...

一键搞定python连接mysql驱动有关问题(windows版本)

对于mysql驱动问题折腾了一下午,现共享出解决方案 1:手动安装驱动 完全是场噩梦,推荐大家采用自动安装 2:自动安装 下载自动安装包,下载地址:https://www.jb51.ne...

Python使用正则匹配实现抓图代码分享

内涵:正则匹配,正则替换,页面抓取,图片保存 。 实用的第一次 Python 代码 参考 #!/usr/bin/env python import urllib import re...

Python多线程及其基本使用方法实例分析

本文实例讲述了Python多线程及其基本使用方法。分享给大家供大家参考,具体如下: 学习Python的多线程(Multi-threading),至少应该要有进程与线程的基本概念,可以参考...

Python实现的各种常见分布算法示例

Python实现的各种常见分布算法示例

本文实例讲述了Python实现的各种常见分布算法。分享给大家供大家参考,具体如下: #-*- encoding:utf-8 -*- import numpy as np from s...