簡體   English   中英

在pyplot圖例中插入%d不起作用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM