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()
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)
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
Moving Average or Running Mean
Live Output from Subprocess Command
What Should I Do with "Unexpected Indent" in Python
Why Can't I Use a List as a Dict Key in Python
How to Redirect Output with Subprocess in Python
Pythonic Way to Print List Items
How to Convert a String with Dot and Comma into a Float in Python
Why Is Button Parameter "Command" Executed When Declared
What Is the Most Efficient Way to Loop Through Dataframes with Pandas
Matplotlib: How to Create Axessubplot Objects, Then Add Them to a Figure Instance
Group by in Group by and Average
Converting a String Representation of a List into an Actual List Object
After Conda Update, Python Kernel Crashes When Matplotlib Is Used
Extracting Extension from Filename in Python
How to Pass Another Entire Column as Argument to Pandas Fillna()