在制作圖示時需要繪制柱狀圖,下面對其進行了繪制:主要就是使用matplotlib的
bar
函數。
bar函數官方API為 【matplotlib.pyplot.bar】
下面是從一篇論文中随意截取的一段話,進行後續的繪制圖像。
import matplotlib.pyplot as plt
import random
my_list = [random.randint(0, 100) for x in range(12)]
str = "Machine learning models are vulnerable to adversarial examples For the black-box setting"
labels = str.split(" ")
plt.barh(range(len(my_list)), my_list, tick_label=labels)
# plt.ylabel('time(S)') #圖像縱坐标的标記
plt.ylabel('time(S)') #圖像縱坐标
plt.savefig("./time_bar.jpg") #儲存圖像
這樣繪制出來的圖形會發現下面非label全都遮擋了(見下圖),是以可以采用将label進行旋轉的操作或者将圖像的橫縱坐标進行變換,達到效果。

label進行旋轉後顯示:
import matplotlib.pyplot as plt
import random
my_list = [random.randint(0, 100) for x in range(12)]
str = "Machine learning models are vulnerable to adversarial examples For the black-box setting"
labels = str.split(" ")
plt.xticks(rotation=270)
plt.bar(range(len(my_list)), my_list, tick_label=labels)
plt.savefig("./time_bar.jpg") #儲存圖像
繪制後的圖像為:
這裡也可以橫縱坐标交換,使用
barh
函數即可實作,代碼和效果如下:
import matplotlib.pyplot as plt
import random
my_list = [random.randint(0, 100) for x in range(12)]
str = "Machine learning models are vulnerable to adversarial examples For the black-box setting"
labels = str.split(" ")
plt.barh(range(len(my_list)), my_list, tick_label=labels)
# plt.ylabel('time(S)') #圖像縱坐标的标記
參考:
【Python——使用matplotlib繪制柱狀圖】