Lab 1
Lab 1
Lab 1
Objectives:
1. To plot cartesian curves
Customizations:
plot(x, y, 'bo'): plot x and y using blue circle markers
plot(y): plot y using x as index array 0..N-1
plot(y, 'r+'): ditto, but with red plusses
plot(x, y, 'go--', linewidth=2, markersize=12) ### plot(x, y, color='green', marker='o',
linestyle='dashed',linewidth=2,markersize=12)
Syntax:
scatter(x_axis_data, y_axis_data, s=None, c=None, marker=None, cmap=None,vmin=None,
vmax=None, alpha=None, linewidths=None, edgecolors=None)
Call signatures::
The coordinates of the points or line nodes are given by *x*, *y*.
>>> plot(x, y) # plot x and y using default line style and color
>>> plot(x, y, 'bo') # plot x and y using blue circle markers
>>> plot(y) # plot y using x as index array 0..N-1
>>> plot(y, 'r+') # ditto, but with red plusses
There's a convenient way for plotting objects with labelled data (i.e.
data that can be accessed by index ``obj['y']``). Instead of giving
the data in *x* and *y*, you can provide the object in the *data*
parameter and just give the labels for *x* and *y*::
- The most straight forward way is just to call `plot` multiple times.
Example:
- If *x* and/or *y* are 2D arrays a separate data set will be drawn
for every column. If both *x* and *y* are 2D, they must have the
same shape. If only one of them is 2D with shape (N, m) the other
must have length N and will be used for every data set m.
Example:
>>> x = [1, 2, 3]
>>> y = np.array([[1, 2], [3, 4], [5, 6]])
>>> plot(x, y)
is equivalent to:
Parameters
----------
x, y : array-like or scalar
The horizontal / vertical coordinates of the data points.
*x* values are optional and default to ``range(len(y))``.
.. note::
Technically there's a slight ambiguity in calls where the
second label is a valid *fmt*. ``plot('n', 'o', data=obj)``
could be ``plt(x, y)`` or ``plt(y, fmt)``. In such cases,
the former interpretation is chosen, but a warning is issued.
You may suppress the warning by adding an empty format string
``plot('n', 'o', '', data=obj)``.
Returns
-------
list of `.Line2D`
A list of lines representing the plotted data.
Other Parameters
----------------
scalex, scaley : bool, default: True
These parameters determine if the view limits are adapted to the
data limits. The values are passed on to `autoscale_view`.
If you specify multiple lines with one plot call, the kwargs apply
to all those lines. In case the label object is iterable, each
element is used as labels for each set of data.
Properties:
agg_filter: a filter function, which takes a (m, n, 3) float array and a d
pi value, and returns a (m, n, 3) array
alpha: scalar or None
animated: bool
antialiased or aa: bool
clip_box: `.Bbox`
clip_on: bool
clip_path: Patch or (Path, Transform) or None
color or c: color
dash_capstyle: `.CapStyle` or {'butt', 'projecting', 'round'}
dash_joinstyle: `.JoinStyle` or {'miter', 'round', 'bevel'}
dashes: sequence of floats (on/off ink in points) or (None, None)
data: (2, N) array or two 1D arrays
drawstyle or ds: {'default', 'steps', 'steps-pre', 'steps-mid', 'steps-pos
t'}, default: 'default'
figure: `.Figure`
fillstyle: {'full', 'left', 'right', 'bottom', 'top', 'none'}
gid: str
in_layout: bool
label: object
linestyle or ls: {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}
linewidth or lw: float
marker: marker style string, `~.path.Path` or `~.markers.MarkerStyle`
markeredgecolor or mec: color
markeredgewidth or mew: float
markerfacecolor or mfc: color
markerfacecoloralt or mfcalt: color
markersize or ms: float
markevery: None or int or (int, int) or slice or list[int] or float or (fl
oat, float) or list[bool]
path_effects: `.AbstractPathEffect`
picker: float or callable[[Artist, Event], tuple[bool, dict]]
pickradius: float
rasterized: bool
sketch_params: (scale: float, length: float, randomness: float)
snap: bool or None
solid_capstyle: `.CapStyle` or {'butt', 'projecting', 'round'}
solid_joinstyle: `.JoinStyle` or {'miter', 'round', 'bevel'}
transform: unknown
url: str
visible: bool
xdata: 1D array
ydata: 1D array
zorder: float
See Also
--------
scatter : XY scatter plot with markers of varying size and/or color (
sometimes also called bubble chart).
Notes
-----
**Format Strings**
fmt = '[marker][line][color]'
Each of them is optional. If not provided, the value from the style
cycle is used. Exception: If ``line`` is given, but no ``marker``,
the data will be a line without markers.
**Markers**
============= ===============================
character description
============= ===============================
``'.'`` point marker
``','`` pixel marker
``'o'`` circle marker
``'v'`` triangle_down marker
``'^'`` triangle_up marker
``'<'`` triangle_left marker
``'>'`` triangle_right marker
``'1'`` tri_down marker
``'2'`` tri_up marker
``'3'`` tri_left marker
``'4'`` tri_right marker
``'8'`` octagon marker
``'s'`` square marker
``'p'`` pentagon marker
``'P'`` plus (filled) marker
``'*'`` star marker
``'h'`` hexagon1 marker
``'H'`` hexagon2 marker
``'+'`` plus marker
``'x'`` x marker
``'X'`` x (filled) marker
``'D'`` diamond marker
``'d'`` thin_diamond marker
``'|'`` vline marker
``'_'`` hline marker
============= ===============================
**Line Styles**
============= ===============================
character description
============= ===============================
``'-'`` solid line style
``'--'`` dashed line style
``'-.'`` dash-dot line style
``':'`` dotted line style
============= ===============================
Example format strings::
**Colors**
============= ===============================
character color
============= ===============================
``'b'`` blue
``'g'`` green
``'r'`` red
``'c'`` cyan
``'m'`` magenta
``'y'`` yellow
``'k'`` black
``'w'`` white
============= ===============================
and the ``'CN'`` colors that index into the default property cycle.
If the color is the only part of the format string, you can
additionally use any `matplotlib.colors` spec, e.g. full names
(``'green'``) or hex strings (``'#008000'``).
Functions
1. Exponential curve
In [7]: # importing the required modules
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-10, 10, 0.001) # x takes the values between -10 and 10 with a step l
y = np.exp(x) # Exponential function
Implicit Function
Syntax:
plot_implicit(expr, x_var=None, y_var=None, adaptive=True, depth=0, points=300,
line_color='blue', show=True, **kwargs)
x_var (optional) : symbol to plot on x-axis or tuple giving symbol and range as (symbol,
xmin, xmax)
y_var (optional) : symbol to plot on y-axis or tuple giving symbol and range as (symbol,
ymin, ymax)
If neither x_var nor y_var are given then the free symbols in the expression will be
assigned in the order they are sorted.
** adaptive: Boolean. The default value is set to True. It has to beset to False if you want
to use a mesh grid.
** depth : integer. The depth of recursion for adaptive mesh grid. Default value is 0.
Takes value in the range (0, 4).
** points: integer. The number of points if adaptive mesh grid is not used. Default value
is 300.
** show: Boolean. Default value is True. If set to False, the plot will not be shown. See
Plot for further information.
title string. The title for the plot.
Aesthetics options:
In [11]: # importing Sympy package with plot_implicit, symbols and Eq functions only
# symbols: used to declare variable as symbolic expression
# Eq: sets up an equation. Ex: 2x-y=0 is written as Eq(2*x-y,0)
2 2 2
2. Astroid: x 3
+ y 3
= a3 ,a > 0
In [ ]: p2 = plot_implicit(
Eq(x**(2/3) + y**(2/3), 3**(2/3)), (x, -5, 5), (y, -5, 5),title= 'Astroid: $x^{
3. Strophoid:
2 2
y (a − x) = x (a + x), a > 0
4. Cissiod: y 2
(a − x) = x , a > 0
3
In [ ]: p4=plot_implicit(
Eq((y**2)*(3-x),x**3),(x,-2,5),(y,-5,5)) # a=3
5. Leminscate: a 2
y
2 2
= x (a
2
− x )
2
In [ ]: p5=plot_implicit(
Eq(4*(y**2),(x**2)*(4-x**2)),(x,-5,5),(y,-5,5)) # a=2
6. Folium of De-Cartes: x 3
+ y
3
= 3axy
In [ ]: p6=plot_implicit(
Eq(x**3+y**3,3*2*x*y),(x,-5,5),(y,-5,5)) # a=2
Polar Curves
The matplotlib.pyplot.polar() function in pyplot
module of matplotlib python library is used to plot
the curves in polar coordinates.
polar(theta,r1,'g')
show()
4. Four leaved Rose: r = 2|cos2x|
5. Ellipse: r =
2
ab
2
√asin (θ)+bcos (θ)
plt.axes(projection = 'polar')
a = 5
b = 2
plt.axes(projection = 'polar')
a=3
for theta in np.linspace(-2*np.pi, 2*np.pi, 100): #loop over a list of theta, whi
x.append(r*np.cos(theta)) #add the corresponding expression of x to the x list
y.append(r*np.sin(theta)) #same for y
2. Cycloid:
x = a(θ − sinθ); y = a(1 − sinθ)
for theta in np.linspace(-2*np.pi, 2*np.pi, 100): #loop over a list of theta, whi
x.append(r*(theta - np.sin(theta))) #add the corresponding expression of x to t
y.append(r*(1 - np.cos(theta))) #same for y
Exercise:
Plot the following:
Parabola y 2
= 4ax
2
2
y
Hyperbola
x
− = 1
2 2
a b
πx
cos( )
2
π
1 + sin(x + )
4
In [ ]:
In [ ]:
In [ ]: