【问题标题】:How to insert labels on to x-axis bar graph using matplotlib?如何使用 matplotlib 在 x 轴条形图上插入标签?
【发布时间】:2021-06-07 06:04:47
【问题描述】:

我显然做错了,因为我使用 python3 matplotlib 收到警告 - 用户警告:FixedFormatter 只能与 FixedLocator 一起使用 ax.set_xticklabels(标签)

此外,绘图标签未正确绘制,似乎重叠或损坏。 基本上,我希望每个点都代表标签 -

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')

【问题讨论】:

    标签: python python-3.x matplotlib


    【解决方案1】:

    设置标签宽度:

    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')
    

    【讨论】:

      猜你喜欢
      • 2021-09-14
      • 2017-09-26
      • 2021-07-24
      • 2021-11-28
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      相关资源
      最近更新 更多