Issue
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).
Solution
Since none of the current answers address all of my questions, I will write my own answer (years later).
Following @luthierBG's answer, plotting in a loop seems like the best option to label each point, though there is no need to plot both before and in the loop (which caused the doubling shown in their figure). With that change, plt.legend()
will contain all the legend elements.
Regarding the empty figure, @1800flowers suggested it could be due to using plt.show()
before the save. This is consistent with the documentation, which states:
If you want an image file as well as a user interface window, use pyplot.savefig before pyplot.show. At the end of (a blocking) show() the figure is closed and thus unregistered from pyplot. Calling pyplot.savefig afterwards would save a new and thus empty figure. This limitation of command order does not apply if the show is non-blocking or if you keep a reference to the figure and use Figure.savefig.
Lastly, the figure can be resized by passing the figsize
argument to plt.figure()
.
import matplotlib.pyplot as plt
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']
fig = plt.figure(figsize=(6,4))
for ir, c, label in zip(IndexofR, Colors, Labels):
plt.scatter(ir, 0, c=c, label=label)
plt.yticks([])
plt.xlabel('Index of Refraction')
plt.title('Actual and Calculated Indexes of Refraction in Tap Water')
plt.legend(loc=1)
plt.tight_layout()
plt.savefig("LineGraphLab2.pdf")
plt.show()
Being that I am now answering my question years after it was asked, I have changed how I write code, so I thought I might as well include the code that has changes reflecting how I'd now write things (i.e. using "
instead of '
to avoid the escape, using the explicit "Axes" matplotlib interface, and lowercase variable names).
import matplotlib.pyplot as plt
plt.close("all")
# Actual, Pfund's Method, Snell's Law
index_of_R = [1.33, 1.443, 1.34]
colors = ["red", "blue", "green"]
labels = ["Actual", "Pfund's Method", "Snell's Law"]
fig, ax = plt.subplots(figsize=(6,4))
for ir, c, label in zip(index_of_R, colors, labels):
ax.scatter(ir, 0, c=c, label=label)
ax.set_yticks([])
ax.set_xlabel("Index of Refraction")
ax.set_title("Actual and Calculated Indexes of Refraction in Tap Water")
ax.legend(loc=1)
fig.tight_layout()
fig.savefig("LineGraphLab2.pdf")
fig.show()
Answered By - jared
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.