module helpers.matplotlib_helper_xyz#

Short summary#

module ensae_teaching_cs.helpers.matplotlib_helper_xyz

scatter plots

source on GitHub

Functions#

function

truncated documentation

scatter_xy_id

Creates a scatter plot with a different color for each zone id. The function requires matplotlib.

scatter_xyc

Draws a 2D graph (X,Y, color), the color is chosen based on a value f(x,y) The function requires matplotlib

scatter_xyz

Draws a 3D graph (X, Y, Z). The function requires matplotlib and scipy.

Documentation#

scatter plots

source on GitHub

ensae_teaching_cs.helpers.matplotlib_helper_xyz.scatter_xy_id(xy_id, legend=None, ax=None, **options)#

Creates a scatter plot with a different color for each zone id. The function requires matplotlib.

Paramètres:
  • xy_id – list of 3-uple (x, y, zone_id)

  • legend – dictionary {id: legend } or None if there is not any

  • ax – existing graph to plot on (can be None)

  • options – others options: xlabel, ylabel, title, marker, figsize (if ax is None)

Renvoie:

fig, ax (fig is None if ax was sent to the function)

import random
def generate_gauss(x, y, sigma, N=1000):
    res = []
    for i in range(N):
        u = random.gauss(0, 1)
        a = sigma * u + x
        b = sigma * random.gauss(0, 1) + y + u
        res.append((a, b))
    return res
nuage1 = generate_gauss(0, 0, 3)
nuage2 = generate_gauss(3, 4, 2)
nuage = [(a, b, 0) for a, b in nuage1] + [(a, b, 1) for a, b in nuage2]

import matplotlib.pyplot as plt
from ensae_teaching_cs.helpers.matplotlib_helper_xyz import scatter_xy_id
fig, ax = scatter_xy_id(nuage, title="example with random observations",
                        legend={0: "c0", 1: "c1"})
plt.show()

(png, hires.png, pdf)

../../_images/matplotlib_helper_xyz-1.png

The error ValueError: Unknown projection '3d' is raised when the line from mpl_toolkits.mplot3d import Axes3D is missing.

source on GitHub

ensae_teaching_cs.helpers.matplotlib_helper_xyz.scatter_xyc(points, smooth=0, div=10, ax=None, **options)#

Draws a 2D graph (X,Y, color), the color is chosen based on a value f(x,y) The function requires matplotlib and scipy.

Paramètres:
  • points – (x,y, z=f(x,y) )

  • smooth – applies n times a smoothing I * M (convolutional)

  • div – number of divisions for axis

  • options – others options: xlabel, ylabel, title, figsize (if ax is None)

  • axmatplotlib axis

Renvoie:

fig, ax (fig is None if ax was sent to the function)

import random
def generate_gauss(x, y, sigma, N=1000):
    res = []
    for i in range(N):
        u = random.gauss(0, 1)
        a = sigma * u + x
        b = sigma * random.gauss(0, 1) + y + u
        res.append((a, b))
    return res
def f(a, b):
    return (a ** 2 + b ** 2) ** 0.5
nuage1 = generate_gauss(0, 0, 3)
nuage2 = generate_gauss(3, 4, 2)
nuage = [(a, b, f(a, b)) for a, b in nuage1] + [(a, b, f(a, b)) for a, b in nuage2]
import matplotlib.pyplot as plt
from ensae_teaching_cs.helpers.matplotlib_helper_xyz import scatter_xyc
fig, ax = scatter_xyc(nuage, title="example with random observations")
plt.show()

(png, hires.png, pdf)

../../_images/matplotlib_helper_xyz-2.png

The error ValueError: Unknown projection '3d' is raised when the line from mpl_toolkits.mplot3d import Axes3D is missing.

source on GitHub

ensae_teaching_cs.helpers.matplotlib_helper_xyz.scatter_xyz(points, smooth=0, div=100, ax=None, **options)#

Draws a 3D graph (X, Y, Z). The function requires matplotlib and scipy.

Paramètres:
  • points – (x,y, z=f(x,y) )

  • div – number of divisions for axis

  • smooth – applies n times a smoothing I * M (convolutional)

  • ax – existing graph to plot on (can be None)

  • options – others options: xlabel, ylabel, zlabel, title, elev, angle, figsize (if ax is None) - elev, angle: see view_init)

Renvoie:

fig, ax (fig is None if ax was sent to the function)

If ax is None, axes are created like:

fig = plt.figure(figsize=options.get('figsize', None))
ax = fig.gca(projection='3d')
import random
def generate_gauss(x, y, sigma, N=1000):
    res = []
    for i in range(N):
        u = random.gauss(0, 1)
        a = sigma * u + x
        b = sigma * random.gauss(0, 1) + y + u
        res.append((a, b))
    return res
def f(a, b):
    return (a ** 2 + b ** 2) ** 0.5
nuage1 = generate_gauss(0, 0, 3)
nuage2 = generate_gauss(3, 4, 2)
nuage = [(a, b, f(a, b)) for a, b in nuage1] + [(a, b, f(a, b)) for a, b in nuage2]

import matplotlib.pyplot as plt
from ensae_teaching_cs.helpers.matplotlib_helper_xyz import scatter_xyz
fig, ax = scatter_xyz(nuage, title="example with random observations")
plt.show()

(png, hires.png, pdf)

../../_images/matplotlib_helper_xyz-3.png

The error ValueError: Unknown projection '3d' is raised when the line from mpl_toolkits.mplot3d import Axes3D is missing.

source on GitHub