【问题标题】:Matplotlib string xticks [duplicate]Matplotlib字符串xticks [重复]
【发布时间】:2014-10-16 10:30:16
【问题描述】:
def plot_freq_error(diff,file,possible_frequency):
    for foo in range(0, len(diff)):
        x = [diff[foo]]
        name = comp
        color = ['0.1', '0.2', '0.3','0.4','0.5','0.6','0.7','0.8', '0.9','0.95','1.0']
        label = ['0.8GHz','1.0GHz','1.2GHz','1.4GHz','1.6GHz','1.8GHz','2.0GHz','2.2GHz','2.4GHz']
        y = zip(*x)
        pos = np.arange(len(x))
        width = 1. / (1 + len(x))

        fig,ax = plt.subplots()
        matplotlib.rcParams.update({'font.size': 22})
        for idx, (serie, color,label) in enumerate(zip(y, color,label)):
            ax.bar(pos + idx * width, serie, width, color=color,label=label)

        plt.tick_params(\
            axis='x',          # changes apply to the x-axis
            which='both',      # both major and minor ticks are affected
            bottom='off',      # ticks along the bottom edge are off
            top='off',         # ticks along the top edge are off
            labelbottom='off') # labels along the bottom edge are off
        plt.tick_params(axis='both', which='major', labelsize=10)
        plt.tick_params(axis='both', which='minor', labelsize=8)
        plt.ylabel(name[foo],fontsize=40)
        #ax.legend(prop={'size':5})
        plt.xticks(label)
        plt.gray()
        plt.show()
        plt.clf()

使用我上面编写的代码,我无法将xticks 绘制为每个条的字符串/浮点值。我做错了什么?

【问题讨论】:

  • 你需要使用set_xticklabels
  • 我确实尝试过ax.set_xticklabels(label),但这似乎不起作用。
  • 而不是“这似乎不起作用”,您需要显示您收到的错误消息
  • 没有错误,没有绘制xticks,仅此而已。
  • 好吧,看来你是在设置它们off,在bottom='off', top='off', ...

标签: python matplotlib


【解决方案1】:

ax.set_xticklabels(label) 应该确实有效。您缺少的是使用ax.set_xticks(float) 命令建立x-ticks

这是一个例子:

x = np.arange(2,10,2)
y = x.copy()
x_ticks_labels = ['jan','feb','mar','apr','may']

fig, ax = plt.subplots(1,1) 
ax.plot(x,y)

# Set number of ticks for x-axis
ax.set_xticks(x)
# Set ticks labels for x-axis
ax.set_xticklabels(x_ticks_labels, rotation='vertical', fontsize=18)

【讨论】:

    猜你喜欢
    • 2018-08-21
    • 2017-01-05
    • 2020-08-17
    • 2012-08-29
    • 2018-08-12
    • 2014-12-09
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多