Adding labels in scatter plot legend

pandas scatter plot label points
matplotlib scatter plot label points
no handles with labels found to put in legend.
scatter plot legend? - matlab
matplotlib legend
matplotlib scatter legend colormap
pandas plot legend
seaborn legend

I am trying to add legend labels to my scatter plot for my physics lab report. It seems to only display the first word (in this case: "Actual") and nothing else. The plot also saves and empty file.

import matplotlib.pyplot as plt
import numpy as np

IndexofR=[1.33, 1.443, 1.34] #Actual, Pfund's Method, Snell's Law
Colors = ['red', 'blue', 'green']
Labels = ['Actual','Pfund\'s Method', 'Snell\'s Law']

plt.scatter(IndexofR, np.zeros_like(IndexofR), c = ['red', 'blue', 'green'], vmin=-2)

plt.yticks([])
plt.xlabel('Index of Refraction')
plt.legend(Labels, loc=1)
plt.title('Actual and Calculated Indexes of Refraction in Tap Water')
plt.show()
plt.savefig('LineGraphLab2.pdf')

I would also like to make the whole plot shorter (it is tall for the small amount of data).

Try doing something like this:

import matplotlib.pyplot as plt
import numpy as np

IndexofR=[1.33, 1.443, 1.34] #Actual, Pfund's Method, Snell's Law
Colors = ['red', 'blue', 'green']
Labels = ['Actual','Pfund\'s Method', 'Snell\'s Law']

for i, c, l in zip(IndexofR, Colors, Labels):
    plt.scatter(i, np.zeros_like(i), c=c, vmin=-2, label=l)

plt.yticks([])
plt.xlabel('Index of Refraction')
plt.legend(loc=1)
plt.title('Actual and Calculated Indexes of Refraction in Tap Water')
plt.show()
plt.savefig('LineGraphLab2.pdf')

Matplotlib scatter plot legend, Other answers seem a bit complex, you can just add a parameter 'label' in scatter function and that will be the legend for your plot. import matplotlib.pyplot as plt  It seems to only display the first word (in this case: "Actual") and nothing else. The plot also saves and empty file. import matplotlib.pyplot as pltimport numpy as npIndexofR=[1.33, 1.443, 1.34] #Actual, Pfund's Method, Snell's LawColors = ['red', 'blue', 'green']Labels = ['Actual','Pfund\'s Method', 'Snell\'s Law']plt.scatter(IndexofR, np.zeros_like(IndexofR), c = ['red', 'blue', 'green'], vmin=-2)plt.yticks([])plt.xlabel('Index of Refraction')plt.legend(Labels, loc=1)plt.

Yes because you are inputting a list, rather than a string, so just the first item in the list will be used, 'Actual' in this case. The phrase you want is,

Actual Pfund's Method Snell's Law

The following might work,

Labels = 'Actual' + 'Pfund\'s Method' + 'Snell\'s Law'

I'm not sure about the Perl regex escape. Does matplotlib use latex (?), If so will work

 Labels = 'Actual' + 'Pfund\textquotesingle s Method' + 'Snell\textquotesingle s Law'

The Linux escape for quote is if course '"'"' , or just skip the apostrophe?

The other part of the question is defining subplot sizes, Viz.

fig = plt.figure(figsize=(10,10), dpi=200)
axes = fig.add_subplot(1.5,1,1)
Etc...

Fiddle with the numbers until you get the plot you want

Customizing Plot Legends, Plot legends give meaning to a visualization, assigning meaning to the various plot We can use a rounded box ( fancybox ) or add a shadow, change the size and color but no label plt.scatter(lon, lat, label=None, c=np.log10(population​),  Step 1: Select the Data, INSERT -> Recommended Charts -> Scatter chart (3 rd chart will be scatter chart) Let the Step 2: Click the + symbol and add data labels by clicking it as shown below Step 3: Now we need to add the flavor names to the label. Now right click on the label and click format

My knowledge could not change the image size.

import matplotlib.pyplot as plt
import numpy as np

IndexofR = [1.33, 1.443, 1.34]  # Actual, Pfund's Method, Snell's Law
Colors = ['red', 'blue', 'green']
Labels = ['Actual', 'Pfund\'s Method', 'Snell\'s Law']

plt.scatter(IndexofR, np.zeros_like(IndexofR), c=['red', 'blue', 'green'], vmin=-2)

plt.yticks([])
plt.xlabel('Index of Refraction')

# plt.scatter(x_array, y_array, label="label_name")
for n in range(len(Labels)):
    plt.scatter(IndexofR[n], 0, label=Labels[n])

plt.legend(Labels, loc=len(Labels))
plt.title('Actual and Calculated Indexes of Refraction in Tap Water')
plt.show()
plt.savefig('LineGraphLab2.pdf')

How to add a legend to a scatter plot in matplotlib ?, Examples of how to add a legend to a scatter plot in matplotlib: [1,2,3,4] y = [4,1​,3,6] plt.scatter(x, y, c='coral', label='Class 1') x = [5,6,7,8] y = [1  Using the pyplot function legend (): How to add a legend for a scatter plot in matplotlib ? import matplotlib.pyplot as plt x = [1,2,3,4] y = [4,1,3,6] plt.scatter (x, y, c='coral', label='Class 1') x = [5,6,7,8] y = [1,3,5,2] plt.scatter (x, y, c='lightblue', label='Class 2') plt.legend () plt.title ('Nuage de points avec Matplotlib') plt.xlabel ('x') plt.ylabel ('y') plt.savefig ('ScatterPlot_09.png') plt.show ()

Legends, Titles, and Labels with Matplotlib, plt.plot(x, y, label='First Line') plt.plot(x2, y2, label='Second Line'). Here, we plot as we've seen already, only this time we add another parameter "label." This allows us to assign a name to the Scatter Plots with Matplotlib. Go. Stack Plots with  2D scatter plot. Using the scatter method of the matplotlib.pyplot module should work (at least with matplotlib 1.2.1 with Python 2.7.5), as in the example code below. Also, if you are using scatter plots, use scatterpoints=1 rather than numpoints=1 in the legend call to have only one point for each legend entry.

How to create a scatter chart and bubble chart in PowerPoint :: think , 12.1 Labels. In both chart types, up to two labels can be associated with each data point. Labels can be added using the image Add Label  Sometimes when designing a plot you'd like to add multiple legends to the same axes. Unfortunately, Matplotlib does not make this easy: via the standard legend interface, it is only possible to create a single legend for the entire plot. If you try to create a second legend using plt.legend() or ax.legend(), it

[PDF] legend options, contour plot legend; see [G-3] clegend option label() suboptions when you wish to change the text of multiple keys. scatter ylow yhigh x, pstyle(p1 p1) || . Adding labels Labels help people understand the significance of each axis of any graph you create. Without labels, the values portrayed don’t have any significance.

Comments
  • The plt.legend applies a label to each plot: as you are only drawing one scatter plot, only one label is used. See @1800flowers answer, where he draws 3 plots and should get a label for each.
  • Your data is 1D and only has 3 points?
  • Yes, and I may need to include error bars somehow (these bars may overlap and I don't know how to clearly differentiate them)
  • Ok but you skip the legend title
  • 1. Can you explain the code? 2. It still creates an empty file
  • Try saving the file before displaying it plt.savefig('LineGraphLab2.pdf'); plt.show()