python数据可视化

散点图

 

折线图

普通折线图

 

x = np.linspace(-10, 10, 10)
y = x ** 2
plt.plot(x, y, color='g',linestyle='-', marker='o', alpha=0.5)
plt.show()

时间折线图

通过strpdate2num将文件中格式为字符串'3月3日2011年'的列转换为浮点数。通过plot_date将浮点数转换为日期格式并画出

 

x, y, z = np.loadtxt('001.csv', delimiter=',', converters={0:mdates.strpdate2num('%m/%d/%Y')}, skiprows=1, usecols=(0, 1, 3), unpack=True)
plt.plot_date(x, y, '-', c='g')
plt.plot_date(x, z, '-', c='g')
plt.show()

条形图

 

 

x = np.arange(5)
y = [2, 4, 5, 3, 2]
plt.bar(left=x, height=y)
plt.show()

 

x = np.arange(5)
y = [2, 4, 5, 3, 2]
z = [1, 2, 3, 4, 5]
plt.bar(x, y, width=0.3)
plt.bar(x+0.3, z, width=0.3)
plt.show()

 

x = np.arange(5)
y = [2, 4, 5, 3, 2]
z = [1, 2, 3, 4, 5]
plt.bar(x, y, 0.3)
plt.bar(x, z, 0.3, bottom=y)
plt.show()

 

 

x = np.arange(5)
y = [2, 4, 5, 3, 2]
plt.barh(x, y, 0.3)
plt.show()

直方图

 

x = 3 + 2 * np.random.randn(1000)
plt.hist(x, 20, normed=True)
plt.show()

 

x = 3 + 2 * np.random.randn(1000)
y = 5 + 3 * np.random.randn(1000)
plt.hist2d(x, y, 40)
plt.show()

饼状图

 

x = ('a', 'b', 'c', 'd')
y = [10, 20, 30, 50]
plt.pie(x=y, labels=x, explode=[0, 0, 0.2, 0], colors=None, autopct='%.f%%',
        pctdistance=0.6, shadow=True, labeldistance=1.1, startangle=None,
        radius=None, counterclock=True, wedgeprops=None, textprops=None,
        center=(0, 0), frame=False, rotatelabels=False, hold=None, data=None)
plt.show()

箱形图

包含上边界,上四分位,中位,下四分位,下边界。whis控制上下边界,边界外为异常值

 

x = np.random.normal(size=(1000, 4), loc=0, scale=1)
y = ['a', 'b', 'c', 'd']
plt.boxplot(x, labels=y, whis=1.5)
plt.show()

颜色

 color= 

1、颜色缩写b:blue    g:green    r:red    c:cyan    m:magenta    y:yellow    l:black    w:white

2、灰色阴影  '0.2'

3、十六进制  '#FF00FF'

4、RGB元组  (0.1, 0.2, 0.3)

点形状

'o' 只改变点形状

marker='o' 点间有连线

    '.'    'o'    '<'    '*'    

线形状

'-' 实线    '--'虚线    '-.'点划线    ':'点线

样式字符串

“颜色点形线形”组成的字符串:'ro--'      

 

三种编程方式

1、pyplot:经典高层封装

2、pylab:将matplotlib 与 numpy 合并的模块,模拟matlab编程环境

3、面向对象方式:matplotlib精髓,更基础和底层,定制能力强

实际工作推荐1、3方法结合使用

 

面向对象方式

 

先创建画布对象,创建坐标系对象,再画图。(111)表示坐标系在画布中(总行数,总列数,位置) 

x = np.arange(1, 5, 1)
y = np.arange(3, 7, 1)
fig = plt.figure()
ax = fig.add_subplot(111)
l, = plt.plot(x, y)
plt.show()

简化画法

 

x = np.arange(1, 5, 1)
y = np.arange(3, 7, 1)
plt.subplot(121)
plt.plot(x, y)
plt.subplot(122)
plt.plot(-x, y)
plt.show()

网格

 

fig = plt.figure()
ax = fig.add_subplot(121)
bx = fig.add_subplot(122)
ax.plot(x, y)
ax.grid(color='k')
plt.show()

 

plt.subplot(121)
plt.plot(x, y)
plt.grid(color='k')
plt.subplot(122)
plt.show()

标签

 

plt.subplot(121)
plt.plot(x, y)
plt.legend('aaa', loc=0)
plt.show()

改变坐标轴范围

整体调整:axis()    

只改变x、y轴:xlim()    ylim()

 

plt.plot(x, y)
plt.axis([0, 4, 0, 4])
plt.show()

改变坐标轴密度

 

plt.plot(x, y)
plt.locator_params('x', nbins=9)
plt.show()

时间序列

 

x = datetime.datetime(2017, 1, 1)
y = datetime.datetime(2018, 1, 1)
z = datetime.timedelta(days=1)
dates = mpl.dates.drange(x, y, z)
l = np.random.randn(len(dates))
plt.plot_date(dates, l)
plt.show()

左右Y轴

 

x = np.arange(1, 5, 1)
y1 = np.arange(3, 7, 1)
y2 = [33, 44, 21, 19]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(x, y1)
ax2 = ax1.twinx()
ax1.set_ylabel('Y1')
ax2.set_ylabel('Y2')
ax1.set_xlabel('Xzhou')
ax2.plot(x, y2)
plt.show()

或者

 

x = np.arange(1, 5, 1)
y1 = np.arange(3, 7, 1)
y2 = [33, 44, 21, 19]
plt.plot(x, y1)
plt.xlabel('Xzhou')
plt.ylabel('Y1')
plt.twinx()
plt.plot(x, y2)
plt.ylabel('Y2')
plt.show()

带箭头注释

 

plt.plot([2, 3, 4, 5], [1, 3, 4, 3])
plt.annotate('zhushi', xy=(3.5, 3.5), xytext=(3.5, 2.8), arrowprops=dict(facecolor='r', headwidth=4, width=2))
plt.show()

文字注释

 

plt.plot([2, 3, 4, 5], [1, 3, 4, 3])
plt.text(3.5, 2.5, 'zhushi', family='fantasy', color='r', size=20, style='oblique')
plt.show()

公式

 

plt.locator_params([0, 10, 0, 10])
plt.text(0.2, 0.8, r'$ \alpha_i \beta_j \pi \lambda $', size=50)
plt.text(0.2, 0.4, r'$ \sin(0)=\cos(\frac{\pi}{2}) $', size=30)
plt.show()

区域填充颜色

 

x = np.linspace(0, 5*np.pi, 1000)
y1 = np.sin(x)
y2 = np.sin(2*x)
plt.fill(x, y1, color='r', alpha=0.3)
plt.fill(x, y2, color='b', alpha=0.3)
plt.show()

几何图形

 

fig = plt.figure()
a = fig.add_subplot(111)
c = patches.Circle([0.1, 0.1], 0.1)
d = patches.Rectangle([0.3, 0.1], 0.1, 0.1)
e = patches.RegularPolygon([0.5, 0.1], 5, 0.1)
a.add_patch(c)
a.add_patch(d)
a.add_patch(e)
plt.axis('equal')
plt.show()

美化

应用样式: plt.style.use('ggplot')

极坐标

 

plt.subplot(projection='polar')
plt.plot([0, np.pi*0.5, np.pi], [4, 5, 4], c='r')
plt.show()

pandas绘图函数

 

y = pd.DataFrame({'a': np.arange(10), 'b': np.arange(3, 13), 'c': np.arange(10, 20)})
y.plot(kind='kde', alpha=0.8)
plt.show()

kind可选line, bar, barh, kde

\

 

y = pd.DataFrame({'a': np.random.randn(100), 'b': np.random.randn(100), 'c': np.random.randn(100)})
y.hist(bins=50, alpha=0.8)
plt.show()

直方图和密度图

a = pd.Series( ......)

a.hist(bins=50)

a.hist(bins=50, normed=True)

散点图矩阵

 

a = pd.read_csv('/home/dlin/下载/pydata-book-1st-edition/ch08/macrodata.csv')
b = a[['cpi', 'm1', 'tbilrate', 'unemp']]
d = np.log(b).diff().dropna()
pd.scatter_matrix(d, diagonal='kde', alpha=0.3)
plt.show()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值