于贵洋的博客

BI、数据分析


  • 首页

  • 分类

  • 标签

  • 归档

  • 站点地图

  • 公益404

  • 关于

  • 搜索

matplotlib手册(5) - 柱状图

发表于 2017-08-11 | 分类于 数据可视化-Python&R

matplotlib手册(5)

这里主要整理下matplotlib中绘制柱状图的方法,参考了几篇文章:
matplotlib绘图——柱状图
官网介绍
感谢上面的分享,博客写的很好很全,帮助很大,下面我们开始。
我们要绘制柱形图,要使用pyplot的bar方法

1
2
3
4
5
6
7
8
matplotlib.pyplot.bar(left, height, width=0.8, bottom=None, hold=None, data=None, **kwargs)
Make a bar plot.
Make a bar plot with rectangles bounded by:
left, left + width, bottom, bottom + height
(left, right, bottom and top edges)

做了一些练习之后,发现通过left、height、width、bottom这几个参数可以实现很多不同的图表,后面我们会说到。
因为是柱形图,所以四个点的坐标很重要,我们初始化,必须指定的是left和height,width默认是0.8,bottom默认是None,就是0
下面,开始我们的小例子

阅读全文 »

numpy手册(5)-random模块

发表于 2017-08-10 | 分类于 Python-Numpy

Python
Numpy知识总结


numpy的random模块应该很常用,这里整理一下,
参考文章:
http://www.mamicode.com/info-detail-507676.html
https://docs.scipy.org/doc/numpy/reference/routines.random.html

简单随机数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
numpy.random.rand(d0, d1, ..., dn)
Random values in a given shape.
Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1)
生成给定形状的随机值,随机值在[0,1)
import numpy as np
np.random.rand(10)
Out[2]:
array([ 0.41103285, 0.19043225, 0.30385602, 0.19330136, 0.09727556,
0.96518049, 0.29930132, 0.00633969, 0.64269577, 0.79953589])
np.random.rand(2,3)
Out[3]:
array([[ 0.86213038, 0.56657202, 0.83083843],
[ 0.48660386, 0.20508572, 0.4927877 ]])
np.random.rand(2,3,1)
Out[4]:
array([[[ 0.06676746],
[ 0.55548283],
[ 0.04411342]],
[[ 0.18659571],
[ 0.02209355],
[ 0.83529269]]])
阅读全文 »

matplotlib手册(4)-中文乱码

发表于 2017-08-10 | 分类于 数据可视化-Python&R

matplotlib手册(4)

前面的练习中,我们基本上没有使用中文,但是在实际使用时,中文肯定是需要的,刚刚试了下,发现会有乱码的,就找了找,这里记录下。

1.问题描述

1
2
3
4
5
6
7
8
9
10
11
import matplotlib.pyplot as plt
plt.figure(u'中文')
plt.title(u'今天')
plt.xlabel(u'明天')
plt.ylabel(u'昨天')
plt.show()

通过上面的例子,我们可以看到,除了figure的title没有问题,其他的title、label中文都显示为乱码

阅读全文 »

Python基础(8)- 关键字yield

发表于 2017-08-10 | 分类于 Python-基础

前几天遇到了这个yield,不知道是干嘛的,这里学习整理下,主要参考了:
如何理解Python关键字yield
Python高级特性

上面介绍的都很好,这里就根据自己的理解,简单整理下。

#1. 什么是迭代

常见的list、tuple等集合,我们会通过遍历,比如for循环来获取每一个元素,这就是迭代。这些可以遍历的对象,也叫做可迭代对象

小例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
a = [1,2,3]
print(a)
for i in a:
print(i)
b = 'abcd'
print(b)
for i in b:
print(i)
c = {'name':'lufei','age':20}
print(c)
for k in c:
print(k)
for v in c.values():
print(v)
for k,v in c.items():
print(k,v)
#out
[1, 2, 3]
1
2
3
abcd
a
b
c
d
{'name': 'lufei', 'age': 20}
name
age
lufei
20
name lufei
age 20

我们怎样判断一个对象是否可以去迭代呢?可以使用collections模块的Iterable

1
2
3
4
5
6
7
8
9
10
print(type(a),isinstance(a,Iterable))
print(type(b),isinstance(b,Iterable))
print(type(c),isinstance(c,Iterable))
print(type(123),isinstance(123,Iterable))
#out
<class 'list'> True
<class 'str'> True
<class 'dict'> True
<class 'int'> False

2. 列表生成式(List Comprehensions)

