简体   繁体   中英

Adding labels in scatter plot 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')

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

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM