Matplotlib 3d scatter _facecolors3d not working - python

I am trying to save a 3d scatter plot animation where points appear one at a time. I made the animation work, but when I set the face colors of the points they do not take effect and all points appear blue. When I use the same color array but on static image, colors work well.
Animation Code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation
import random
import seaborn as sns
import pandas as pd
import json
import os
from matplotlib.animation import FuncAnimation
import mpl_toolkits.mplot3d.axes3d as p3
from matplotlib import rc
from IPython.display import HTML
from itertools import product
x=[]
y=[]
for i in range(-80, 80, 10):
x.append(i)
y.append(i)
combs = list(product(x,y))
def obj(x, y):
global HISTORY
e = 2.718
res = 7*x*y/(e**(0.001*x**2 + 0.001*y**2))
return res
z = [obj(x,y) for x, y in combs]
x = [obj[0] for obj in combs]
y = [obj[1] for obj in combs]
data = [[x[i],y[i],z[i]] for i in range(len(x))]
cmap = sns.cubehelix_palette(as_cmap=True)
m = max(z) # Get the worst score so we can use it as the darkest area of the plot.
face_colors = np.array([cmap(i/m) for i in z]) # Map all of the values with cmap colors.
df = pd.DataFrame(data, columns=["x","y","z"])
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
sc = ax.scatter([],[],[], alpha=0.5)
def update(i):
sc._offsets3d = (df.x.values[:i], df.y.values[:i], df.z.values[:i])
sc._facecolors3d = face_colors[:i]
sc._facecolors2d=sc._facecolors3d
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_xlim(min(x),max(x))
ax.set_ylim(min(y),max(y))
ax.set_zlim(min(z),max(z))
ani = matplotlib.animation.FuncAnimation(fig, update, frames=len(df), interval=70)
HTML(ani.to_html5_video())
When I do not use the animation and just call plt.scatter like this:
sc = ax.scatter(df.x.values,df.y.values,df.z.values, facecolors=face_colors)
My image works well:
How can I keep these colors in my animation as well?
Code for static image:
x=[]
y=[]
for i in range(-80, 80, 10):
x.append(i)
y.append(i)
combs = list(product(x,y))
def obj(x, y):
global HISTORY
e = 2.718
res = 7*x*y/(e**(0.001*x**2 + 0.001*y**2))
return res
z = [obj(x,y) for x, y in combs]
x = [obj[0] for obj in combs]
y = [obj[1] for obj in combs]
data = [[x[i],y[i],z[i]] for i in range(len(x))]
cmap = sns.cubehelix_palette(as_cmap=True)
m = max(z) # Get the worst score so we can use it as the darkest area of the plot.
face_colors = [cmap(i/m) for i in z] # Map all of the values with cmap colors.
df = pd.DataFrame(data, columns=["x","y","z"])
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
sc = ax.scatter(df.x.values,df.y.values,df.z.values, facecolors=face_colors)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_xlim(min(x),max(x))
ax.set_ylim(min(y),max(y))
ax.set_zlim(min(z),max(z))
plt.show()

Might just be a typo. _facecolor3d instead of _facecolors3d try this:
def update(i):
sc._offsets3d = (df.x.values[:i], df.y.values[:i], df.z.values[:i])
sc._facecolor3d = face_colors[:i]
sc._edgecolor3d = face_colors[:i]

Related

Scale y-axis for really small numbers

I'm trying to scale the y-axis so my errorbars can be seen.
Any help would be appreciated! :)
Here is my current code.
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline
x = ntermsList
y = allPmuCycleCountAverages
xerr = 0
yerr = allPmuCycleCountStandardDeviations
fig, ax = plt.subplots()
ax.errorbar(x, y, xerr=xerr, yerr=yerr,fmt='-o')
ax.set_xlabel('x-axis')
ax.set_ylabel('y-axis')
ax.set_title('Line plot with error bars')
ax.set_xticks(ntermsList)
ax.set_xticklabels(ntermsList)
ax.set_yticks(allPmuCycleCountAverages)
ax.yaxis.grid(True)
plt.show()
I've tried these solutions, but no joy:
plt.ylim(-1, 1)
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
plt.yticks(np.arange(min(y), max(y)+0.5, 0.01))
I was expecting the y-axis scale to zoom close enough to the points so my errorbars could be seen
Try autoscalling based in y ticks. Here I'm adding some logic that just rescales the y-axis based on the data that is in the visible x-region. As I don't have your data I took random data.
import numpy as np
import random
ntermsList = np.random.randint(low=0, high=10, size=(555,))
allPmuCycleCountAverages = np.random.randint(low=0, high=10, size=(555,))
allPmuCycleCountStandardDeviations = np.random.randint(low=0, high=10, size=(555,))
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline
x = ntermsList
y = allPmuCycleCountAverages
xerr = 0
yerr = allPmuCycleCountStandardDeviations
fig, ax = plt.subplots()
ax.errorbar(x, y, xerr=xerr, yerr=yerr,fmt='-o')
ax.set_xlabel('x-axis')
ax.set_ylabel('y-axis')
ax.set_title('Line plot with error bars')
ax.set_xticks(ntermsList)
ax.set_xticklabels(ntermsList)
ax.set_yticks(allPmuCycleCountAverages)
#plt.setp(ax.get_yticklabels(), rotation=90, horizontalalignment='right')
ax.yaxis.grid(True)
margin =0.1
def get_bottom_top(line):
xd = line.get_xdata()
yd = line.get_ydata()
lo,hi = ax.get_xlim()
y_displayed = yd[((xd>lo) & (xd<hi))]
h = np.max(y_displayed) - np.min(y_displayed)
bot = np.min(y_displayed)-margin*h
top = np.max(y_displayed)+margin*h
return bot,top
lines = ax.get_lines()
bot,top = np.inf, -np.inf
for line in lines:
new_bot, new_top = get_bottom_top(line)
if new_bot < bot: bot = new_bot
if new_top > top: top = new_top
ax.set_ylim(bot,top)
plt.show()
Before Rescalling
After rescalling

Add a vertical label to matplotlib colormap legend

This code enables me to plot a colormap of a "3d" array [X,Y,Z] (they are 3 simple np.array of elements). But I can't succeed in adding a vertical written label at the right of the colorbar legend.
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure("Color MAP 2D+")
contour = plt.tricontourf(X, Y, Z, 100, cmap="bwr")
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Color MAP 2D+")
#Legend
def fmt(x, pos):
a, b = '{:.2e}'.format(x).split('e')
b = int(b)
return r'${} \times 10^{{{}}}$'.format(a, b)
import matplotlib.ticker as ticker
plt.colorbar(contour, format=ticker.FuncFormatter(fmt))
plt.show()
It's anoying to not get an easy answer from google... can someone help me ?
You are looking to add a label to the colorbar object. Thankfully, colorbar has a set_label function.
in short:
cbar = plt.colorbar(contour, format=ticker.FuncFormatter(fmt))
cbar.set_label('your label here')
In a minimal script:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
X = np.random.uniform(-2, 2, 200)
Y = np.random.uniform(-2, 2, 200)
Z = X*np.exp(-X**2 - Y**2)
contour = plt.tricontourf(X, Y, Z, 100, cmap="bwr")
def fmt(x, pos):
a, b = '{:.2e}'.format(x).split('e')
b = int(b)
return r'${} \times 10^{{{}}}$'.format(a, b)
cbar = plt.colorbar(contour, format=ticker.FuncFormatter(fmt))
cbar.set_label('your label here')
plt.show()
I believe your code is working. See this example:
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
iris = datasets.load_iris().data
X = iris[:,0]
Y = iris[:,1]
Z = iris[:,2]
fig = plt.figure("Color MAP 2D+")
contour = plt.tricontourf(X, Y, Z, 100, cmap="bwr")
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Color MAP 2D+")
#Legend
def fmt(x, pos):
a, b = '{:.2e}'.format(x).split('e')
b = int(b)
return r'${} \times 10^{{{}}}$'.format(a, b)
import matplotlib.ticker as ticker
plt.colorbar(contour, format=ticker.FuncFormatter(fmt))
plt.show()
Output:

Change RGB color in matplotlib animation

I seems that it is not possible to change colors of a Matplotlib scatter plot through a RGB definition. Am I wrong?
Here is a code (already given in stack overflow) which work with colors indexed in float:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
def main():
numframes = 100
numpoints = 10
color_data = np.random.random((numframes, numpoints))
x, y, c = np.random.random((3, numpoints))
fig = plt.figure()
scat = plt.scatter(x, y, c=c, s=100)
ani = animation.FuncAnimation(fig, update_plot, frames=range(numframes),
fargs=(color_data, scat))
plt.show()
def update_plot(i, data, scat):
scat.set_array(data[i])
return scat,
main()
But if color_data is defined through RGB colors, I get an error:
ValueError: Collections can only map rank 1 arrays
The related code is the following (in this code, I just change the color of one sample each time):
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
def main():
numframes = 100
numpoints = 10
rgb_color_data = np.random.random((numpoints, 3))
x, y = np.random.random((2, numpoints))
fig = plt.figure()
scat = plt.scatter(x, y, c=rgb_color_data, s=100) #this work well at this level
ani = animation.FuncAnimation(fig, update_plot2, frames=range(numframes),
fargs=(rgb_color_data, scat))
plt.show()
def update_plot2(i,data,scat):
data[ i%10 ] = np.random.random((3))
scat.set_array(data) # this fails
return scat,
main()
Is there a means to use set_array with RGB color array?
Not sure what you are trying to achieve. But if you are trying to change the color, why not use the set_color() function of Collection?
def update_plot2(i,data,scat):
data[ i%10 ] = np.random.random((3))
scat.set_color(data) # <<<<<<<<<<<<<<<<<<<
return scat,

