python使用matplotlib画柱状图、散点图
作者:zhuiqiuk 时间:2023-05-18 11:50:06
本文实例为大家分享了python使用matplotlib画柱状图、散点图的具体代码,供大家参考,具体内容如下
柱状图(plt.bar)
代码与注释
import numpy as np
from matplotlib import pyplot as plt
plt.figure(figsize=(9,6))
n = 8
X = np.arange(n)+1
#X是1,2,3,4,5,6,7,8,柱的个数
# numpy.random.uniform(low=0.0, high=1.0, size=None), normal
#uniform均匀分布的随机数,normal是正态分布的随机数,0.5-1均匀分布的数,一共有n个
Y1 = np.random.uniform(0.5,1.0,n)
Y2 = np.random.uniform(0.5,1.0,n)
plt.bar(X,Y1,width = 0.35,facecolor = 'lightskyblue',edgecolor = 'white')
#width:柱的宽度
plt.bar(X+0.35,Y2,width = 0.35,facecolor = 'yellowgreen',edgecolor = 'white')
#水平柱状图plt.barh,属性中宽度width变成了高度height
#打两组数据时用+
#facecolor柱状图里填充的颜色
#edgecolor是边框的颜色
#想把一组数据打到下边,在数据前使用负号
#plt.bar(X, -Y2, width=width, facecolor='#ff9999', edgecolor='white')
#给图加text
for x,y in zip(X,Y1):
plt.text(x+0.3, y+0.05, '%.2f' % y, ha='center', va= 'bottom')
for x,y in zip(X,Y2):
plt.text(x+0.6, y+0.05, '%.2f' % y, ha='center', va= 'bottom')
plt.ylim(0,+1.25)
plt.show()
结果
散点图(plt.scatter)
代码与注释
plt.figure(figsize=(9,6))
n=1000
#rand 均匀分布和 randn高斯分布
x=np.random.randn(1,n)
y=np.random.randn(1,n)
T=np.arctan2(x,y)
plt.scatter(x,y,c=T,s=25,alpha=0.4,marker='o')
#T:散点的颜色
#s:散点的大小
#alpha:是透明程度
plt.show()
结果
来源:https://blog.csdn.net/zhuiqiuk/article/details/70943535
标签:python,matplotlib,柱状图,散点图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
javascript面向对象编程(二)
2008-03-07 12:59:00
Dreamweaver MX技巧之超链接
2009-05-29 18:40:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113349643s.jpg)
python pandas loc 布尔索引示例说明
2021-02-28 22:43:19
![](https://img.aspxhome.com/file/2023/9/102979_0s.png)
CODEPAGE=936是什么意思?
2009-07-05 18:37:00
Python asyncore socket客户端开发基本使用教程
2021-01-25 11:06:39
![](https://img.aspxhome.com/file/2023/6/90836_0s.png)
在IPython中执行Python程序文件的示例
2023-02-20 09:06:14
Python实现字典排序、按照list中字典的某个key排序的方法示例
2022-06-10 15:21:38
在Django中自定义filter并在template中的使用详解
2021-01-15 19:17:45
![](https://img.aspxhome.com/file/2023/7/114647_0s.jpg)
SQL 将一列拆分成多列的三种方法
2024-01-23 15:30:18
![](https://img.aspxhome.com/file/2023/4/98004_0s.jpg)
Python爬虫实战之爬取携程评论
2022-02-03 14:26:23
![](https://img.aspxhome.com/file/2023/5/91835_0s.png)
python小例子-缩进式编码+算术运算符+定义与赋值
2022-09-15 06:03:45
![](https://img.aspxhome.com/file/2023/2/103592_0s.png)
PHP global全局变量经典应用与注意事项分析【附$GLOBALS用法对比】 <font color=red>原创</font>
2023-11-16 07:37:55
Python 爬虫爬取指定博客的所有文章
2021-09-10 04:44:51
python 实现让字典的value 成为列表
2022-01-16 00:26:30
Django实现翻页的示例代码
2023-01-10 23:10:20
![](https://img.aspxhome.com/file/2023/2/109402_0s.png)
详解Python中range()与xrange()的区别
2021-09-06 12:32:14
MySQL中的多表联合查询功能操作
2024-01-21 07:21:30
![](https://img.aspxhome.com/file/2023/9/68049_0s.jpg)
python实现的一个火车票转让信息采集器
2023-09-05 11:42:53
python3 flask实现文件上传功能
2022-11-19 18:42:10
基于Python实现打哈欠检测详解
2021-12-10 06:50:45
![](https://img.aspxhome.com/file/2023/6/99596_0s.jpg)