Python 调用axes.cla()后,matplotlib-autofmt_xdate()无法旋转x轴标签

Python 调用axes.cla()后,matplotlib-autofmt_xdate()无法旋转x轴标签,python,matplotlib,Python,Matplotlib,我有两组数据需要根据时间绘制。 我需要通过单选按钮(或类似方式)单独或同时显示它们。单选按钮代码基于 在加载第一组数据之前,一切看起来都很好。 每当要打印下一组数据时,我清除轴并重新打印数据。但当我单击单选按钮中的下一项时,将显示新数据,但x轴不会旋转。有没有办法解决这个问题 用于重现我面临的问题的示例代码 import datetime import matplotlib.pyplot as plt from matplotlib.widgets import RadioButtons imp

我有两组数据需要根据时间绘制。 我需要通过单选按钮(或类似方式)单独或同时显示它们。单选按钮代码基于

在加载第一组数据之前,一切看起来都很好。 每当要打印下一组数据时,我清除轴并重新打印数据。但当我单击单选按钮中的下一项时,将显示新数据,但x轴不会旋转。有没有办法解决这个问题

用于重现我面临的问题的示例代码

import datetime
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
import matplotlib.dates as mdates

data0_usage = [45, 76, 20, 86, 79, 95, 14, 94, 59, 84]
data1_usage = [57, 79, 25, 28, 17, 46, 29, 52, 68, 92]
data0_timestamp = []


def draw_data_plot(ax, data_no):
    if data_no == 0:
        data_usage = data0_usage
        data_color = 'go'
    elif data_no == 1:
        data_usage = data1_usage
        data_color = 'ro'

    ax.plot_date(data0_timestamp, data_usage, data_color)
    ax.plot_date(data0_timestamp, data_usage, 'k', markersize=1)


def draw_plot():
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.grid(True)

    # Adjust the subplots region to leave some space for the sliders and buttons
    fig.subplots_adjust(left=0.25, bottom=0.25)

    # Beautify the dates on x axis
    time_format = mdates.DateFormatter('%Y-%b-%d %H:%M:%S')
    plt.gca().xaxis.set_major_formatter(time_format)
    plt.gcf().autofmt_xdate()

    # Draw data0 plot
    draw_data_plot(ax, 0)

    # Add a set of radio buttons for changing color
    color_radios_ax = fig.add_axes(
        [0.025, 0.5, 0.15, 0.15], axisbg='lightgoldenrodyellow')
    color_radios = RadioButtons(
        color_radios_ax, ('data 0', 'data 1', 'all'),
        active=0)

    def color_radios_on_clicked(label):
        ax.cla()
        ax.grid(True)
        if label == 'all':
            draw_data_plot(ax, 0)
            draw_data_plot(ax, 1)
        else:
            draw_data_plot(ax, int(label[5]))

        ax.xaxis.set_major_formatter(time_format)
        plt.gcf().autofmt_xdate()
        fig.canvas.draw_idle()

    color_radios.on_clicked(color_radios_on_clicked)
    plt.show()


current_date = datetime.datetime.today()
for days in range(0, 10):
    data0_timestamp.append(current_date + datetime.timedelta(days))
draw_plot()
使用Windows 10、Python 2.7.32、matplotlib 2.1.0解决了这个问题 对于轴内容的后续更新,
plt.gcf().autofmt_xdate()
失败的原因是,在调用时,图形中有轴,这些轴不是子批次。这是由
图创建的轴。添加_轴
,即radiobutton轴
autofmt_xdate
将不知道如何处理该轴,即它不知道这是否是旋转标签的轴,因此它将决定不执行任何操作。这看起来像

allsubplots = all(hasattr(ax, 'is_last_row') for ax in self.axes)
if len(self.axes) == 1:
    # rotate labels
else:
    if allsubplots:
        for ax in self.get_axes():
            if ax.is_last_row():
                #rotate labels
            else:
                #set labels invisible
因为您有一个轴,它不是子地块,
allsubplot==False
并且不会发生旋转

解决方案 解决方案不是使用
autofmt_xdate()
,而是手动旋转标签的工作-实际上只有3行代码。将行
plt.gcf().autofmt\u xdate()
替换为

for label in ax.get_xticklabels():
    label.set_ha("right")
    label.set_rotation(30)

@importanceofbeingernest在清除轴之后(就在调用draw_idle之前),我正在调用plt.gcf().autofmt_xdate()。我认为一旦清除轴,plt.gcf()可能没有引用正确的图形