0

Please see code below. I am trying to get 2 exact same scatter plots, in ax2 I try to set the color after the scatter plot has been created. How can I achieve this?

I want this because in my interface I am trying to have the user (optionally) select data to color the scatterplot with. I could just redo the whole plot but I am guessing for large number of data points, it is better add colors to existing axis object. Is that correct?

import numpy as np
import matplotlib.pyplot as plt

fig, (ax1,ax2) = plt.subplots(1,2)
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)

#in ax1, I set color using the 'c' argument
ax1.scatter(x,y, c=z)
sc = ax2.scatter(x,y)
#in ax2, I try to mimic the 'c'  argument with set_color but it raises error
# sc.set_color(z)
user2677285
  • 313
  • 1
  • 7
  • Do you mean you want your user to optionally select color of first plot or second plot? – Henry Yik Oct 17 '19 at 03:22
  • For the second plot. It is a dropdown menu, so each time the user selects a property (a pandas dataframe column name) I want to color the existing plot using the values of the selected column – user2677285 Oct 17 '19 at 03:59
  • The problem is in your original plot you are passing a `np.array` to `c`, which maps your array to colors using `cmap` and `norm`. If you pass one color or a list of colors to `set_color`, it will work just fine. – Henry Yik Oct 17 '19 at 04:10
  • 1
    Check [how-to-animate-a-scatter-plot](https://stackoverflow.com/questions/9401658/how-to-animate-a-scatter-plot) – ImportanceOfBeingErnest Oct 17 '19 at 11:44

0 Answers0