Python绘图:matplotlib.pyplot笔记

  • Post author:
  • Post category:python



一、柱状图


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数据库官方文档



版权声明:本文为Francis_cxt原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。