关于matplotlib学习还是强烈建议常去官方http://matplotlib.org/contents.html里查一查各种用法和toturial等。 
下面是jupyter notebook代码导出的md文件。

Plotting and Visualization

  1. from __future__ import division
  2. from numpy.random import randn
  3. import numpy as np
  4. import os
  5. import matplotlib.pyplot as plt
  6. np.random.seed(12345)
  7. plt.rc('figure', figsize=(10, 6))
  8. from pandas import Series, DataFrame
  9. import pandas as pd
  10. np.set_printoptions(precision=4)
  1. %matplotlib inline
  • 1

matplotlib API 介绍

  1. import matplotlib.pyplot as plt
  • 1

Figures and Subplots

  1. fig = plt.figure()
  • 1
  1. ax1 = fig.add_subplot(2, 2, 1)
  • 1
  1. ax2 = fig.add_subplot(2, 2, 2)
  2. ax3 = fig.add_subplot(2, 2, 3)
  1. from numpy.random import randn
  2. plt.plot(randn(50).cumsum(), 'k--')
  1. [<matplotlib.lines.Line2D at 0x28e7668cb38>]

  1. _ = ax1.hist(randn(100), bins=20, color='k', alpha=0.3)
  2. ax2.scatter(np.arange(30), np.arange(30) + 3 * randn(30))
  1. plt.close('all')
  • 1
  1. fig, axes = plt.subplots(2, 3)
  2. axes
  1. array([[<matplotlib.axes._subplots.AxesSubplot object at 0x0000028E76BAFF98>,
  2. <matplotlib.axes._subplots.AxesSubplot object at 0x0000028E76C047F0>,
  3. <matplotlib.axes._subplots.AxesSubplot object at 0x0000028E76C4CB00>],
  4. [<matplotlib.axes._subplots.AxesSubplot object at 0x0000028E76C89D30>,
  5. <matplotlib.axes._subplots.AxesSubplot object at 0x0000028E76CD7940>,
  6. <matplotlib.axes._subplots.AxesSubplot object at 0x0000028E76D0FFD0>]], dtype=object)

## 调整subplot间距

  1. plt.subplots_adjust(left=None, bottom=None, right=None, top=None,
  2. wspace=None, hspace=None)
  1. fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)
  2. for i in range(2):
  3. for j in range(2):
  4. axes[i, j].hist(randn(500), bins=50, color='k', alpha=0.5)
  5. plt.subplots_adjust(wspace=0, hspace=0)

  1. fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)
  2. for i in range(2):
  3. for j in range(2):
  4. axes[i, j].hist(randn(500), bins=50, color='k', alpha=0.5)
  5. plt.subplots_adjust(wspace=0, hspace=0)

### 线条格式

  1. plt.figure()
  • 1
  1. plt.plot(randn(30).cumsum(), 'ko--')
  • 1
  1. [<matplotlib.lines.Line2D at 0x28e7866a390>]
  • 1
  • 2

  1. plt.close('all')
  • 1
  1. data = randn(30).cumsum()
  2. plt.plot(data, 'k--', label='Default')
  3. plt.plot(data, 'k-', drawstyle='steps-post', label='steps')
  4. plt.legend(loc='best')
  1. <matplotlib.legend.Legend at 0x28e781103c8>

### Ticks, labels, and legends #### Setting the title, axis labels, ticks, and ticklabels

  1. fig = plt.figure(); ax = fig.add_subplot(1, 1, 1)
  2. ax.plot(randn(1000).cumsum())
  3. ticks = ax.set_xticks([0, 250, 500, 750, 1000])
  4. labels = ax.set_xticklabels(['one', 'two', 'three', 'four', 'five'],
  5. rotation=30, fontsize='small')
  6. ax.set_title('some random lines')
  7. ax.set_xlabel('Stages')
  1. <matplotlib.text.Text at 0x28e782525c0>
  • 1
  • 2

#### Adding legends

  1. fig = plt.figure(); ax = fig.add_subplot(1, 1, 1)
  2. ax.plot(randn(1000).cumsum(), 'k', label='one')
  3. ax.plot(randn(1000).cumsum(), 'k--', label='two')
  4. ax.plot(randn(1000).cumsum(), 'k.', label='three')
  5. ax.legend(loc='best')
  1. <matplotlib.legend.Legend at 0x28e7801e668>
  • 1
  • 2

