
Bikash Santra
Indian Statistical Institute, Kolkata
import matplotlib # OR, from matplotlib import pyplot as plt
import numpy as np
print(matplotlib.__version__)
3.3.4
# The inline is important for the notebook, so that plots are displayed in the notebook and not in a new window.
import matplotlib.pyplot as plt
marks = np.random.randint(1,100,100)
marks1 = np.random.randint(1,100,100)
plt.plot(x,marks,'*') # displaying points
plt.plot(x,marks1,'+')
plt.legend(['Marks','Marks1'], loc = 'upper right')
plt.show()
x1 = np.random.normal(loc=[0,0], scale=1, size=(100,2))
x2 = np.random.normal(loc=[10,0], scale=1, size=(100,2))
x3 = np.random.normal(loc=[5,5], scale=1, size=(100,2))
plt.figure(figsize=(9,6), dpi=150)
g1 = plt.scatter(
x1[:,0], x1[:,1], marker='x', s=80, c='b',
label='Gaussian #1'
)
g2 = plt.scatter(
x2[:,0], x2[:,1], marker='o', s=80, c='w', edgecolor='r',
label='Gaussian #2'
)
g3 = plt.scatter(
x3[:,0], x3[:,1], marker='d', s=80, c='w', edgecolor='g',
label='Gaussian #3'
)
plt.legend(
handles=[g1, g2, g3], fontsize=30, shadow=True,
bbox_to_anchor=(1.03, 1), borderaxespad=0
)
plt.xlabel('Feature 1', fontsize=30)
plt.ylabel('Feature 2', fontsize=30)
plt.title('Mixture of 3 Gaussians', fontsize=30)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.show()
#plt.savefig('scatter_demo.png', dpi=300, bbox_inches='tight', pad_inches=0.05)
x = np.arange(1,101)
marks = np.random.randint(1,100,100)
marks1 = np.random.randint(1,100,100)
plt.bar(x,marks,width = 0.5)
plt.show()
plt.bar(x,marks1,width = 0.9)
plt.show()
help(plt.bar)
Help on function bar in module matplotlib.pyplot:
bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)
Make a bar plot.
The bars are positioned at *x* with the given *align*\ment. Their
dimensions are given by *height* and *width*. The vertical baseline
is *bottom* (default 0).
Many parameters can take either a single value applying to all bars
or a sequence of values, one for each bar.
Parameters
----------
x : float or array-like
The x coordinates of the bars. See also *align* for the
alignment of the bars to the coordinates.
height : float or array-like
The height(s) of the bars.
width : float or array-like, default: 0.8
The width(s) of the bars.
bottom : float or array-like, default: 0
The y coordinate(s) of the bars bases.
align : {'center', 'edge'}, default: 'center'
Alignment of the bars to the *x* coordinates:
- 'center': Center the base on the *x* positions.
- 'edge': Align the left edges of the bars with the *x* positions.
To align the bars on the right edge pass a negative *width* and
``align='edge'``.
Returns
-------
`.BarContainer`
Container with all the bars and optionally errorbars.
Other Parameters
----------------
color : color or list of color, optional
The colors of the bar faces.
edgecolor : color or list of color, optional
The colors of the bar edges.
linewidth : float or array-like, optional
Width of the bar edge(s). If 0, don't draw edges.
tick_label : str or list of str, optional
The tick labels of the bars.
Default: None (Use default numeric labels.)
xerr, yerr : float or array-like of shape(N,) or shape(2, N), optional
If not *None*, add horizontal / vertical errorbars to the bar tips.
The values are +/- sizes relative to the data:
- scalar: symmetric +/- values for all bars
- shape(N,): symmetric +/- values for each bar
- shape(2, N): Separate - and + values for each bar. First row
contains the lower errors, the second row contains the upper
errors.
- *None*: No errorbar. (Default)
See :doc:`/gallery/statistics/errorbar_features`
for an example on the usage of ``xerr`` and ``yerr``.
ecolor : color or list of color, default: 'black'
The line color of the errorbars.
capsize : float, default: :rc:`errorbar.capsize`
The length of the error bar caps in points.
error_kw : dict, optional
Dictionary of kwargs to be passed to the `~.Axes.errorbar`
method. Values of *ecolor* or *capsize* defined here take
precedence over the independent kwargs.
log : bool, default: False
If *True*, set the y-axis to be log scale.
**kwargs : `.Rectangle` properties
Properties:
agg_filter: a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array
alpha: float or None
animated: bool
antialiased or aa: unknown
capstyle: {'butt', 'round', 'projecting'}
clip_box: `.Bbox`
clip_on: bool
clip_path: Patch or (Path, Transform) or None
color: color
contains: unknown
edgecolor or ec: color or None or 'auto'
facecolor or fc: color or None
figure: `.Figure`
fill: bool
gid: str
hatch: {'/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}
in_layout: bool
joinstyle: {'miter', 'round', 'bevel'}
label: object
linestyle or ls: {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}
linewidth or lw: float or None
path_effects: `.AbstractPathEffect`
picker: None or bool or callable
rasterized: bool or None
sketch_params: (scale: float, length: float, randomness: float)
snap: bool or None
transform: `.Transform`
url: str
visible: bool
zorder: float
See Also
--------
barh: Plot a horizontal bar plot.
Notes
-----
Stacked bars can be achieved by passing individual *bottom* values per
bar. See :doc:`/gallery/lines_bars_and_markers/bar_stacked`.
.. note::
In addition to the above described arguments, this function can take
a *data* keyword argument. If such a *data* argument is given,
every other argument can also be string ``s``, which is
interpreted as ``data[s]`` (unless this raises an exception).
Objects passed as **data** must support item access (``data[s]``) and
membership test (``s in data``).
x = np.linspace(0, 3, 20)
print(x, x.shape)
y = np.linspace(0, 9, 20)
print(y)
[0. 0.15789474 0.31578947 0.47368421 0.63157895 0.78947368 0.94736842 1.10526316 1.26315789 1.42105263 1.57894737 1.73684211 1.89473684 2.05263158 2.21052632 2.36842105 2.52631579 2.68421053 2.84210526 3. ] (20,) [0. 0.47368421 0.94736842 1.42105263 1.89473684 2.36842105 2.84210526 3.31578947 3.78947368 4.26315789 4.73684211 5.21052632 5.68421053 6.15789474 6.63157895 7.10526316 7.57894737 8.05263158 8.52631579 9. ]
fig = plt.figure()
# fig.subplots_adjust(bottom=0.1, left=0.1, top = 0.9, right=.9)
plt.subplot(2, 1, 1)
plt.plot(x,y)
plt.xticks((np.linspace(0,3,3))), plt.yticks(np.linspace(0,9,3))
# plt.subplot(2, 3, 2)
# plt.xticks(()), plt.yticks(())
# plt.subplot(2, 3, 3)
# plt.xticks(()), plt.yticks(())
plt.subplot(2, 3, 4)
plt.xticks(())
plt.yticks(())
plt.subplot(2, 3, 5)
plt.xticks(())
plt.yticks(())
plt.subplot(2, 3, 6)
plt.xticks(())
plt.yticks(())
plt.show()
fig = plt.figure()
plt.subplot(3,1,1)
plt.plot(x,x**4)
plt.xticks((np.linspace(0,3,3))),plt.yticks(())
plt.subplot(3,2,4)
plt.plot(x,x**4)
plt.xticks((np.linspace(0,3,3))),plt.yticks(())
plt.subplot(3,2,5)
plt.plot(x,x**5)
plt.xticks((np.linspace(0,3,3))),plt.yticks(())
plt.subplot(3,3,7)
plt.plot(x,x)
plt.xticks((np.linspace(0,3,3))),plt.yticks(())
plt.subplot(3,3,8)
plt.plot(x,x**2)
plt.xticks((np.linspace(0,3,3))),plt.yticks(())
plt.subplot(3,3,9)
plt.plot(x,x**3)
plt.xticks((np.linspace(0,3,3))),plt.yticks(())
# plt.subplot(3,1,7)
# plt.plot(x,x)
# plt.xticks((np.linspace(0,3,3))),plt.yticks(())
(([<matplotlib.axis.XTick at 0x1da312f8250>, <matplotlib.axis.XTick at 0x1da312f8160>, <matplotlib.axis.XTick at 0x1da313881c0>], [Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, '')]), ([], []))
fig = plt.figure()
plt.subplot(3,3,1)
plt.plot(x,x)
plt.xticks((np.linspace(0,3,3))),plt.yticks(())
plt.subplot(3,3,2)
plt.plot(x,x**2)
plt.xticks((np.linspace(0,3,3))),plt.yticks(())
plt.subplot(3,3,3)
plt.plot(x,x**3)
plt.xticks((np.linspace(0,3,3))),plt.yticks(())
plt.subplot(3,2,3)
plt.plot(x,x**4)
plt.xticks((np.linspace(0,3,3))),plt.yticks(())
plt.subplot(3,2,4)
plt.plot(x,x**5)
plt.xticks((np.linspace(0,3,3))),plt.yticks(())
plt.subplot(3,1,3)
plt.plot(x,x**6)
plt.xticks((np.linspace(0,3,3))),plt.yticks(())
(([<matplotlib.axis.XTick at 0x1da3191d580>, <matplotlib.axis.XTick at 0x1da3191d550>, <matplotlib.axis.XTick at 0x1da314b1ee0>], [Text(0, 0, ''), Text(0, 0, ''), Text(0, 0, '')]), ([], []))
# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
# Set up a subplot grid that has height 2 and width 1,
# and set the first such subplot as active.
plt.subplot(2, 1, 1)
# Make the first plot
plt.plot(x, y_sin)
plt.title('Sine')
# Set the second subplot as active, and make the second plot.
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('Cosine')
# Show the figure.
plt.show()
a) Matpltlob documentation gallery
b) https://www.scipy-lectures.org/intro/matplotlib/index.html
c) https://www.scipy-lectures.org/packages/3d_plotting/index.html
d) https://github.com/kuleshov/cs228-material/blob/master/tutorials/python/cs228-python-tutorial.ipynb