簡體   English   中英

如何使用 matplotlib 在 x 軸條形圖上插入標簽?

[英]How to insert labels on to x-axis bar graph using matplotlib?

我顯然做錯了,因為我使用 python3 matplotlib 收到警告 - UserWarning: FixedFormatter should only be used with FixedLocator ax.set_xticklabels(labels)

此外,plot 標簽未正確繪制,似乎重疊或損壞。 基本上,我希望每個點都代表標簽,例如 -

Total_Ticks Time_in_secs 迭代次數/秒 迭代次數

但是,它並沒有表明這一點。 出了什么問題,我在這里錯過了什么?

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from matplotlib import ticker

data = [[1.0, 1.0, 1.0, 1.0], [1.0909675345548056, 1.0909675345548056, 2.499866074843752, 2.727272727272727], [1.255062680810029, 1.255062680810029, 4.346034298168655, 5.454545454545454]
print(data)
X = np.arange(4)
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
labels = ['Total_Ticks', 'Time_in_secs', 'Iterations/sec', 'Iterations']
ax.bar(X + 0.00, data[0], color = 'b', width = 0.25)
ax.bar(X + 0.25, data[1], color = 'g', width = 0.25)
ax.bar(X + 0.50, data[2], color = 'r', width = 0.25)
# save the figure
ax.legend(labels=['intel', 'amd','apple'])
ax.set_xticklabels(labels)
plt.savefig('plot.png', dpi=300, bbox_inches='tight')

設置標簽寬度:

ax.set_xticks(np.linspace(0.25,3.25,4))

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from matplotlib import ticker

data = [[1.0, 1.0, 1.0, 1.0], [1.0909675345548056, 1.0909675345548056, 2.499866074843752, 2.727272727272727], [1.255062680810029, 1.255062680810029, 4.346034298168655, 5.454545454545454]]
print(data)
X = np.arange(4)
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
labels = ['Total_Ticks', 'Time_in_secs', 'Iterations/sec', 'Iterations']
ax.bar(X + 0.00, data[0], color = 'b', width = 0.25)
ax.bar(X + 0.25, data[1], color = 'g', width = 0.25)
ax.bar(X + 0.50, data[2], color = 'r', width = 0.25)
# save the figure
ax.legend(labels=['intel', 'amd','apple'])
ax.set_xticklabels(labels)
ax.set_xticks(np.linspace(0.25,3.25,4))
plt.savefig('plot.png', dpi=300, bbox_inches='tight')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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