### subplot 做标记

  1. from datetime import datetime
  2. fig = plt.figure()
  3. ax = fig.add_subplot(1, 1, 1)
  4. data = pd.read_csv('julyedu/spx.csv', index_col=0, parse_dates=True)
  5. spx = data['SPX']
  6. spx.plot(ax=ax, style='k-')
  7. crisis_data = [
  8. (datetime(2007, 10, 11), 'Peak of bull market'),
  9. (datetime(2008, 3, 12), 'Bear Stearns Fails'),
  10. (datetime(2008, 9, 15), 'Lehman Bankruptcy')
  11. ]
  12. for date, label in crisis_data:
  13. ax.annotate(label, xy=(date, spx.asof(date) + 50),
  14. xytext=(date, spx.asof(date) + 200),
  15. arrowprops=dict(facecolor='black'),
  16. horizontalalignment='left', verticalalignment='top')
  17. # Zoom in on 2007-2010
  18. ax.set_xlim(['1/1/2007', '1/1/2011'])
  19. ax.set_ylim([600, 1800])
  20. ax.set_title('Important dates in 2008-2009 financial crisis')
  1. <matplotlib.text.Text at 0x28e77fb7358>

  1. fig = plt.figure()
  2. ax = fig.add_subplot(1, 1, 1)
  3. rect = plt.Rectangle((0.2, 0.75), 0.4, 0.15, color='k', alpha=0.3)
  4. circ = plt.Circle((0.7, 0.2), 0.15, color='b', alpha=0.3)
  5. pgon = plt.Polygon([[0.15, 0.15], [0.35, 0.4], [0.2, 0.6]],
  6. color='g', alpha=0.5)
  7. ax.add_patch(rect)
  8. ax.add_patch(circ)
  9. ax.add_patch(pgon)
  1. <matplotlib.patches.Polygon at 0x28e77ed76a0>
  • 1
  • 2

### Saving plots to file

  1. fig
  • 1

  1. fig.savefig('figpath.svg')
  • 1
  1. fig.savefig('figpath.png', dpi=400, bbox_inches='tight')
  • 1
  1. from io import BytesIO
  2. buffer = BytesIO()
  3. plt.savefig(buffer)
  4. plot_data = buffer.getvalue()

### matplotlib configuration

  1. plt.rc('figure', figsize=(10, 10))
  • 1

## Plotting functions in pandas ### Line plots

  1. plt.close('all')
  • 1
  1. s = Series(np.random.randn(10).cumsum(), index=np.arange(0, 100, 10))
  2. s.plot()
  1. <matplotlib.axes._subplots.AxesSubplot at 0x28e781c0208>

  1. df = DataFrame(np.random.randn(10, 4).cumsum(0),
  2. columns=['A', 'B', 'C', 'D'],
  3. index=np.arange(0, 100, 10))
  4. df.plot()
  1. <matplotlib.axes._subplots.AxesSubplot at 0x28e7809d358>

### Bar plots

  1. fig, axes = plt.subplots(2, 1)
  2. data = Series(np.random.rand(16), index=list('abcdefghijklmnop'))
  3. data.plot(kind='bar', ax=axes[0], color='k', alpha=0.7)
  4. data.plot(kind='barh', ax=axes[1], color='k', alpha=0.7)
  1. <matplotlib.axes._subplots.AxesSubplot at 0x11fd02b50>

  1. df = DataFrame(np.random.rand(6, 4),
  2. index=['one', 'two', 'three', 'four', 'five', 'six'],
  3. columns=pd.Index(['A', 'B', 'C', 'D'], name='Genus'))
  4. df
  5. df.plot(kind='bar')
  1. <matplotlib.axes._subplots.AxesSubplot at 0x28e77f482e8>
  • 1
  • 2

  1. plt.figure()
  • 1
  1. df.plot(kind='barh', stacked=True, alpha=0.5)
  • 1
  1. <matplotlib.axes._subplots.AxesSubplot at 0x28e77e05be0>