How to color parts of links in dendrograms using scipy in python?

I can color labels in Python dendrograms but I don't know how to color parts of the links belonging its labels.. I want to make something like this:
Is it possible in Python?
Here I color only labels:
import numpy as np
import matplotlib.pyplot as plt
import scipy.cluster.hierarchy as sc
dists = np.array([[0,2,1,4],[2,0,3,5],[1,3,0,6],[4,5,6,0]])
l = ['a','b','c','b']
Z = sc.linkage(dists, method='complete')
d = sc.dendrogram(Z, labels=l)
label_colors = {'a': 'r', 'b': 'g', 'c': 'm'}
ax = plt.gca()
xlbls = ax.get_xmajorticklabels()
for i in range(len(xlbls)):
xlbls[i].set_color(label_colors[xlbls[i].get_text()])
plt.show()
Not sure if it's possible to color part of an u-shape, however you can color it complete shapes with
something like
d = sc.dendrogram(Z, labels=l)
it = iter(map(label_colors.__getitem__, d['ivl'])[-2::-1])
def f(x):
return it.next()
d = sc.dendrogram(Z, labels=l, link_color_func=f)
ax = plt.gca()
xlbls = ax.get_xmajorticklabels()
for y in xlbls:
y.set_color(label_colors[y.get_text()])
In Python dendrogram you can not colour a half u-shape directly, but you can appoint colours to any node. This can be accomplished as below:
import numpy as np
import matplotlib.pyplot as plt
import scipy.cluster.hierarchy as sc
dists = np.array([[0,2,1,4],[2,0,3,5],[1,3,0,6],[4,5,6,0],[4,7,6,2]])
Z = sc.linkage(dists, method='complete')
num = len(dists)
color = ["b"]*(2*num-1) # initialize color list with blue
# define the color of a specific node
color[5]="g"
color[6]="r"
color[7]="y"
d = sc.dendrogram(Z,link_color_func=lambda x: color[x])
# add labels for nodes
coord = np.c_[np.array(d['icoord'])[:,1:3],np.array(d['dcoord'])[:,1]]
coord = coord[np.argsort(coord[:,2])]
for posi in coord:
x = 0.5 * sum(posi[0:2])
y = posi[2]
plt.plot(x, y, 'ro')
plt.annotate("%2i" % num, (x, y), xytext=(0, -8),
textcoords='offset points',
va='top', ha='center')
num = num+1
plt.show()
#~ plt.savefig("test.png")

Can python make a tile plot?

Can python (eg matplotlib) make a tile plot like the following, where color indicates the intensity at each data point? Thanks!
You only need all of that machinery if you want the mouse to report back the value of the data under your mouse. To generate the image all you really need is (doc):
plt.imshow(data, interpolation='nearest')
You can control the color mapping via the cmap keyword.
Here is an example taken from http://matplotlib.org/examples/api/image_zcoord.html:
"""
Show how to modify the coordinate formatter to report the image "z"
value of the nearest pixel given x and y
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
X = 10*np.random.rand(5,3)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(X, cmap=cm.jet, interpolation='nearest')
numrows, numcols = X.shape
def format_coord(x, y):
col = int(x+0.5)
row = int(y+0.5)
if col>=0 and col<numcols and row>=0 and row<numrows:
z = X[row,col]
return 'x=%1.4f, y=%1.4f, z=%1.4f'%(x, y, z)
else:
return 'x=%1.4f, y=%1.4f'%(x, y)
ax.format_coord = format_coord
plt.show()
You are looking for image_zcode The example given is:
"""
Show how to modify the coordinate formatter to report the image "z"
value of the nearest pixel given x and y
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
X = 10*np.random.rand(5,3)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(X, cmap=cm.jet, interpolation='nearest')
numrows, numcols = X.shape
def format_coord(x, y):
col = int(x+0.5)
row = int(y+0.5)
if col>=0 and col<numcols and row>=0 and row<numrows:
z = X[row,col]
return 'x=%1.4f, y=%1.4f, z=%1.4f'%(x, y, z)
else:
return 'x=%1.4f, y=%1.4f'%(x, y)
ax.format_coord = format_coord
plt.show()

Categories