Python绘图:matplotlib.pyplot笔记

一、柱状图
matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align=‘center’, data=None, **kwargs)
1、主要参数:
x: 柱形x的坐标,一般可采用arange函数产生一个序列
height: 柱形的高度,即需要展示的数据
width: 柱形的宽度,默认值0.8
bottom: 柱形基座的y坐标,默认值0
align: 横坐标数字在柱形的位置,可选{‘center’, ‘edge’},默认: ‘center’,若想标在右边,则可以通过width=负值,align=‘edge’实现

2、其他参数:
alpha: 透明度,数值为 float 类型或者默认
color/facecolor: 柱形颜色
label: 每个柱形图代表的含义,要加语句:plt.legend(loc=“upper left”) 才能显示label

附:matplotlib.pyplot.bar官方文档

二、添加数据标签
matplotlib.pyplot.text(x, y, s, fontdict=None, withdash=False, **kwargs)
1、主要参数:
x、y: 放置文本的位置。默认情况下,这是数据坐标。
s: str类型,文本内容

2、其他参数:
horizontalalignment: 水平对齐,ha={‘center’…}
verticalalignment: 垂直对齐,va={‘center’,‘bottom’…}
fontsize: 字体大小

附:matplotlib.pyplot.text官方文档

三、例子

法1:
plt.bar(X, Y, width=0.5)
for x, y in zip(X, Y):  # 在直方图上方标注数据
	plt.text(x, 1.03*y, '%s' % float(y), ha='center')

法2:
def autolabel(rects): 
    for rect in rects:
        height = rect.get_height()
        plt.text(rect.get_x() + rect.get_width() / 2., 1.03 * height, '%s' % float(height))
rect = plt.bar(left = (0, 1), height = (1, 0.5), width = 0.2, align="center")
autolabel(rect)

四、python中字符串list转为数值型
  如:recordNum=[‘1’, ‘2’],将recordNum中数据转为int类型并存进列表
Num = list(map(int, recordNum))

五、Python 操作 MySQL 数据库
Python操作MySQL数据库官方文档

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值