png

  1. tips = pd.read_csv('julyedu/tips.csv')
  2. party_counts = pd.crosstab(tips.day, tips.size)
  3. print(party_counts)
  4. # Not many 1- and 6-person parties
  5. party_counts = party_counts.ix[:, 2:5]
  6. print(party_counts)

col_0 1708 day Fri 19 Sat 87 Sun 76 Thur 62 Empty DataFrame Columns: [] Index: [Fri, Sat, Sun, Thur] ### Histograms and density plots

  1. plt.figure()
  • 1
  1. tips['tip_pct'] = tips['tip'] / tips['total_bill']
  2. print(tips.head())
  3. tips['tip_pct'].hist(bins=50)
  1. total_bill tip sex smoker day time size tip_pct
  2. 0 16.99 1.01 Female No Sun Dinner 2 0.059447
  3. 1 10.34 1.66 Male No Sun Dinner 3 0.160542
  4. 2 21.01 3.50 Male No Sun Dinner 3 0.166587
  5. 3 23.68 3.31 Male No Sun Dinner 2 0.139780
  6. 4 24.59 3.61 Female No Sun Dinner 4 0.146808
  7. <matplotlib.axes._subplots.AxesSubplot at 0x28e7997b390>

png

  1. plt.figure()
  • 1
  1. tips['tip_pct'].plot(kind='kde')
  • 1
  1. plt.figure()
  • 1
  1. comp1 = np.random.normal(0, 1, size=200) # N(0, 1)
  2. comp2 = np.random.normal(10, 2, size=200) # N(10, 4)
  3. values = Series(np.concatenate([comp1, comp2]))
  4. values.hist(bins=100, alpha=0.3, color='k', normed=True)
  5. values.plot(kind='kde', style='k--')
  1. <matplotlib.axes._subplots.AxesSubplot at 0x28e79b24358>
  • 1
  • 2

### Scatter plots

  1. macro = pd.read_csv('julyedu/macrodata.csv')
  2. data = macro[['cpi', 'm1', 'tbilrate', 'unemp']]
  3. trans_data = np.log(data).diff().dropna()
  4. trans_data[-5:]
  cpi m1 tbilrate unemp
198 -0.007904 0.045361 -0.396881 0.105361
199 -0.021979 0.066753 -2.277267 0.139762
200 0.002340 0.010286 0.606136 0.160343
201 0.008419 0.037461 -0.200671 0.127339
202 0.008894 0.012202 -0.405465 0.042560
  1. plt.figure()
  • 1
  1. plt.scatter(trans_data['m1'], trans_data['unemp'])
  2. plt.title('Changes in log %s vs. log %s' % ('m1', 'unemp'))
  • 1
  • 2
  1. <matplotlib.text.Text at 0x28e7bfebcc0>
  • 1
  • 2

  1. pd.scatter_matrix(trans_data, diagonal='kde', alpha=0.3)
  • 1
  1. array([[<matplotlib.axes._subplots.AxesSubplot object at 0x0000028E7CA07EF0>,
  2. <matplotlib.axes._subplots.AxesSubplot object at 0x0000028E7C6E9128>,
  3. <matplotlib.axes._subplots.AxesSubplot object at 0x0000028E7DFEEBA8>,
  4. <matplotlib.axes._subplots.AxesSubplot object at 0x0000028E7C3DB3C8>],
  5. [<matplotlib.axes._subplots.AxesSubplot object at 0x0000028E7C9E5EB8>,
  6. <matplotlib.axes._subplots.AxesSubplot object at 0x0000028E7C9D0E10>,
  7. <matplotlib.axes._subplots.AxesSubplot object at 0x0000028E7BFE87B8>,
  8. <matplotlib.axes._subplots.AxesSubplot object at 0x0000028E7C732FD0>],
  9. [<matplotlib.axes._subplots.AxesSubplot object at 0x0000028E7C9704E0>,
  10. <matplotlib.axes._subplots.AxesSubplot object at 0x0000028E7CF63320>,
  11. <matplotlib.axes._subplots.AxesSubplot object at 0x0000028E7C8BB748>,
  12. <matplotlib.axes._subplots.AxesSubplot object at 0x0000028E7C820978>],
  13. [<matplotlib.axes._subplots.AxesSubplot object at 0x0000028E7C6BBB00>,
  14. <matplotlib.axes._subplots.AxesSubplot object at 0x0000028E7C3405F8>,
  15. <matplotlib.axes._subplots.AxesSubplot object at 0x0000028E7C874DA0>,
  16. <matplotlib.axes._subplots.AxesSubplot object at 0x0000028E7E036550>]], dtype=object)