一个非常简单的方式来生成list,像这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
range(10)
Out[56]: range(0, 10)
list(range(10))
Out[57]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list(x for x in range(10))
Out[58]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list(x*x for x in range(10))
Out[59]: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[x*x for x in range(3)]
Out[60]: [0, 1, 4]
#上面的for后面还可以加上if判断
[x for x in range(10) if x>5]
Out[61]: [6, 7, 8, 9]
#for循环也可以嵌套
[x+y for x in 'abc' for y in 'xyz']
Out[62]: ['ax', 'ay', 'az', 'bx', 'by', 'bz', 'cx', 'cy', 'cz']

3.迭代器

前面,我们说了for循环和可迭代对象,像这种可以使用for循环不断取出先一个元素的对象,就叫做迭代器(Iterator)。迭代器不单单可以使用for循环来遍历,
还可以使用next()函数不断获取下一个元素,当没有下一个元素时,会抛出StopIteration异常。
我们可以使用collections模块的Iterator来判断一个对象是否为迭代器

1
2
3
4
5
6
7
8
9
10
from collections import Iterator
isinstance([1,2,3],Iterator)
Out[2]: False
isinstance('abc',Iterator)
Out[3]: False
isinstance({'name':'lufei','age':20},Iterator)
Out[4]: False

创建一个迭代器有3中方式:

  • 为对象创建 iter()和next()方法
  • 内置的iter()可以将可迭代对象转换为迭代器
  • 生成器
1
2
3
4
5
6
7
a = [1,2,3]
type(iter(a))
Out[6]: list_iterator
isinstance(iter(a),Iterator)
Out[7]: True

4. 生成器

上面,我们使用list()或者[],很简单方便的生成了一个列表,只要我们将[]替换为(),就创建一个一个generator。生成器可以一边循环,一边计算生成下一个元素,而不是像list一样,一下生成所有的数据。

1
2
3
4
5
6
7
8
9
10
(x+y for x in 'abc' for y in 'xyz')
Out[63]: <generator object <genexpr> at 0x0000021AE0FC9D58>
a = (x+y for x in 'abc' for y in 'xyz')
a
Out[66]: <generator object <genexpr> at 0x0000021AE0FC9518>
isinstance(a,Iterator)
Out[67]: True

通常,我们使用yield语句可以返回一个生成器,很多例子,这里都是举一个斐波那契数列
yield类似return,只不过他返回的是生成器,调用了next()之后,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def fab(n):
a,b,i = 0,1,0
while i<n:
print(b)
a,b = b,a+b
i+=1
fab(5)
#out
1
1
2
3
5
#使用yield改造
def fab(n):
a,b,i = 0,1,0
while i<n:
yield b
a,b = b,a+b
i+=1
for i in fab(5):
print(i)

我们调用fab的时候,执行到yield,会返回一个生成器,当调用next()后,程序会回到yield停止的地方继续往下执行
这样,就是每次只生成当前元素,而不是一下子生成所有的元素;
当然,for循环替我们调用了next(),并处理了StopIteration异常。
好了,梳理好上面这些概念,到yield这里,其实还好,平时在理解下,多用用,好,就到这。

matplotlib手册(3)-pyplot文本相关函数使用

发表于 2017-08-10 | 分类于 数据可视化-Python&R

matplotlib手册(3)

昨天学习了figure、subplot,最后还说了些常用的方法。
这里先简单总结下文本相关的函数使用,算是昨天最后一个部分的补充。

1.pyplot.plot

我们先来看下,这个plot函数,之前呢,绘图的时候,一直都是使用这个函数, 貌似没说这个函数到底干啥的。

1
2
3
matplotlib.pyplot.plot(*args, **kwargs)
Plot lines and/or markers to the Axes. args is a variable length argument, allowing for multiple x, y pairs with an optional format string. For example, each of the following is legal:

就是说,这个plot函数呢,可以在axes上绘制线条或者标记;
我们呢,可以直接使用pyplot.plot函数去在当前的Axes上绘图(我们之前应该主要使用这种方法),
也可以获取当前Axes,然后通过Axes去绘图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import matplotlib.pyplot as plt
#手动创建一个figure,标题为"one"
f1 = plt.figure('one')
#获取当前axes
current_axes = plt.gca()
#使用axes绘图
current_axes.plot([1,2,3],[3,2,1],color='red',label='lable1',linewidth=3)
current_axes.plot([1,2,3],[1,2,3],color='green',label='label2',linewidth=3)
print(type(current_axes))
plt.show()

阅读全文 »
1…121314…23
于贵洋

于贵洋

111 日志
17 分类
30 标签
RSS
GitHub
友情链接
  • 很久之前的CSDN博客
0%
© 2017 于贵洋
由 Hexo 强力驱动
|
主题 — NexT.Pisces v5.1.3
Hosted by GitHub Pages
本站访客数 人次 本站总访问量 次