首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在散点图图例中添加标签

在散点图图例中添加标签
EN

Stack Overflow用户
提问于 2019-09-28 05:43:14
回答 3查看 543关注 0票数 0

我正在尝试将图例标签添加到我的物理实验报告的散点图中。它似乎只显示第一个单词(在本例中:"Actual"),而不显示其他单词。绘图还会保存和清空文件。

代码语言:javascript
复制
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')

我也想让整个图变得更短(对于少量的数据来说,它是高的)。

EN

回答 3

Stack Overflow用户

发布于 2019-09-28 06:05:02

试着这样做:

代码语言:javascript
复制
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')
票数 1
EN

Stack Overflow用户

发布于 2019-09-28 06:08:37

是,因为您输入的是一个列表,而不是一个字符串,所以只会使用列表中的第一项,在本例中是'Actual‘。你想要的短语是,

斯奈尔定律实数法

以下方法可能会起作用,

代码语言:javascript
复制
Labels = 'Actual' + 'Pfund\'s Method' + 'Snell\'s Law'

我不确定Perl正则表达式的转义。matplotlib是否使用latex (?),如果可以

代码语言:javascript
复制
 Labels = 'Actual' + 'Pfund\textquotesingle s Method' + 'Snell\textquotesingle s Law'

引号的Linux转义是如果过程是‘“’,还是直接跳过撇号?

问题的另一部分是定义子图大小,即。

代码语言:javascript
复制
fig = plt.figure(figsize=(10,10), dpi=200)
axes = fig.add_subplot(1.5,1,1)
Etc...

摆弄这些数字,直到你得到你想要的图

票数 0
EN

Stack Overflow用户

发布于 2019-09-28 06:21:25

我的知识无法改变图像的大小。

代码语言:javascript
复制
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')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58141628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档