## Plotting Maps: Visualizing Haiti Earthquake Crisis data

  1. data = pd.read_csv('julyedu/Haiti.csv')
  2. data.info()
  • 1
  • 2
  1. data[['INCIDENT DATE', 'LATITUDE', 'LONGITUDE']][:10]
  • 1
  INCIDENT DATE LATITUDE LONGITUDE
0 05/07/2010 17:26 18.233333 -72.533333
1 28/06/2010 23:06 50.226029 5.729886
2 24/06/2010 16:21 22.278381 114.174287
3 20/06/2010 21:59 44.407062 8.933989
4 18/05/2010 16:26 18.571084 -72.334671
5 26/04/2010 13:14 18.593707 -72.310079
6 26/04/2010 14:19 18.482800 -73.638800
7 26/04/2010 14:27 18.415000 -73.195000
8 15/03/2010 10:58 18.517443 -72.236841
9 15/03/2010 11:00 18.547790 -72.410010
  1. data['CATEGORY'][:6]
  • 1

0 1. Urgences | Emergency, 3. Public Health, 1 1. Urgences | Emergency, 2. Urgences logistiqu… 2 2. Urgences logistiques | Vital Lines, 8. Autr… 3 1. Urgences | Emergency, 4 1. Urgences | Emergency, 5 5e. Communication lines down, Name: CATEGORY, dtype: object

  1. data.describe()
  • 1
  Serial LATITUDE LONGITUDE
