The main aim of mplutils is to help in the daily work with matplotlib. The motivation to create the library is to help in some tasks that should be easier if you use them on a daily basis and to provide some information that sometimes is not easy to find/understand (at least for me).
The library is divided in diferent sections related with main topics of Matplotlib usage.
colors: Some tools to work with colors and to provide best practices.text: Very simple tools to inspect text features.figure: Transformations applied to a Figure instance.axes: Transformations applied to an Axes instance.charts: Some best practices for some chart types as well as some functions to create not so frequent charts.
In [1]:
%cd ..
In [2]:
import numpy as np
import matplotlib.pyplot as plt
import mplutils as mplu
%matplotlib inline
In [3]:
mplu.text.text_getinfo_availablesystemfonts()[0:10]
Out[3]:
In [4]:
print(mplu.colors.colors_mpl_cnames())
In a notebook you could use a richer display using it like:
In [5]:
mplu.colors.colors_mpl_cnames()
Out[5]:
If you only want colors related with a string you could use a string in the colors_mpl_cnames call and it will show only those aliases including the string used:
In [6]:
mplu.colors.colors_mpl_cnames('yellow')
Out[6]:
In [10]:
np.random.seed(1)
fig, ax = plt.subplots(figsize = (10, 2))
x, y = np.arange(-3, 7), np.random.rand(10)*5-5/2
ax.plot(x, y, color = 'cyan', lw = 20)
ax.plot(x, y - 1, color = 'lightblue', lw = 20)
ax.plot(x, y - 2, color = 'pink', lw = 20)
Out[10]:
Now, let's check how the image would be in a grayscale colormap.
There are three possibilities to change from color to grayscale:
luminosity: It uses the info found at https://en.wikipedia.org/wiki/Grayscale#Colorimetric_.28luminance-preserving.29_conversion_to_grayscale
In [11]:
mplu.colors.colors_check_grayscale(fig, 'luminosity')
luma: It uses the info found at https://en.wikipedia.org/wiki/Grayscale#Luma_coding_in_video_systems
In [12]:
mplu.colors.colors_check_grayscale(fig, 'luma')
average: the grayscale is obtained using the average value between the Reg, Green and Blue channels.
In [13]:
mplu.colors.colors_check_grayscale(fig, 'average')
Warning: This functionality creates a hardcopy of the original image. If you want to save the grayscale image created on-the-fly you could use the filename keyword.
mplu.colors.colors_check_grayscale(fig, 'average', filename = 'sample.png') will create a file called sample.png on the actual folder.
In order to provide some better defaults (this is very subjective) the function axes_set_better_defaults has been included. It does some very basic stuff:
For instance, the following figure:
In [11]:
fig, ax = plt.subplots(facecolor = 'lightgray')
ax.grid(True)
ax.plot([1,2,3])
Out[11]:
Would be like this one in case we apply axes_set_better_defaults to an Axes instance
In [12]:
fig, ax = plt.subplots(facecolor = 'lightgray')
ax.grid(True)
mplu.axes.axes_set_better_defaults(ax)
ax.plot([1,2,3])
Out[12]:
In [15]:
fig, (ax1, ax2) = plt.subplots(ncols = 2, figsize = (12, 6))
ax1.plot([1,2,3])
ax1.grid(True)
mplu.axes.axes_set_better_defaults(ax2)
ax2.plot([1,2,3])
Out[15]:
The function axes_set_axis provides an easy way to manage the axis, their visibility, if the axis should show some pan,...
In [5]:
fig, (ax1, ax2, ax3) = plt.subplots(nrows = 3, figsize = (5, 15))
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y = np.sin(x)
ax1.plot(x, y)
ax1.grid(True)
ax1.set_title('Default with grid')
mplu.axes.axes_set_axis(ax2, spines = ['bottom', 'left'], pan = 20)
ax2.plot(x, y)
ax2.set_title('Axis modified')
mplu.axes.axes_set_axis(ax3, spines = ['bottom', 'left'], pan = 20)
mplu.axes.axes_set_better_defaults(ax3, axes_color = '#666666')
ax3.plot(x, y)
ax3.set_title('Axis modified and better defaults', color = '#666666')
fig.tight_layout()
The following code will center the origin of the axis on (0,0):
In [6]:
fig, ax = plt.subplots()
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y = np.sin(x)
mplu.axes.axes_set_origin(ax)
ax.plot(x, y, lw = 2)
Out[6]:
The previous example using also 'better defaults':
In [7]:
fig, ax = plt.subplots()
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y = np.sin(x)
mplu.axes.axes_set_origin(ax)
mplu.axes.axes_set_better_defaults(ax)
ax.plot(x, y, lw = 2)
Out[7]:
If you want the axis centered on a special location you can use the x, y keyword parameters:
In [8]:
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows = 2, ncols = 2, figsize = (10, 10))
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y = np.sin(x)
mplu.axes.axes_set_origin(ax1)
ax1.plot(x, y, lw = 2)
mplu.axes.axes_set_origin(ax2, x = 0, y = x.min())
ax2.plot(x, y, lw = 2)
mplu.axes.axes_set_origin(ax3, x = y.max(), y = x.min())
ax3.plot(x, y, lw = 2)
mplu.axes.axes_set_origin(ax4, x = 0, y = x.max() + 1)
ax4.plot(x, y, lw = 2)
fig.tight_layout()
In some of the previous subplots the tick labels are not well located. You can use the xticks_position or the yticks_position to improve the layout.
In [12]:
fig, (ax3, ax4) = plt.subplots(nrows = 2, figsize = (10, 10))
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y = np.sin(x)
mplu.axes.axes_set_origin(ax3, x = y.max(), y = -4, xticks_position = 'top')
ax3.plot(x, y, lw = 2)
mplu.axes.axes_set_origin(ax4, x = 0, y = 4, yticks_position = 'right')
ax4.plot(x, y, lw = 2)
fig.tight_layout()
Finally, if you don't want tick labels you could use xticks_visible and/or yticks_visible keyword parameters to remove ticks and ticklabels:
In [14]:
fig, ax = plt.subplots()
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y = np.sin(x)
mplu.axes.axes_set_origin(ax, xticks_visible = False, yticks_visible = False)
mplu.axes.axes_set_better_defaults(ax)
ax.plot(x, y, lw = 2)
ax.set_ylim(-1.1, 1.1)
Out[14]:
This is a dummy wrapper around Axes.set_aspect method. It is included just to remember I should apply an equal aspect ratio to axis scales in some situations.
For instance, in a linear relation sometimes it could be misleading to show the plot without aspect ratio. In this example one can infer the y variation in relation to x would be higher than it is:
In [3]:
np.random.seed(5)
x = np.random.rand(200) * 10
y = x / 4 + np.random.rand(200) - 0.5
fig, ax = plt.subplots()
ax.scatter(x, y)
Out[3]:
With equal axis aspect ratio:
In [3]:
np.random.seed(5)
x = np.random.rand(200) * 10
y = x / 4 + np.random.rand(200) - 0.5
fig, ax = plt.subplots()
mplu.axes.axes_set_aspect_ratio(ax, adjustable = 'datalim')
ax.scatter(x, y)
Out[3]:
In [ ]: