[英]inserting %d in pyplot legend does not work
請查看下面的代碼。
import matplotlib.pyplot as plt
title = 25
plt.title('title is %d' % (title))
L = 15
plt.legend('L = %d' % L)
標題復制了預期的數字。 但是,圖例只是在數字應留的位置留有空白。
調用plt.legend
您必須傳遞一系列字符串,例如字符串列表。
僅傳遞單個字符串的問題在於它將字符串視為一個序列(它是這樣),並且僅從中獲取第一個元素,在這種情況下,恰好是字母L
請注意,與其將您的字符串傳遞到plt.legend()
,更label = my_string
在調用plt.plot
時使用keyword-argument label = my_string
,這樣看起來像plt.plot(x, y, label='L = %d' % L)
。
import matplotlib.pyplot as plt
title = 25
plt.title('title is %d' % (title))
plt.plot([1,2,3])
L = 15
plt.legend(['L = %d' % L])
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.