我正在尝试将图例标签添加到我的物理实验报告的散点图中。它似乎只显示第一个单词(在本例中:"Actual"),而不显示其他单词。绘图还会保存和清空文件。
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')我也想让整个图变得更短(对于少量的数据来说,它是高的)。
发布于 2019-09-28 06:05:02
试着这样做:
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')发布于 2019-09-28 06:08:37
是,因为您输入的是一个列表,而不是一个字符串,所以只会使用列表中的第一项,在本例中是'Actual‘。你想要的短语是,
斯奈尔定律实数法
以下方法可能会起作用,
Labels = 'Actual' + 'Pfund\'s Method' + 'Snell\'s Law'我不确定Perl正则表达式的转义。matplotlib是否使用latex (?),如果可以
Labels = 'Actual' + 'Pfund\textquotesingle s Method' + 'Snell\textquotesingle s Law'引号的Linux转义是如果过程是‘“’,还是直接跳过撇号?
问题的另一部分是定义子图大小,即。
fig = plt.figure(figsize=(10,10), dpi=200)
axes = fig.add_subplot(1.5,1,1)
Etc...摆弄这些数字,直到你得到你想要的图
发布于 2019-09-28 06:21:25
我的知识无法改变图像的大小。

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')https://stackoverflow.com/questions/58141628
复制相似问题