Matplotlib 在第二轴上旋转标签

Matplotlib 在第二轴上旋转标签,matplotlib,Matplotlib,我正在向绘图中添加第二个x轴,如下所示: ax2 = ax.twiny() offset = 0, -25 new_axisline = ax2.get_grid_helper().new_fixed_axis ax2.axis["bottom"] = new_axisline(loc="bottom", axes=ax2, offset=offset) ax2.axis["top"].set_visible(False) ax2.set_xticks(xticks) ax2.xaxis.se

我正在向绘图中添加第二个x轴,如下所示:

ax2 = ax.twiny()
offset = 0, -25
new_axisline = ax2.get_grid_helper().new_fixed_axis
ax2.axis["bottom"] = new_axisline(loc="bottom", axes=ax2, offset=offset)
ax2.axis["top"].set_visible(False)

ax2.set_xticks(xticks)
ax2.xaxis.set_major_formatter(ticker.NullFormatter())
ax2.xaxis.set_minor_locator(ticker.FixedLocator(xticks))
ax2.xaxis.set_minor_formatter(ticker.FixedFormatter(xticks_labels))
问题是我不知道如何从那里旋转标签

另外:如果将记号添加到第一个轴:

plt.xticks(xticks1, xticks1_labels, rotation='vertical')
rotation
参数被忽略,我也不明白为什么

我试过了

ax2.set_xticklabels(ax2.xaxis.get_minorticklabels(), rotation=45)
但它也没有效果

任何帮助都将不胜感激


您可以在下面查看完整的打印逻辑:

def event_plot(event_list, labels=None, figsize=(16, 9), padding=0.85, grid=False, title=None, colors=None):

    fig = plt.figure(figsize=figsize)
    ax = SubplotHost(fig, 111)
    # ax = fig.add_subplot(111)
    fig.add_subplot(ax)
    ax.grid(grid)

    if title is not None:
        ax.set_title(title)

    max_end = 0
    for i, events in enumerate(event_list):

        for event in events:
            start = event[0]
            end = event[1]
            max_end = max(max_end, end)
            y = (i, i + padding)
            c = 'red' if colors is None else colors[i]
            plt.fill_between([start, end], y[0], y2=y[1], color=c, alpha=0.35, linewidth=0.0)

    plt.legend(['Recording data available for channel'], loc='upper center')

    if labels is not None:
        labels_ids = np.asarray(range(len(labels))) + 1
        labels_y = labels_ids - 0.5 - (1 - padding) / 2.
        plt.yticks(labels_y, labels)
        for y in labels_y:
            plt.axhline(y, alpha=0.125, color='k', linestyle='--')

    return ax, fig


def plot_case_windows(all_records, case_windows, filename_title, filename=None):

    channel_event_list = list()
    labels = list()

    for group_name, group in all_records.channel_groups.items():
        [(labels.append(x[0]), channel_event_list.append(x[1])) for x in group.items()]

    recording_time = all_records.end - all_records.start

    title = 'File: {:s}, recording time: {:d} sec'.format(os.path.basename(filename_title), int(recording_time))
    ax1, fig = event_plot(channel_event_list, title=title, labels=labels)

    xticksmax = 0
    xticksmin = float('Inf')
    xticks1 = list()
    xticks1_labels = list()
    xticks2 = list()
    xticks2_labels = list()

    for case_win in case_windows:

        xticks1.append(int(case_win.start + (case_win.end - case_win.start)/2.))
        xticks2.append(case_win.start)

        xticksmax = max(xticksmax, case_win.end)
        xticksmin = min(xticksmin, case_win.start)

        xticks1_labels.append(case_win.name)
        xticks2_labels.append(str(case_win.start) + ' s')

        plt.axvline(x=case_win.start, color='k', linestyle='--')
        plt.axvline(x=case_win.end, color='k', linestyle='--')

    xticks2 = (np.asarray(xticks2) - xticksmin) / (xticksmax - xticksmin)

    plt.xlim([xticksmin, xticksmax])

    ax1.set_xticks(xticks1)
    ax1.xaxis.set_major_formatter(ticker.NullFormatter())
    ax1.xaxis.set_minor_locator(ticker.FixedLocator(xticks1))
    ax1.xaxis.set_minor_formatter(ticker.FixedFormatter(xticks1_labels))

    ax2 = ax1.twiny()

    offset = 0, -20
    new_axisline = ax2.get_grid_helper().new_fixed_axis
    ax2.axis["bottom"] = new_axisline(loc="bottom", axes=ax2, offset=offset)
    ax2.axis["top"].set_visible(False)
    ax2.set_xticks(xticks2)
    ax2.xaxis.set_major_formatter(ticker.NullFormatter())
    ax2.xaxis.set_minor_locator(ticker.FixedLocator(xticks2))
    ax2.xaxis.set_minor_formatter(ticker.FixedFormatter(xticks2_labels))

    plt.setp(ax2.xaxis.get_minorticklabels(), rotation=45)

    # ax2.set_xticklabels(ax1.xaxis.get_minorticklabels(), rotation=45)

    #plt.show()
    plt.tight_layout()

    if filename:
        plt.savefig(filename)

您可以使用
ax2.xaxis.set_major\u格式化程序(ticker.NullFormatter())
=>ax2.xaxis上没有主刻度,也没有可旋转的内容。你必须旋转小刻度。@Serenity这当然有意义。我现在已将其更改为
plt.setp(ax2.xaxis.get_minorticklabels(),rotation=45)
但仍然没有显示任何效果。确定。没有其他线索。你能提供一个可以复制和运行的文件吗?我认为只看代码很难判断出哪里出了问题。请尝试:
ax2.axis[“bottom”].major\u ticklabels.set(rotation=90)
。告诉我这是否可行。裁判: