aerosandbox.tools.pretty_plots.plots
#
Submodules#
Package Contents#
Functions#
|
|
|
An analogue for plt.contour and plt.tricontour and friends that produces a much prettier default graph. |
|
|
|
Plots a curve that interpolates a 2D dataset. Same as matplotlib.pyplot.plot(), with the following changes: |
|
Attributes#
- aerosandbox.tools.pretty_plots.plots.eng_string(x, unit='', format='%.3g', si=True, add_space_after_number=None)[source]#
-
Returns float/int value <x> formatted in a simplified engineering format - using an exponent that is a multiple of 3.
- Parameters:
x (float) – The value to be formatted. Float or int.
unit (str) – A unit of the quantity to be expressed, given as a string. Example: Newtons -> “N”
format – A printf-style string used to format the value before the exponent.
si – if true, use SI suffix for exponent. (k instead of e3, n instead of e-9, etc.)
add_space_after_number (bool) –
- Return type:
str
Examples:
- With format=’%.2f’:
- 1.23e-08 -> 12.30e-9
123 -> 123.00
1230.0 -> 1.23e3
-1230000.0 -> -1.23e6
- With si=True:
1230.0 -> “1.23k”
-1230000.0 -> “-1.23M”
- With unit=”N” and si=True:
1230.0 -> “1.23 kN”
-1230000.0 -> “-1.23 MN”
- aerosandbox.tools.pretty_plots.plots.contour(*args, levels=31, colorbar=True, linelabels=True, cmap=None, alpha=0.7, extend='neither', linecolor='k', linewidths=0.5, extendrect=True, linelabels_format=eng_string, linelabels_fontsize=8, max_side_length_nondim=np.inf, colorbar_label=None, x_log_scale=False, y_log_scale=False, z_log_scale=False, mask=None, drop_nans=None, contour_kwargs=None, contourf_kwargs=None, colorbar_kwargs=None, linelabels_kwargs=None, **kwargs)[source]#
An analogue for plt.contour and plt.tricontour and friends that produces a much prettier default graph.
Can take inputs with either contour or tricontour syntax.
- See syntax here:
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.contour.html https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.contourf.html https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.tricontour.html https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.tricontourf.html
- Parameters:
X – If dataset is gridded, follow contour syntax. Otherwise, follow tricontour syntax.
Y – If dataset is gridded, follow contour syntax. Otherwise, follow tricontour syntax.
Z – If dataset is gridded, follow contour syntax. Otherwise, follow tricontour syntax.
levels (Union[int, List, numpy.ndarray]) – See contour docs.
colorbar (bool) – Should we draw a colorbar?
linelabels (bool) – Should we add line labels?
cmap – What colormap should we use?
alpha (float) – What transparency should all plot elements be?
extend (str) – See contour docs.
linecolor – What color should the line labels be?
linewidths (float) – See contour docs.
extendrect (bool) – See colorbar docs.
linelabels_format (Union[str, Callable[[float], str]]) – See ax.clabel docs.
linelabels_fontsize (float) – See ax.clabel docs.
contour_kwargs (Dict) – Additional keyword arguments for contour.
contourf_kwargs (Dict) – Additional keyword arguments for contourf.
colorbar_kwargs (Dict) – Additional keyword arguments for colorbar.
linelabels_kwargs (Dict) – Additional keyword arguments for the line labels (ax.clabel).
**kwargs – Additional keywords, which are passed to both contour and contourf.
max_side_length_nondim (float) –
colorbar_label (str) –
x_log_scale (bool) –
y_log_scale (bool) –
z_log_scale (bool) –
mask (numpy.ndarray) –
drop_nans (bool) –
Returns: A tuple of (contour, contourf, colorbar) objects.
- aerosandbox.tools.pretty_plots.plots.eng_string(x, unit='', format='%.3g', si=True, add_space_after_number=None)[source]#
-
Returns float/int value <x> formatted in a simplified engineering format - using an exponent that is a multiple of 3.
- Parameters:
x (float) – The value to be formatted. Float or int.
unit (str) – A unit of the quantity to be expressed, given as a string. Example: Newtons -> “N”
format – A printf-style string used to format the value before the exponent.
si – if true, use SI suffix for exponent. (k instead of e3, n instead of e-9, etc.)
add_space_after_number (bool) –
- Return type:
str
Examples:
- With format=’%.2f’:
- 1.23e-08 -> 12.30e-9
123 -> 123.00
1230.0 -> 1.23e3
-1230000.0 -> -1.23e6
- With si=True:
1230.0 -> “1.23k”
-1230000.0 -> “-1.23M”
- With unit=”N” and si=True:
1230.0 -> “1.23 kN”
-1230000.0 -> “-1.23 MN”
- aerosandbox.tools.pretty_plots.plots.pie(values, names, colors=None, label_format=lambda name, value, percentage: ..., sort_by=None, startangle=0.0, center_text=None, x_labels=1.25, y_max_labels=1.3, arm_length=20, arm_radius=5)#
- Parameters:
values (Union[numpy.ndarray, List[float]]) –
names (List[str]) –
colors (Union[numpy.ndarray, List[str]]) –
label_format (Callable[[str, float, float], str]) –
sort_by (Union[numpy.ndarray, List[float], str, None]) –
startangle (float) –
center_text (str) –
x_labels (float) –
y_max_labels (float) –
- aerosandbox.tools.pretty_plots.plots.plot_color_by_value(x, y, *args, c, cmap='turbo', colorbar=False, colorbar_label=None, clim=None, **kwargs)#
Uses same syntax as matplotlib.pyplot.plot, except that c is now an array-like that maps to a specific color pulled from cmap. Makes lines that are multicolored based on this c value.
- Parameters:
x (numpy.ndarray) – Array of x-points.
y (numpy.ndarray) – Array of y-points.
*args – Args that will be passed into matplotlib.pyplot.plot(). Example: “.-” for a dotted line.
c (numpy.ndarray) – Array of values that will map to colors. Must be the same length as x and y.
cmap – The colormap to use.
colorbar (bool) – Whether or not to display the colormap. [bool]
colorbar_label (str) – The label to add to the colorbar. Only applies if the colorbar is created. [str]
clim (Tuple[float, float]) – A tuple of (min, max) that assigns bounds to the colormap. Computed from the range of c if not given.
**kwargs – Kwargs that will be passed into matplotlib.pyplot.plot()
Returns:
- aerosandbox.tools.pretty_plots.plots.plot_smooth(*args, color=None, label=None, function_of=None, resample_resolution=500, drop_nans=False, **kwargs)[source]#
- Plots a curve that interpolates a 2D dataset. Same as matplotlib.pyplot.plot(), with the following changes:
uses B-splines to draw a smooth curve rather than a jagged polyline
By default, plots in line format fmt=’.-’ rather than fmt=’-’.
Other than that, almost all matplotlib.pyplot.plot() syntax can be used. See syntax here: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html
- Example usage:
>>> import aerosandbox.numpy as np >>> >>> t = np.linspace(0, 1, 12) # Parametric variable >>> x = np.cos(2 * np.pi * t) >>> y = np.cos(2 * np.pi * t ** 4) - t >>> >>> plot_smooth( >>> x, y, 'o--', color='purple' >>> ) >>> plt.show()
Note: a true 2D interpolation is performed - it is not assumed y is a function of x, or vice versa. This can,
in rare cases, cause single-valuedness to not be preserved in cases where it logically should. If this is the case, you need to perform the interpolation yourself without plot_smooth().
- Parameters:
*args –
Same arguments as matplotlib.pyplot.plot(). Notes on standard plot() syntax:
Call signatures: >>> plot([x], y, [fmt], *, data=None, **kwargs) >>> plot([x], y, [fmt], [x2], y2, [fmt2], …, **kwargs)
Examples: >>> 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
color – Specifies the color of any line and/or markers that are plotted (as determined by the fmt).
label – Attaches a label to this line. Use plt.legend() to display.
resample_resolution (int) – The number of points to use when resampling the interpolated curve.
**kwargs – Same keyword arguments as matplotlib.pyplot.plot().
function_of (str) –
drop_nans (bool) –
- Return type:
Tuple[aerosandbox.numpy.ndarray, aerosandbox.numpy.ndarray]
Returns: A tuple (x, y) of the resampled points on the interpolated curve. Both x and y are 1D ndarrays.
- aerosandbox.tools.pretty_plots.plots.plot_with_bootstrapped_uncertainty(x, y, ci=0.95, x_stdev=0.0, y_stdev=None, color=None, draw_data=True, label_line='Best Estimate', label_ci=True, label_data='Raw Data', line_alpha=0.9, ci_to_alpha_mapping=lambda ci: ..., n_bootstraps=2000, n_fit_points=500, spline_degree=3, normalize=True, x_log_scale=False, y_log_scale=False)[source]#
- Parameters:
x (numpy.ndarray) –
y (numpy.ndarray) –
ci (Optional[Union[float, Iterable[float], numpy.ndarray]]) –
x_stdev (Union[None, float]) –
y_stdev (Union[None, float]) –
color (Optional[Union[str, Tuple[float]]]) –
draw_data (bool) –
label_line (Union[bool, str]) –
label_ci (bool) –
label_data (Union[bool, str]) –
line_alpha (float) –
ci_to_alpha_mapping (Callable[[float], float]) –
normalize (bool) –
x_log_scale (bool) –
y_log_scale (bool) –