user3598726 user3598726 - 5 months ago 22
Python Question

Adjusting matplotlib colormap to show simulation

I'm building a little simulation based on this experiment: , which shows the evolution of a bacteria over time.

I have initialized some bacteria with random resistance at the edge of a petri dish:

enter image description here

As the bacteria spreads, the contrast from the non occupied space is less and less, after a 100 generation it becomes like this:
enter image description here

I use plt.matshow(Map) to make the pictures, where Map is a 2D numpy array, with bacteria resistance values (always bigger than 0), the non occupied space is represented as 0. I tried different colormaps, but it did not help. My aim is to make the non occupied space some constant dark color,
the infected space some light color, with conrtast to notice the different resistance values.

Can you help me with that?


I was able to make the visulaization better, using logaritmic normalization: Let's say my bacteriaMap has really different resistancevalues:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

a = rand(size, size) * 0.2
b = rand(size, size) * 2
c = rand(size, size) * 20
sumMap = np.concatenate((a,b,c), axis=1)

sumMap is the numpy array to plot, with small, medium and big numbers. Building up colormap and normalizer:

colormap = plt.get_cmap('magma')
norm = mpl.colors.LogNorm(vmax=maxval, vmin=minval)

If I use the normalization the map loks like this:

img = plt.pcolormesh(sumh, cmap=colormap,  norm=norm)

enter image description here

If I don't:

img = plt.pcolormesh(sumh, cmap=cmap,  )

enter image description here