count 3593.000000 3593.000000 3593.000000
mean 2080.277484 18.611495 -72.322680
std 1171.100360 0.738572 3.650776
min 4.000000 18.041313 -74.452757
25% 1074.000000 18.524070 -72.417500
50% 2163.000000 18.539269 -72.335000
75% 3088.000000 18.561820 -72.293570
max 4052.000000 50.226029 114.174287
  1. data = data[(data.LATITUDE > 18) & (data.LATITUDE < 20) &
  2. (data.LONGITUDE > -75) & (data.LONGITUDE < -70)
  3. & data.CATEGORY.notnull()]
  1. def to_cat_list(catstr):
  2. stripped = (x.strip() for x in catstr.split(','))
  3. return [x for x in stripped if x]
  4. def get_all_categories(cat_series):
  5. cat_sets = (set(to_cat_list(x)) for x in cat_series)
  6. return sorted(set.union(*cat_sets))
  7. def get_english(cat):
  8. code, names = cat.split('.')
  9. if '|' in names:
  10. names = names.split(' | ')[1]
  11. return code, names.strip()
  1. get_english('2. Urgences logistiques | Vital Lines')
  • 1
  1. ('2', 'Vital Lines')
  1. all_cats = get_all_categories(data.CATEGORY)
  2. # Generator expression
  3. english_mapping = dict(get_english(x) for x in all_cats)
  4. english_mapping['2a']
  5. english_mapping['6c']
  1. 'Earthquake and aftershocks'
  1. def get_code(seq):
  2. return [x.split('.')[0] for x in seq if x]
  3. all_codes = get_code(all_cats)
  4. code_index = pd.Index(np.unique(all_codes))
  5. dummy_frame = DataFrame(np.zeros((len(data), len(code_index))),
  6. index=data.index, columns=code_index)
  1. dummy_frame.ix[:, :6].info()
  • 1
  1. <class 'pandas.core.frame.DataFrame'>
  2. Int64Index: 3569 entries, 0 to 3592
  3. Data columns (total 6 columns):
  4. 1 3569 non-null float64
  5. 1a 3569 non-null float64
  6. 1b 3569 non-null float64
  7. 1c 3569 non-null float64
  8. 1d 3569 non-null float64
  9. 2 3569 non-null float64
  10. dtypes: float64(6)
  11. memory usage: 195.2 KB
  1. for row, cat in zip(data.index, data.CATEGORY):
  2. codes = get_code(to_cat_list(cat))
  3. dummy_frame.ix[row, codes] = 1
  4. data = data.join(dummy_frame.add_prefix('category_'))
  1. data.ix[:, 10:15].info()
  • 1
  1. <class 'pandas.core.frame.DataFrame'>
  2. Int64Index: 3569 entries, 0 to 3592
  3. Data columns (total 5 columns):
  4. category_1 3569 non-null float64
  5. category_1a 3569 non-null float64
  6. category_1b 3569 non-null float64
  7. category_1c 3569 non-null float64
  8. category_1d 3569 non-null float64
  9. dtypes: float64(5)
  10. memory usage: 167.3 KB
  1. from mpl_toolkits.basemap import Basemap
  2. import matplotlib.pyplot as plt
  3. def basic_haiti_map(ax=None, lllat=17.25, urlat=20.25,
  4. lllon=-75, urlon=-71):
  5. # create polar stereographic Basemap instance.
  6. m = Basemap(ax=ax, projection='stere',
  7. lon_0=(urlon + lllon) / 2,
  8. lat_0=(urlat + lllat) / 2,
  9. llcrnrlat=lllat, urcrnrlat=urlat,
  10. llcrnrlon=lllon, urcrnrlon=urlon,
  11. resolution='f')
  12. # draw coastlines, state and country boundaries, edge of map.
  13. m.drawcoastlines()
  14. m.drawstates()
  15. m.drawcountries()
  16. return m
  1. ---------------------------------------------------------------------------
  2. ImportError Traceback (most recent call last)
  3. <ipython-input-66-ec31ba3e955e> in <module>()
  4. ----> 1 from mpl_toolkits.basemap import Basemap
  5. 2 import matplotlib.pyplot as plt
  6. 3
  7. 4 def basic_haiti_map(ax=None, lllat=17.25, urlat=20.25,
  8. 5 lllon=-75, urlon=-71):
  9. ImportError: No module named 'mpl_toolkits.basemap'
  1. fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(12, 10))
  2. fig.subplots_adjust(hspace=0.05, wspace=0.05)
  3. to_plot = ['2a', '1', '3c', '7a']
  4. lllat=17.25; urlat=20.25; lllon=-75; urlon=-71
  5. for code, ax in zip(to_plot, axes.flat):
  6. m = basic_haiti_map(ax, lllat=lllat, urlat=urlat,
  7. lllon=lllon, urlon=urlon)
  8. cat_data = data[data['category_%s' % code] == 1]
  9. # compute map proj coordinates.
  10. x, y = m(cat_data.LONGITUDE.values, cat_data.LATITUDE.values)
  11. m.plot(x, y, 'k.', alpha=0.5)
  12. ax.set_title('%s: %s' % (code, english_mapping[code]))

  1. fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(12, 10))
  2. fig.subplots_adjust(hspace=0.05, wspace=0.05)
  3. to_plot = ['2a', '1', '3c', '7a']
  4. lllat=17.25; urlat=20.25; lllon=-75; urlon=-71
  5. def make_plot():
  6. for i, code in enumerate(to_plot):
  7. cat_data = data[data['category_%s' % code] == 1]
  8. lons, lats = cat_data.LONGITUDE, cat_data.LATITUDE
  9. ax = axes.flat[i]
  10. m = basic_haiti_map(ax, lllat=lllat, urlat=urlat,
  11. lllon=lllon, urlon=urlon)
  12. # compute map proj coordinates.
  13. x, y = m(lons.values, lats.values)
  14. m.plot(x, y, 'k.', alpha=0.5)
  15. ax.set_title('%s: %s' % (code, english_mapping[code]))
  1. make_plot()

