Matplotlib Scatterplot; Color as a Function of a Third Variable

coloring matplotlib scatterplot by third variable with log color bar

After some tinkering, and using information learned from user4421975's answer and the link in the comments, I've puzzled it out. In short, I used plt.scatter's norm feature/attribute/thingie to mess with the colors and make them logarithmic.

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(1)

time = 2

x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]

multi_array = np.asarray([[1, 1, 10, 100, 1000], [10000, 1000, 100, 10, 1], [300, 400, 5000, 12, 47]])

for counter in np.arange(0, 5):
s = plt.scatter(x[counter], y[counter], c = multi_array[time, counter], cmap = 'winter', norm = matplotlib.colors.LogNorm(vmin=multi_array[time].min(), vmax=multi_array[time].max()), marker = 's', )

cb = plt.colorbar(s)
cb.set_label('Log of Data')

plt.show()

matplotlib scatter plot colour as function of third and fourth variable

Expanding on falsetru's answer, you could make a two-dimensional colorbar using imshow:

import numpy as np
from matplotlib import pyplot as plt
x = np.random.random(50)
y = np.random.random(50)
a = np.random.random(50)
b = np.random.random(50)
s = c=zip(a, b, np.zeros(50))
ax = plt.gca()
print ax.get_position()
plt.scatter(x,y,c = s)
#adjust limits to make room for inset axes
plt.xlim(xmax = 1.5)
plt.ylim(ymax = 1.5)
#create inset axes
ax = plt.axes([.7, .675, .2, .2], axisbg='y')
n = 20
red = np.linspace(min(a), max(a), n)
green = np.linspace(min(b), max(b), n)
floats = np.linspace(0, 1, n)
#make arrays of all possible values between 0 and 1
X, Y = np.meshgrid(floats, floats)
#stack the arrays with a third array of zeros
Z = np.dstack((X, Y, np.zeros(X.shape)))
Z = np.rot90(Z)
red = np.round(red[::-1], 2)
green = np.round(green, 2)
plt.imshow(Z)
ticks = np.arange(0, n, 6)
plt.yticks(ticks, [red[i] for i in ticks])
plt.xticks(ticks, [green[i] for i in ticks], rotation = 'vertical')
plt.xlabel('Green')
plt.ylabel('Red')
plt.show()

two-dimensional colorbar

Matplotlib Scatterplot / Color as function of third variable / Symmetric Log Colorscale

The documentation for SymLogNorm is not particularly clear, as a result I am not confident everything I say in this answer is correct. It seems the vmin and vmax arguments should be used to determine the range of data your consider e.g:

# loading modules
import matplotlib as mpl
import matplotlib.pyplot as plt

# defining variables
x=[0,1,2,3]
y=[0,1,2,3]
c=[-1000,-100,100,1000]

# making scatterplot
plt.scatter(x, y, c=c, s=100, norm=mpl.colors.SymLogNorm(linthresh=10, vmin=-1e3, vmax=1e3))
plt.colorbar(ticks=c)

Sample Image

The colorbar ticks are then not going to know that it is log scaled but I think this is the effect you were aiming for.



Related Topics



Leave a reply



Submit