matplotlib(三)散点图和条形图

散点图

简单示例

from matplotlib import pyplot as plt
plt.rc('font', family='Microsoft YaHei', size=18) 
y_3 = [18,16,19,20,21,16,19,22,24,23,18,23,25,22,23,25,26,28,27,24,19,26,24,29,30,31,21,28,33,32,34]
y_10 = [34,33,32,34,31,29,25,28,26,29,24,25,23,25,24,23,22,19,23,22,16,26,25,20,16,18,19,16,18,16,16]

x = range(1,32)
plt.scatter(x,y_3)

plt.show()

简单示例

a.绘制散点图

plt.scatter(x,y_3)

b.x轴y轴刻度值,描述信息,图例

from matplotlib import pyplot as plt
plt.rc('font', family='Microsoft YaHei', size=18) 
plt.figure(figsize=(20,8),dpi=80)

x_3 = range(1,32)
x_10 = range(51,82)

x_3_10 = list(x_3) + list(x_10)
x_string = ['3月{}号'.format(i) for i in range(1,32) ]
x_string += ['10月{}号'.format(i) for i in range(1,32)]

plt.xticks(list(x_3_10)[::3],x_string[::3],rotation=45)

plt.scatter(x_3,y_3,label='3月份')
plt.scatter(x_10,y_10,label='10月份')

#描述信息
plt.xlabel('时间')
plt.ylabel('气温 单位(℃)')
plt.title('3月和10月气温变化图')

#添加图例
plt.legend(loc='upper left')

plt.show()

复杂示例

条形图

离散数据:个数据之间没什么关系 (ps:直方图条与条之间无间隔,而条形图有。)

简单示例

x = ['A','B','C','D','E','F','G']
y = [21,22,13,2,5,34,23]

plt.figure(figsize=(20,8),dpi=80)

#绘制条形图
plt.bar(range(len(x)),y,width = 0.3)
 
x_string = ['{}'.format(i) for i in x]
plt.xticks(range(len(x)),x_string)

plt.xlabel('姓名')
plt.ylabel('收入 单位(k元)')
plt.title('个人收入表')

plt.show()

简单示例

a.绘制条形图

plt.bar(range(len(x)),y,width = 0.3)

width : 柱状图的宽度
color:选择柱状图的颜色

b.绘制横向条形图

x = ['A','B','C','D','E','F','G']
y = [21,22,13,2,5,34,23]

plt.figure(figsize=(20,8),dpi=80)

#绘制横着的条形图
plt.barh(range(len(x)),y,height = 0.3,color = 'orange')
 
x_string = ['{}'.format(i) for i in x]
plt.yticks(range(len(x)),x_string)

plt.ylabel('姓名')
plt.xlabel('收入 单位(k元)')
plt.title('个人收入表')

plt.grid(alpha = 0.4)
plt.show()

横向条形图

#不同电影三天的对比图
x = ['天降之物','喜洋洋与灰太狼','鬼灭之刃']
y_1 = [14,4,15]
y_2 = [15,2,17]
y_3 = [10,1,14]

plt.figure(figsize=(20,8),dpi=80)

bar_height = 0.2

_y_1 = range(len(x))
_y_2 = [i + bar_height for i in _y_1]
_y_3 = [i + bar_height*2 for i in _y_1]

#绘制横着的条形图
plt.barh(_y_1,y_1,height = bar_height,label='15号')
plt.barh(_y_2,y_2,height = bar_height,label='16号')
plt.barh(_y_3,y_3,height = bar_height,label='17号')

#y轴刻度显示
plt.yticks(_y_2,x)

plt.ylabel('电影名')
plt.xlabel('票房 单位(万元)') 
plt.title('三日内电影票房对比表')

#图例
plt.legend(loc='upper right')

#网格
plt.grid(alpha = 0.4)
plt.show()

对比条形图

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值