[英]Adding labels in scatter plot legend
我正在嘗試為我的物理實驗室報告添加圖例標簽到我的散點 plot 中。 它似乎只顯示第一個單詞(在本例中:“Actual”),沒有其他任何內容。 plot 還保存並清空文件。
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')
我還想讓整個 plot 更短(對於少量數據來說它很高)。
嘗試做這樣的事情:
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')
是的,因為您輸入的是列表,而不是字符串,因此僅使用列表中的第一項,在這種情況下為“實際”。 你想要的短語是,
實際 Pfund 方法 Snell 定律
以下可能有效,
Labels = 'Actual' + 'Pfund\'s Method' + 'Snell\'s Law'
我不確定 Perl 正則表達式轉義。 matplotlib 是否使用 latex(?),如果可以
Labels = 'Actual' + 'Pfund\textquotesingle s Method' + 'Snell\textquotesingle s Law'
引用的 Linux 轉義是 if course '"'"',或者只是跳過撇號?
問題的另一部分是定義子圖大小,即。
fig = plt.figure(figsize=(10,10), dpi=200)
axes = fig.add_subplot(1.5,1,1)
Etc...
擺弄數字,直到你得到你想要的 plot
我的知識無法改變圖像大小。
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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.