matplotlib常用操作2的更多相关文章

  1. matplotlib 常用操作

    标准的Python中用列表(list)保存一组值,可以当作数组使用.但由于列表的元素可以是任何对象,因此列表中保存的是对象的指针.这样一来,为了保存一个简单的列表[1,2,3],就需 要有三个指针和三 ...

  2. matplotlib常用操作

    1.根据坐标点绘制: import numpy as np import matplotlib.pyplot as plt x = np.array([1,2,3,4,5,6,7,8]) y = np ...

  3. 二叉树的python可视化和常用操作代码

    二叉树是一个重要的数据结构, 本文基于"二叉查找树"的python可视化 pybst 包, 做了一些改造, 可以支持更一般的"二叉树"可视化. 关于二叉树和二叉 ...

  4. 【三】用Markdown写blog的常用操作

    本系列有五篇:分别是 [一]Ubuntu14.04+Jekyll+Github Pages搭建静态博客:主要是安装方面 [二]jekyll 的使用 :主要是jekyll的配置 [三]Markdown+ ...

  5. php模拟数据库常用操作效果

    test.php <?php header("Content-type:text/html;charset='utf8'"); error_reporting(E_ALL); ...

  6. Mac OS X常用操作入门指南

    前两天入手一个Macbook air,在装软件过程中摸索了一些基本操作,现就常用操作进行总结, 1关于触控板: 按下(不区分左右)            =鼠标左键 control+按下        ...

  7. mysql常用操作语句

    mysql常用操作语句 1.mysql -u root -p   2.mysql -h localhost -u root -p database_name 2.列出数据库: 1.show datab ...

  8. nodejs配置及cmd常用操作

    一.cmd常用操作 1.返回根目录cd\ 2.返回上层目录cd .. 3.查找当前目录下的所有文件dir 4.查找下层目录cd window 二.nodejs配置 Node.js安装包及源码下载地址为 ...

  9. Oracle常用操作——创建表空间、临时表空间、创建表分区、创建索引、锁表处理

    摘要:Oracle数据库的库表常用操作:创建与添加表空间.临时表空间.创建表分区.创建索引.锁表处理 1.表空间 ■  详细查看表空间使用状况,包括总大小,使用空间,使用率,剩余空间 --详细查看表空 ...

随机推荐

  1. brief 程序注释

    /** * @brief Acceleration (g's) in body frame. * Embedded MPL defines gravity as positive accelerati ...

  2. GITHUB添加SSH内容

    首先,你需要注册一个 github账号,最好取一个有意义的名字,比如姓名全拼,昵称全拼,如果被占用,可以加上有意义的数字. 本文中假设用户名为 chuaaqiCSDN(我的博客名的全拼) 一.gihu ...

  3. 2019-2020-1 20199312《Linux内核原理与分析》第七周作业

    进程的描述 操作系统内核实现操作系统的三大管理功能 进程管理(内核中最核心的功能) 内存管理 文件系统 在操作系统中,我们通过进程控制块PCB描述进程. 为了管理进程,内核必须对每个进程进行清晰的描述 ...

  4. python3文本读取与写入常用代码

    创建文件夹: import os import shutil def buildfile(echkeyfile): if os.path.exists(echkeyfile): #创建前先判断是否存在 ...

  5. Tensorflow细节-P202-数据集的高层操作

    本节是对上节的补充 import tempfile import tensorflow as tf # 输入数据使用本章第一节(1. TFRecord样例程序.ipynb)生成的训练和测试数据. tr ...

  6. CF19D Points 平衡树

    题意:支持插入/删除点 $(x,y)$,查询一个点右上方横坐标与之最接近的点坐标. 我们可以对于每一个操作过的横坐标都开一个 $set$,然后再开一个平衡树,维护每个横坐标上最大的纵坐标. 然后查询点 ...

  7. learning java AWT 剪贴板 传递文本

    import javax.swing.*; import java.awt.*; import java.awt.datatransfer.Clipboard; import java.awt.dat ...

  8. HTML插入音频和视频:audio和video标签及其属性

    一.上传到第三方网站,然后引入例如视频上传到优酷网,然后得到代码 <iframe height=498 width=510 src='http://player.youku.com/embed/ ...

  9. CSS渐变色边框,解决border设置渐变后,border-radius无效的问题

    需求:用css设置渐变边框通过border-image来实现渐变色边框 <div class="content"></div> .content { wid ...

  10. [golang]golang 汇编

    https://lrita.github.io/2017/12/12/golang-asm/#why 在某些场景下,我们需要进行一些特殊优化,因此我们可能需要用到golang汇编,golang汇编源于 ...