python 柱状图折线图共用一个图例_Python数据可视化–折线图–柱状图

from matplotlib

import pyplot

import random

x = list(range(0,100))

y = [random.randint(0,100) for r in range(0,100)]

fig1 = pyplot.figure()#初始化一个空白画布

pyplot.plot(x, y, ‘-‘)#生成一个折线图,X轴,Y轴,图形样式

pyplot.title(‘First Plot – Random integers’)

pyplot.xlabel(‘X Axis’)

pyplot.ylabel(‘Y Axis’)

pyplot.show()

|

生成的图片见下图:

5-1540878057.jpg

生成的随机数折线图

结合CSV文件生成图形

CSV文件如下图:

2-1540878058.png

csv 数据

该数据可以由Arduino生成,参考下文:

Python与arduino文件IO操作简介

该例子将生成两个图片,一个是折线图一个是柱状图,代码如下:

import csv

from matplotlib

import pyplot

num =

[]

btnValues =

[]

potValues =

[]

with open(‘Arduino_data.csv’, ‘r’) as f:

reader = csv.reader(f)

header = next(reader, None)#读取第一行标题

for row in reader:

num.append(int(row[0]))#序列

potValues.append(float(row[1]))#电位计数据列

btnValues.append(int(row[2]))#按钮数据列

pyplot.subplot(2, 1, 1)##三个参数的意思是:整个图表分为2行1列,该子图表位于第一行

pyplot.plot(num, potValues, ‘-‘)#生成折线图

pyplot.title(‘Line plot – ‘ + header[1])

pyplot.xlim([1, 30])

pyplot.xlabel(‘X Axis’)

pyplot.ylabel(‘Y Axis’)

pyplot.subplot(2, 1, 2)#三个参数的意思是:整个图表分为2行1列,该子图表位于第二行

pyplot.bar(num, btnValues)#生成柱状图

pyplot.title(‘Bar chart – ‘ + header[2])

pyplot.xlim([1, 30])#x轴坐标范围

pyplot.xlabel(‘X Axis’)

pyplot.ylabel(‘Y Axis’)

pyplot.tight_layout()#下面有比较

pyplot.show()

|

5-1540878058.jpg

有pyplot.tight_layout()语句

7-1540878058.jpg

无pyplot.tight_layout()语句

喜欢文章,欢迎大家转发!!!

|

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值