【问题标题】:How to change the ticks on x-axis?如何更改 x 轴上的刻度?
【发布时间】:2018-11-23 23:56:13
【问题描述】:

我想更改 x 轴刻度。我要:0 10 20 30 40 50 ...

    with pydicom.dcmread(directory) as dataset:
        all_population_ages.append(dataset.PatientAge)
        
plt.hist(all_population_ages,  histtype='bar', rwidth=0.8)
plt.xticks(np.arange(0, 100, step=10))
plt.show()

输出:

我尝试了这个解决方案: Changing the "tick frequency" on x or y axis in matplotlib?

plt.xticks(np.arange(min(x), max(x)+1, 1.0))

plt.xticks(np.arange(min(all_population_ages), max(all_population_ages) + 1, 10.0))

但是收到错误:

plt.xticks(np.arange(min(all_population_ages), max(all_population_ages) + 1, 10.0))

TypeError: 必须是 str,而不是 int

提前感谢您的帮助。

【问题讨论】:

  • 堆栈跟踪告诉您将项目从 int 转换为字符串,即 ['{:d}'.format(x) for x in np.arange(0,10,1)]

标签: python plot axis


【解决方案1】:

完成。

results = list(map(int, all_population_ages))
    bins = np.arange(0, 100, 5)  # fixed bin size
    plt.rcParams.update({'font.size': 8})
    plt.hist(results, bins=bins, alpha=0.5, rwidth=0.8)
    plt.xticks(np.arange(0, 100, 5))

我将字符串列表更改为 int 列表。 我改变了字体大小。

【讨论】: