larray.Array.plot
- property Array.plot: PlotObject
Plot the data of the array into a graph (window pop-up).
- Parameters
- kindstr
‘line’ : line plot (default)
‘bar’ : vertical bar plot
‘barh’ : horizontal bar plot
‘hist’ : histogram
‘box’ : boxplot
‘kde’ : Kernel Density Estimation plot
‘density’ : same as ‘kde’
‘area’ : area plot
‘pie’ : pie plot
‘scatter’ : scatter plot (if array’s dimensions >= 2)
‘hexbin’ : hexbin plot (if array’s dimensions >= 2)
- ‘heatmap’: heatmap plot (if array’s dimensions >= 2).
See Array.plot.heatmap for more details.
- filepathstr or Path, default None
Save plot as a file at filepath. Defaults to None (do not save). When saving the plot to a file, the function returns None. In other words, in that case, the plot is no longer available for further tweaking or display.
- showbool, optional
Whether to display the plot directly. Defaults to True if filepath is None and ax is None, False otherwise.
- axmatplotlib axes object, default None
- subplotsboolean, Axis, int, str or tuple, default False
Make several subplots. - if an Axis (or int or str), make subplots for each label of that axis. - if a tuple of Axis (or int or str), make subplots for each combination of
labels of those axes.
True is equivalent to all axes except the last.
Defaults to False.
- sharexboolean, default True if ax is None else False
When subplots are used, share x axis and set some x axis labels to invisible; defaults to True if ax is None otherwise False if an ax is passed in; Be aware, that passing in both an ax and sharex=True will alter all x axis labels for all axis in a figure!
- shareyboolean, default False
When subplots are used, share y axis and set some y axis labels to invisible.
- layouttuple (optional)
(rows, columns) for the layout of subplots
- figsizea tuple (width, height) in inches
- use_indexboolean, default True
Use index as ticks for x axis
- titlestring
Title to use for the plot
- gridboolean, default None (matlab style default)
Axis grid lines
- legendFalse/True/’reverse’
Place legend on axis subplots. Defaults to True.
- stylelist or dict
matplotlib line style per column
- logxboolean, default False
Use log scaling on x axis
- logyboolean, default False
Use log scaling on y axis
- loglogboolean, default False
Use log scaling on both x and y axes
- xtickssequence
Values to use for the xticks
- ytickssequence
Values to use for the yticks
- xlim2-tuple/list, optional
Limits (minimum and maximum values) on x axis. If this argument is not used, or None for either bound, these are determined automatically from the data. Defaults to (None, None).
- ylim2-tuple/list, optional
Limits (minimum and maximum values) on y axis. If this argument is not used, or None for either bound, these are determined automatically from the data. Defaults to (None, None).
- rotint, default None
Rotation for ticks (xticks for vertical, yticks for horizontal plots)
- fontsizeint, default None
Font size for xticks and yticks
- colormapstr or matplotlib colormap object, default None
Colormap to select colors from. If string, load colormap with that name from matplotlib.
- colorbarboolean, optional
If True, plot colorbar (only relevant for ‘scatter’ and ‘hexbin’ plots)
- positionfloat, optional
Specify relative alignments for bar plot layout. From 0 (left/bottom-end) to 1 (right/top-end). Defaults to 0.5 (center).
- yerrarray-like, optional
Error bars on y axis
- xerrarray-like, optional
Error bars on x axis
- stackboolean, Axis, int, str or tuple, optional
Make a stacked plot. - if an Axis (or int or str), stack that axis. - if a tuple of Axis (or int or str), stack each combination of labels of those axes. - True is equivalent to all axes (not already used in other arguments) except the last. Defaults to False in line and bar plots, and True in area plot.
- animateAxis, int, str or tuple, optional
Make an animated plot. - if an Axis (or int or str), animate that axis (create one image per label on that axis).
One would usually use a time-related axis.
if a tuple of Axis (or int or str), animate each combination of labels of those axes.
Defaults to None.
- anim_params: dict, optional
Optional parameters to control how animations are saved to file. - writer : str, optional
Backend to use. Defaults to ‘pillow’ for images (.gif .png and .tiff), ‘ffmpeg’ otherwise.
- fpsint, optional
Animation frame rate (per second). Defaults to 5.
- metadatadict, optional
Dictionary of metadata to include in the output file. Some keys that may be of use include: title, artist, genre, subject, copyright, srcform, comment. Defaults to {}.
- bitrateint, optional
The bitrate of the movie, in kilobits per second. Higher values means higher quality movies, but increase the file size. A value of -1 lets the underlying movie encoder select the bitrate.
- **kwargskeywords
Options to pass to matplotlib plotting method
- Returns
- axesmatplotlib.AxesSubplot or np.array of them
Notes
See Pandas documentation of plot function for more details on this subject
Examples
Let us first define an array with some made up data
>>> import larray as la >>> arr = la.Array([[5, 20, 5, 10], ... [6, 16, 8, 11]], 'gender=M,F;year=2018..2021')
Simple line plot
>>> arr.plot() <Axes: xlabel='year'>
Line plot with grid and a title, saved in a file
>>> arr.plot(grid=True, title='line plot', filepath='my_file.png')
2 bar plots (one for each gender) sharing the same y axis, which makes sub plots easier to compare. By default sub plots are independant of each other and the axes ranges are computed to “fit” just the data for their individual plot.
>>> arr.plot.bar(subplots='gender', sharey=True) array([<Axes: title={'center': 'M'}, xlabel='year'>, <Axes: title={'center': 'F'}, xlabel='year'>], dtype=object)
A stacked bar plot (genders are stacked)
>>> arr.plot.bar(stack='gender') <Axes: xlabel='year'>
An animated bar chart (with two bars). We set explicit y bounds via ylim so that the same boundaries are used for the whole animation.
>>> arr.plot.bar(animate='year', ylim=(0, 22), filepath='myanim.avi')
Create a figure containing 2 x 2 graphs
>>> import matplotlib.pyplot as plt >>> # see matplotlib.pyplot.subplots documentation for more details >>> fig, ax = plt.subplots(2, 2, figsize=(10, 8), tight_layout=True) >>> # line plot with 2 curves (Males and Females) in the top left corner (0, 0) >>> arr.plot(ax=ax[0, 0], title='line plot') <Axes: title={'center': 'line plot'}, xlabel='year'> >>> # bar plot with stacked values in the top right corner (0, 1) >>> arr.plot.bar(ax=ax[0, 1], stack='gender', title='stacked bar plot') <Axes: title={'center': 'stacked bar plot'}, xlabel='year'> >>> # area plot in the bottom left corner (1, 0) >>> arr.plot.area(ax=ax[1, 0], title='area plot') <Axes: title={'center': 'area plot'}, xlabel='year'> >>> # scatter plot in the bottom right corner (1, 1), using the year as color >>> # index and a specific colormap >>> arr.plot.scatter(ax=ax[1, 1], x='M', y='F', c=arr.year, colormap='viridis', ... title='scatter plot') <Axes: title={'center': 'scatter plot'}, xlabel='M', ylabel='F'> >>> plt.show()