0

I'm trying to set up the legends of each points in the scatter plot. My main problem is that the colors of each point do not match the color of what is in the legend. What am I doing wrong and how do I rectify it?

def scatter(self, indep, dep, labl):
   x = self.df_input[indep]
   y = self.df_input[dep]
   random = np.random.RandomState(0)
   colors = random.rand(len(labl)+1)

   fig = plt.figure()
   ax = fig.add_subplot(111)

   for leg in labl:
      ax.scatter(x, y, c=colors, cmap='gist_ncar', label=leg)

   ax.legend()
   ax.set_xlabel(indep)
   ax.set_ylabel(dep)
   ax.axis('tight')
   plt.show()
Nix
  • 17
  • 4
  • Can you explain what you want the result to look like? Is [this answer](https://stackoverflow.com/questions/49297599/why-doesnt-the-color-of-the-points-in-a-scatter-plot-match-the-color-of-the-poi/49301633#49301633) what you're after? – ImportanceOfBeingErnest Apr 09 '19 at 18:38
  • Further potentially relevant posts [1](https://stackoverflow.com/questions/45041149/matplotlib-scatterplot-legend-customize-handles-to-look-like-tiny-scatter-plots) [2](https://stackoverflow.com/questions/44595288/matplotlib-adding-legend-based-on-existing-color-series) [3](https://stackoverflow.com/questions/53525499/how-to-add-a-legend-to-scatterplot) [4](https://stackoverflow.com/questions/50654620/add-legend-to-scatter-plot-pca) [5](https://stackoverflow.com/questions/43812911/adding-second-legend-to-scatter-plot) – ImportanceOfBeingErnest Apr 09 '19 at 19:02
  • You only have one set of `x` and `y`, so I think you only want to make one plot. I don't think you need to loop over `labl` at all. You're using a colormap (`cmap`) to color your data points, so that's the relevant property. Instead of `plt.legend()`, try `plt.colorbar()`. Maybe that will make more sense to you. – Matt Hall Apr 10 '19 at 02:35
  • @ImportanceOfBeingErnest The results look like a scatter plot with different colored points and a label/legend of what these points represent. My main issue is that the colors for the label/legend does not match its corresponding point color in the scatter plot. The method accepts a string indep and dep variable and the list of labl or label. I've already set the dataframe earlier in the code. My expected output is a scatter plot with legends. – Nix Apr 10 '19 at 03:09

1 Answers1

0

It seems like you might be trying to plot groups in a dataframe. So something like this might work:

import matplotlib.pyplot as plt
import pandas as pd

data = [['a', 1, 3],
        ['a', 2, 2],
        ['b', 2, 4],
        ['b', 1, 5],
        ['b', 3, 5],
       ]

df = pd.DataFrame(data, columns=['cat', 'x', 'y'])

for name, group in df.groupby('cat'):
    plt.scatter(group.x, group.y, label=name)
plt.legend()

This produces:

Sample plot

Matt Hall
  • 7,614
  • 1
  • 23
  • 36