Search code examples
pythonpandasmatplotlibscatter-plotplt

How to add a legend to a scatter graph?


I have plotted a scatter graph that takes two columns from a table using:

df.plot.scatter("volume/mm^3", "Cost per m^3/$") 
plt.title("Volume vs. Cost Analysis", size = 16)
plt.ylim((0,80000))
plt.xlim((7500,30000))

I am now trying to either annotate each point or create a legend using the first column How would I do so?


Solution

  • Use the label parameter

    df = pd.DataFrame(np.random.rand(10, 2), columns=['x', 'y'])
    
    df.plot.scatter('x', 'y', label='x')
    

    enter image description here