数据分析02

 

2.线型,线宽和颜色
mp.plot(
    ..., linestyle=线型, linewidth=线宽, color=颜色, ...)
代码:数据分析01

3.设置坐标范围
mp.xlim(水平坐标最小值,水平坐标最大值)
mp.ylim(垂直坐标最小值,垂直坐标最大值)
代码

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
x = np.linspace(-np.pi, np.pi, 1000)
cos_y = np.cos(x) / 2
sin_y = np.sin(x)
mp.xlim(x.min() * 1.2, x.max() * 1.2)
mp.ylim(sin_y.min() * 1.2, sin_y.max() * 1.2)
mp.plot(x, cos_y, linestyle='--', linewidth=1,
        color='red')
mp.plot(x, sin_y, linestyle=':', linewidth=3,
        color='green')
mp.show()

4.设置坐标刻度
mp.xticks(位置序列,标签序列)
mp.yticks(位置序列,标签序列)
代码:
 

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
x = np.linspace(-np.pi, np.pi, 1000)
cos_y = np.cos(x) / 2
sin_y = np.sin(x)
mp.xlim(x.min() * 1.2, x.max() * 1.2)
mp.ylim(sin_y.min() * 1.2, sin_y.max() * 1.2)
mp.xticks([-np.pi, -np.pi / 2, 0,
           np.pi / 2, np.pi * 3 / 4, np.pi],
          [r'$-\pi$', r'$-\frac{\pi}{2}$', r'$0$',
           r'$\frac{\pi}{2}$', r'$\frac{3\pi}{4}$',
           r'$\pi$'])
mp.yticks([-1, -0.5, 0.5, 1])
mp.plot(x, cos_y, linestyle='--', linewidth=1,
        color='red')
mp.plot(x, sin_y, linestyle=':', linewidth=3,
        color='green')
mp.show()

5.设置坐标轴
坐标轴名:left/right/bottom/top
ax = mp.gca() #获取当前坐标轴
ax.spines['坐标轴名'].set_position((坐标系,坐标))
ax.spines['坐标轴名'].set_color(颜色)
代码:
 

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
x = np.linspace(-np.pi, np.pi, 1000)
cos_y = np.cos(x) / 2
sin_y = np.sin(x)
mp.xlim(x.min() * 1.2, x.max() * 1.2)
mp.ylim(sin_y.min() * 1.2, sin_y.max() * 1.2)
mp.xticks([-np.pi, -np.pi / 2, 0,
           np.pi / 2, np.pi * 3 / 4, np.pi],
          [r'$-\pi$', r'$-\frac{\pi}{2}$', r'$0$',
           r'$\frac{\pi}{2}$', r'$\frac{3\pi}{4}$',
           r'$\pi$'])
mp.yticks([-1, -0.5, 0.5, 1])
ax = mp.gca()
ax.spines['left'].set_position(('data', 0))
ax.spines['bottom'].set_position(('data', 0))
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
mp.plot(x, cos_y, linestyle='--', linewidth=1,
        color='red')
mp.plot(x, sin_y, linestyle=':', linewidth=3,
        color='green')
mp.show()

6.图例
mp.plot(...,label=图例标签,...)
mp.legend([loc=位置])
代码:
 

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
x = np.linspace(-np.pi, np.pi, 1000)
cos_y = np.cos(x) / 2
sin_y = np.sin(x)
mp.xlim(x.min() * 1.2, x.max() * 1.2)
mp.ylim(sin_y.min() * 1.2, sin_y.max() * 1.2)
mp.xticks([-np.pi, -np.pi / 2, 0,
           np.pi / 2, np.pi * 3 / 4, np.pi],
          [r'$-\pi$', r'$-\frac{\pi}{2}$', r'$0$',
           r'$\frac{\pi}{2}$', r'$\frac{3\pi}{4}$',
           r'$\pi$'])
mp.yticks([-1, -0.5, 0.5, 1])
ax = mp.gca()
ax.spines['left'].set_position(('data', 0))
ax.spines['bottom'].set_position(('data', 0))
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
mp.plot(x, cos_y, linestyle='--', linewidth=1,
        color='red', label=r'$y=\frac{1}{2}cos(x)$')
mp.plot(x, sin_y, linestyle=':', linewidth=3,
        color='green', label=r'$y=sin(x)$')
# mp.legend(loc='lower right')
mp.legend()
mp.show()

7.特殊点
mp.scatter(水平坐标,垂直坐标,marker=典型,s=大小,color=颜色,edgecolor=边缘色,facecolor=填充色,zorder=图层序号)

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
x = np.linspace(-np.pi, np.pi, 1000)
cos_y = np.cos(x) / 2
sin_y = np.sin(x)
xo = np.pi * 3 / 4
yo_cos = np.cos(xo) / 2
yo_sin = np.sin(xo)
mp.xlim(x.min() * 1.2, x.max() * 1.2)
mp.ylim(sin_y.min() * 1.2, sin_y.max() * 1.2)
mp.xticks([-np.pi, -np.pi / 2, 0,
           np.pi / 2, np.pi * 3 / 4, np.pi],
          [r'$-\pi$', r'$-\frac{\pi}{2}$', r'$0$',
           r'$\frac{\pi}{2}$', r'$\frac{3\pi}{4}$',
           r'$\pi$'])
mp.yticks([-1, -0.5, 0.5, 1])
ax = mp.gca()
ax.spines['left'].set_position(('data', 0))
ax.spines['bottom'].set_position(('data', 0))
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
mp.plot(x, cos_y, linestyle='--', linewidth=1,
        color='red', label=r'$y=\frac{1}{2}cos(x)$')
mp.plot(x, sin_y, linestyle=':', linewidth=3,
        color='green', label=r'$y=sin(x)$')
mp.plot([xo, xo], [yo_cos, yo_sin], linewidth=0.5,
        color='dodgerblue')
mp.scatter([xo, xo], [yo_cos, yo_sin], marker='*',
           s=120, edgecolor='dodgerblue',
           facecolor='white', zorder=3)
# mp.legend(loc='lower right')
mp.legend()
mp.show()

8.备注
mp.annotate(
    备注文本,
    xy=目标坐标,
    xycoords=目标坐标系,
    xytext=文本坐标,
    textcoords=文本坐标系,
    fontsize=字体大小,
    arrowprops=箭头线属性)

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
x = np.linspace(-np.pi, np.pi, 1000)
cos_y = np.cos(x) / 2
sin_y = np.sin(x)
xo = np.pi * 3 / 4
yo_cos = np.cos(xo) / 2
yo_sin = np.sin(xo)
mp.xlim(x.min() * 1.2, x.max() * 1.2)
mp.ylim(sin_y.min() * 1.2, sin_y.max() * 1.2)
mp.xticks([-np.pi, -np.pi / 2, 0,
           np.pi / 2, np.pi * 3 / 4, np.pi],
          [r'$-\pi$', r'$-\frac{\pi}{2}$', r'$0$',
           r'$\frac{\pi}{2}$', r'$\frac{3\pi}{4}$',
           r'$\pi$'])
mp.yticks([-1, -0.5, 0.5, 1])
ax = mp.gca()
ax.spines['left'].set_position(('data', 0))
ax.spines['bottom'].set_position(('data', 0))
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
mp.plot(x, cos_y, linestyle='--', linewidth=1,
        color='red', label=r'$y=\frac{1}{2}cos(x)$')
mp.plot(x, sin_y, linestyle=':', linewidth=3,
        color='green', label=r'$y=sin(x)$')
mp.plot([xo, xo], [yo_cos, yo_sin], linewidth=0.5,
        color='dodgerblue')
mp.scatter([xo, xo], [yo_cos, yo_sin], marker='*',
           s=120, edgecolor='dodgerblue',
           facecolor='white', zorder=3)
mp.annotate(
    r'$\frac{1}{2}cos(\frac{3\pi}{4})=-\frac{\sqrt{2}}{4}$',
    xy=(xo, yo_cos), xycoords='data',
    xytext=(-90, -40), textcoords='offset points',
    fontsize=14,
    arrowprops=dict(arrowstyle='->',
                    connectionstyle='arc3, rad=.2'))
mp.annotate(
    r'$sin(\frac{3\pi}{4})=\frac{\sqrt{2}}{2}$',
    xy=(xo, yo_sin), xycoords='data',
    xytext=(20, 20), textcoords='offset points',
    fontsize=14,
    arrowprops=dict(arrowstyle='->',
                    connectionstyle='arc3, rad=.2'))
# mp.legend(loc='lower right')
mp.legend()
mp.show()

9图形对象(图形窗口)
mp.figure(窗口名称,figsize=窗口大小,dpi=分辨率,facecolor=颜色)
如果‘窗口名称’是第一次出现,那么就创建一个新窗口,其标题栏显示该名称,如果‘窗口名称’已经出现过,那么不在创建新窗口,而只是将与该名称相对应的窗口设置为当前窗口.所谓当前窗口,就是接受后续绘图操作的窗口。
mp.title(标题文本,fontsize=字体大小)
mp.xlabel(水平轴标签,fontsize=字体大小)
mp.ylabel(垂直轴标签,fontsize=字体大小)
mp.tick_params(...,labelsize=刻度标签字体大小,...)
mp.grid(linestyle=网格线型)
mp.tight_layout() #紧凑布局

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
x = np.linspace(-np.pi, np.pi, 1000)
cos_y = np.cos(x) / 2
sin_y = np.sin(x)
mp.figure('Figure Object 1', figsize=(4, 3), dpi=120,
          facecolor='lightgray')
mp.title('Figure Object 1', fontsize=16)
mp.xlabel('x', fontsize=12)
mp.ylabel('y', fontsize=12)
mp.tick_params(labelsize=8)
mp.grid(linestyle=':')
mp.tight_layout()
mp.figure('Figure Object 2', figsize=(4, 3), dpi=120,
          facecolor='lightgray')
mp.title('Figure Object 2', fontsize=16)
mp.xlabel('x', fontsize=12)
mp.ylabel('y', fontsize=12)
mp.tick_params(labelsize=8)
mp.grid(linestyle=':')
mp.tight_layout()
mp.figure('Figure Object 1')
mp.plot(x, cos_y, c='dodgerblue',
        label=r'$y=\frac{1}{2}cos(x)$')
mp.legend()
mp.figure('Figure Object 2')
mp.plot(x, sin_y, c='orangered',
        label=r'$y=sin(x)$')
mp.legend()
mp.show()

10.子图

  1. 矩阵式布局
    mp.subplot(行数,列数,序号)
    1 2 3
    4 5 6
    7 8 9
    mp.subplot(3,3,5)
    mp.subplot(335)
    代码:
    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    import matplotlib.pyplot as mp
    mp.figure('Subplot', facecolor='lightgray')
    mp.subplot(221)
    mp.xticks(())
    mp.yticks(())
    mp.text(0.5, 0.5, '1', ha='center', va='center',
            size=36, alpha=0.5)
    mp.subplot(222)
    mp.xticks(())
    mp.yticks(())
    mp.text(0.5, 0.5, '2', ha='center', va='center',
            size=36, alpha=0.5)
    mp.subplot(223)
    mp.xticks(())
    mp.yticks(())
    mp.text(0.5, 0.5, '3', ha='center', va='center',
            size=36, alpha=0.5)
    mp.subplot(224)
    mp.xticks(())
    mp.yticks(())
    mp.text(0.5, 0.5, '4', ha='center', va='center',
            size=36, alpha=0.5)
    mp.tight_layout()
    mp.show()

  2. 网格布局
    import matplotlib.gridspec as mg
    gs = mg.GridSpec(行数,列数)
    mp.subplot(gs[占行,占列])
    代码:
    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    import matplotlib.pyplot as mp
    import matplotlib.gridspec as mg
    mp.figure('Grid', facecolor='lightgray')
    gs = mg.GridSpec(3, 3)
    mp.subplot(gs[0, :2])
    mp.xticks(())
    mp.yticks(())
    mp.text(0.5, 0.5, '1', ha='center', va='center',
            size=36, alpha=0.5)
    mp.subplot(gs[1:, 0])
    mp.xticks(())
    mp.yticks(())
    mp.text(0.5, 0.5, '2', ha='center', va='center',
            size=36, alpha=0.5)
    mp.subplot(gs[2, 1:])
    mp.xticks(())
    mp.yticks(())
    mp.text(0.5, 0.5, '3', ha='center', va='center',
            size=36, alpha=0.5)
    mp.subplot(gs[:2, 2])
    mp.xticks(())
    mp.yticks(())
    mp.text(0.5, 0.5, '4', ha='center', va='center',
            size=36, alpha=0.5)
    mp.subplot(gs[1, 1])
    mp.xticks(())
    mp.yticks(())
    mp.text(0.5, 0.5, '5', ha='center', va='center',
            size=36, alpha=0.5)
    mp.tight_layout()
    mp.show()

  3. 自由式布局
    mp.axes([左,底,宽,高])
    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    import matplotlib.pyplot as mp
    mp.figure('Axes', facecolor='lightgray')
    mp.axes([0.03, 0.038, 0.94, 0.924])
    mp.xticks(())
    mp.yticks(())
    mp.text(0.5, 0.5, '1', ha='center', va='center',
            size=36, alpha=0.5)
    mp.axes([0.63, 0.076, 0.31, 0.308])
    mp.xticks(())
    mp.yticks(())
    mp.text(0.5, 0.5, '2', ha='center', va='center',
            size=36, alpha=0.5)
    mp.show()

     

11.刻度定位器
刻度定位器对象 = mp.xxxLocator(...)
ax = mp.gca()
ax.xaxis.set_major_locator(刻度器定位对象)
ax.xaxis.set_minor_locator(刻度器定位对象)
ax.yaxis.set_major_locator(刻度器定位对象)
ax.yaxis.set_minor_locator(刻度器定位对象)

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
# 创建图形窗口
mp.figure('Locator')
# 刻度定位器列表
locators = [
    # 空定位器:不绘制刻度
    'mp.NullLocator()',
    # 最大值定位器:最多绘制nbins个刻度,
    # 每两个刻度之间的间隔从steps列表中选择
    'mp.MaxNLocator(nbins=3, steps=[1, 3, 5, 7, 9])',
    # 定点定位器:根据locs参数中的位置绘制刻度
    'mp.FixedLocator(locs=[0, 2.5, 5, 7.5, 10])',
    # 自动定位器:由系统自动选择刻度的绘制位置
    'mp.AutoLocator()',
    # 索引定位器:由offset确定起始刻
    # 度,由base确定相邻刻度的间隔
    'mp.IndexLocator(offset=0.5, base=1.5)',
    # 多点定位器:从0开始,按照参
    # 数指定的间隔(缺省1)绘制刻度
    'mp.MultipleLocator()',
    # 线性定位器:等分numticks-1
    # 份,绘制numticks个刻度
    'mp.LinearLocator(numticks=21)',
    # 对数定位器:以base为底,用subs
    # 中的元素作为指数增量,绘制刻度
    'mp.LogLocator(base=2, subs=[1.0])']
# 刻度定位器数
n_locators = len(locators)
# 遍历刻度定位器列表
for i, locator in enumerate(locators):
        # 为每个刻度定位器创建一个子图
    mp.subplot(n_locators, 1, i + 1)
    # 设置坐标范围
    mp.xlim(0, 10)
    mp.ylim(-1, 1)
    # 关闭垂直坐标轴刻度
    mp.yticks(())
    # 获取当前坐标轴
    ax = mp.gca()
    # 隐藏除底轴以外的所有坐标轴
    ax.spines['left'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.spines['right'].set_color('none')
    # 将底坐标轴调整到子图中心位置
    ax.spines['bottom'].set_position(('data', 0))
    # 设置水平坐标轴的主刻度定位器
    ax.xaxis.set_major_locator(eval(locator))
    # 设置水平坐标轴的次刻度定位器为多点定位器,间隔0.1
    ax.xaxis.set_minor_locator(mp.MultipleLocator(0.1))
    # 绘制一条与水平坐标轴重合的直线,无色透明
    mp.plot(np.arange(11), np.zeros(11), c='none')
    # 标记所用刻度定位器类名
    mp.text(5, 0.3, locator[3:], ha='center',
            size=12)
# 紧凑布局
mp.tight_layout()
# 显示图形
mp.show()

12.刻度网格线
ax = mp.gca()
ax.grid(which='major'/'minor',axis='x'/'y'/'both',linewidth=线宽,linestyle=线型,color=颜色,alpha=透明度)

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
x = np.linspace(-5, 5, 1000)
y = 8 * np.sinc(x)
mp.figure('Grid', facecolor='lightgray')
mp.title('Grid', fontsize=20)
mp.xlabel('x', fontsize=14)
mp.ylabel('y', fontsize=14)
ax = mp.gca()
ax.xaxis.set_major_locator(mp.MultipleLocator(1.0))
ax.xaxis.set_minor_locator(mp.MultipleLocator(0.1))
ax.yaxis.set_major_locator(mp.MultipleLocator(1.0))
ax.yaxis.set_minor_locator(mp.MultipleLocator(0.1))
mp.tick_params(labelsize=10)
ax.grid(which='major', axis='both', linewidth=0.75,
        linestyle='-', color='orange')
ax.grid(which='minor', axis='both', linewidth=0.25,
        linestyle='-', color='orange')
mp.plot(x, y, c='dodgerblue', label=r'$y=8sinc(x)$')
mp.legend()
mp.show()

13.半对数坐标
mp.semilogy(参数同plot函数)

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
y = np.array([1, 10, 100, 1000, 100, 10, 1])
mp.figure('Normal & Log', facecolor='lightgray')
mp.subplot(211)
mp.title('Normal', fontsize=20)
mp.ylabel('y', fontsize=14)
ax = mp.gca()
ax.xaxis.set_major_locator(mp.MultipleLocator(1.0))
ax.xaxis.set_minor_locator(mp.MultipleLocator(0.1))
ax.yaxis.set_major_locator(mp.MultipleLocator(250))
ax.yaxis.set_minor_locator(mp.MultipleLocator(50))
mp.tick_params(labelsize=10)
ax.grid(which='major', axis='both', linewidth=0.75,
        linestyle='-', color='orange')
ax.grid(which='minor', axis='both', linewidth=0.25,
        linestyle='-', color='orange')
mp.plot(y, 'o-', c='dodgerblue', label='plot')
mp.legend()
mp.subplot(212)
mp.title('Log', fontsize=20)
mp.xlabel('x', fontsize=14)
mp.ylabel('y', fontsize=14)
ax = mp.gca()
ax.xaxis.set_major_locator(mp.MultipleLocator(1.0))
ax.xaxis.set_minor_locator(mp.MultipleLocator(0.1))
mp.tick_params(labelsize=10)
ax.grid(which='major', axis='both', linewidth=0.75,
        linestyle='-', color='orange')
ax.grid(which='minor', axis='both', linewidth=0.25,
        linestyle='-', color='orange')
mp.semilogy(y, 'o-', c='dodgerblue', label='semilogy')
mp.legend()
mp.tight_layout()
mp.show()

14.散点图
可以通过每个点的坐标,颜色,大小和形状,不同的特征值。
身高 体重 性别 年龄段 种族
1.8    80    男     中年    亚洲
1.6    50    女     青少    美洲
...
 x       y     颜色   大小   形状
代码

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
n = 1000  # 1000个样本
x = np.random.normal(0, 1, n)  # 标准正态分布
y = np.random.normal(0, 1, n)  # 标准正态分布
d = np.sqrt(x ** 2 + y ** 2)
mp.figure('Scatter', facecolor='lightgray')
mp.title('Scatter', fontsize=20)
mp.xlabel('x', fontsize=14)
mp.ylabel('y', fontsize=14)
mp.tick_params(labelsize=10)
mp.grid(linestyle=':')
# jet_r: 从深红到深蓝的渐变颜色表
mp.scatter(x, y, c=d, cmap='jet_r', s=60, alpha=0.5)
mp.show()

15.填充
mp.fill_between(水平坐标,下边界曲线上点的垂直坐标,上边界曲线上点的垂直坐标,
填充条件,color=颜色,alpha=透明度)

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
n = 1000
x = np.linspace(0, 8 * np.pi, n)
sin_y = np.sin(x)
cos_y = np.cos(x / 2) / 2
mp.figure('Fill', facecolor='lightgray')
mp.title('Fill', fontsize=20)
mp.xlabel('x', fontsize=14)
mp.ylabel('y', fontsize=14)
mp.tick_params(labelsize=10)
mp.grid(linestyle=':')
mp.plot(x, sin_y, c='dodgerblue',
        label=r'$y=sin(x)$')
mp.plot(x, cos_y, c='orangered',
        label=r'$y=\frac{1}{2}cos(\frac{x}{2})$')
mp.fill_between(x, cos_y, sin_y, cos_y < sin_y,
                color='dodgerblue', alpha=0.5)
mp.fill_between(x, cos_y, sin_y, cos_y > sin_y,
                color='orangered', alpha=0.5)
mp.legend()
mp.show()

16.条形图
mp.bar(水平坐标,高度,宽度[,底坐标],color=颜色,alpha=透明度,label=图例标签)
代码:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
apples = np.array([
    30, 25, 22, 36, 21, 29, 20, 24, 33, 19, 27, 15])
oranges = np.array([
    24, 33, 19, 27, 35, 20, 15, 27, 20, 32, 20, 22])
mp.figure('Bar', facecolor='lightgray')
mp.title('Bar', fontsize=20)
mp.xlabel('Month', fontsize=14)
mp.ylabel('Price', fontsize=14)
mp.tick_params(labelsize=10)
mp.grid(axis='y', linestyle=':')
mp.ylim((0, 40))
x = np.arange(len(apples))
mp.bar(x, apples, 0.4, color='dodgerblue',
       label='Apple')
mp.bar(x + 0.3, oranges, 0.4, color='orangered',
       label='Orange', alpha=0.75)
mp.xticks(x + 0.1, [
    'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
    'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
mp.legend()
mp.show()

17.饼图
mp.pie(值,间隙,标签,颜色,格式,
  shadow=是否带阴影,startangel=起始角度)

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
values = [26, 17, 21, 29, 11]
spaces = [0.05, 0.01, 0.01, 0.01, 0.01]
labels = ['Python', 'JavaScript',
          'C++', 'Java', 'PHP']
colors = ['dodgerblue', 'orangered',
          'limegreen', 'violet', 'gold']
mp.figure('Pie', facecolor='lightgray')
mp.title('Pie', fontsize=20)
mp.pie(values, spaces, labels, colors, '%d%%',
       shadow=True, startangle=90)
# 等轴比例
mp.axis('equal')
mp.show()

18.等高线图
mp.contourf(x,y,z,阶数,cmap=颜色映射)
mp.contour(x,y,z,阶数,linewidths=线宽)
代码:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as mp
n = 1000
# 网格化
x, y = np.meshgrid(np.linspace(-3, 3, n),
                   np.linspace(-3, 3, n))
z = (1 - x / 2 + x ** 5 + y ** 3) * np.exp(
    -x ** 2 - y ** 2)
mp.figure('Contour', facecolor='lightgray')
mp.title('Contour', fontsize=20)
mp.xlabel('x', fontsize=14)
mp.ylabel('y', fontsize=14)
mp.tick_params(labelsize=10)
mp.grid(linestyle=':')
# 绘制等高线图
mp.contourf(x, y, z, 8, cmap='jet')
cntr = mp.contour(x, y, z, 8, colors='black',
                  linewidths=0.5)
# 为等高线图添加高度标签
mp.clabel(cntr, inline_spacing=1, fmt='%.1f',
          fontsize=10)
mp.show()

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值