![]() I chose the "tab10" discrete (aka qualitative) colormap here. Plt.legend(handles=handles, title='Species') Levels, categories = pd.factorize(iris)Ĭolors = # using the "tab10" colormap To choose your own colormap and add a legend, the simplest approach is this: import matplotlib.patches This creates a plot without a legend, using the default "viridis" colormap. Plt.gca().set(xlabel='Petal Width', ylabel='Petal Length', title='Petal Width vs Length') Plt.scatter(iris, iris, c=pd.factorize(iris)) The easiest way is to simply pass an array of integer category levels to the plt.scatter() color parameter. Plt.scatter(x, y, s=15, color=scalarMap.to_rgba(i), label=uniq)Įdit: explicitly add labels for the legend. ScalarMap = cmx.ScalarMappable(norm=cNorm, cmap=hot) # Set the color map to match the number of speciesĬNorm = colors.Normalize(vmin=0, vmax=len(uniq)) There is probably a more elegant way, but one implementation would be the following (I used the following dataset: ): import matplotlib.pyplot as plt You can pass either colors, or an array of values that it will interpret as colors itself. ValueError: to_rgba: Invalid rgba arg "Iris-setosa"Ĭould not convert string to float: iris-setosaĪs your traceback tells you, you can't pass a string to the color parameter. > 376 'to_rgba: Invalid rgba arg "%s"\n%s' % (str(arg), exc))ģ78 def to_rgba_array(self, c, alpha=None): Users/mpgartland1/anaconda/lib/python2.7/site-packages/matplotlib/colors.pyc in to_rgba(self, arg, alpha)ģ74 except (TypeError, ValueError) as exc: Users/mpgartland1/anaconda/lib/python2.7/site-packages/matplotlib/colors.pyc in to_rgba_array(self, c, alpha)Ĥ20 result = np.zeros((nc, 4), dtype=np.float) > 3607 colors = _rgba_array(c, alpha)ģ609 # The inherent ambiguity is resolved in favor of color Users/mpgartland1/anaconda/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, **kwargs) ![]() > 3200 linewidths=linewidths, verts=verts, **kwargs) Users/mpgartland1/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.pyc in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, hold, **kwargs)ģ198 ret = ax.scatter(x, y, s=s, c=c, marker=marker, cmap=cmap, norm=norm, ValueError Traceback (most recent call last) To use the iris dataset- here is the code I think I would use: #Scatter of Petalīut I get an error that: could not convert string to float: iris-setosaĭo I have to change the categorical variable to a numeric one before I run, or is there something I can do with the data in its current format? The categorical variable is in a string form. ![]() But I want to change the color of the marker based on a third categorical variable. I have a basic scatter where the x and y are float.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |