I'm using python to simulate some automation models, and with the help of matplotlib I'm producing plots like the one shown below.
I'm currently plotting with the following command:
ax.imshow(self.g, cmap=map, interpolation='nearest')
where self.g
is the binary map (0
-> blue, 1
-> red in my current plots).
However, to include this in my report I would like the plot to be with black dots on white background instead of red on blue. How do I accomplish that?
Subplots mean groups of axes that can exist in a single matplotlib figure. subplots() function in the matplotlib library, helps in creating multiple layouts of subplots. It provides control over all the individual plots that are created.
cmap stands for colormap and it's a colormap instance or registered colormap name (cmap will only work if c is an array of floats). Matplotlib colormaps are divided into the following categories: sequential, diverging, and qualitative.
Figure size, aspect ratio and DPI Matplotlib allows the aspect ratio, DPI and figure size to be specified when the Figure object is created, using the figsize and dpi keyword arguments. figsize is a tuple of the width and height of the figure in inches, and dpi is the dots-per-inch (pixel per inch).
You can change the color map you are using via the cmap
keyword. The color map 'Greys'
provides the effect you want. You can find a list of available maps on the scipy website.
import matplotlib.pyplot as plt import numpy as np np.random.seed(101) g = np.floor(np.random.random((100, 100)) + .5) plt.subplot(211) plt.imshow(g) plt.subplot(212) plt.imshow(g, cmap='Greys', interpolation='nearest') plt.savefig('blkwht.png') plt.show()
which results in:
There is an alternative method to Yann's answer that gives you finer control. Matplotlib's imshow can take a MxNx3
matrix where each entry is the RGB color value - just set them to white [1,1,1]
or black [0,0,0]
accordingly. If you want three colors it's easy to expand this method.
import matplotlib.pyplot as plt import numpy as np # Z is your data set N = 100 Z = np.random.random((N,N)) # G is a NxNx3 matrix G = np.zeros((N,N,3)) # Where we set the RGB for each pixel G[Z>0.5] = [1,1,1] G[Z<0.5] = [0,0,0] plt.imshow(G,interpolation='nearest') plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With