青春有我
我认为Pandas绘图和Matplotlib格式化程序存在兼容性问题。使用以下代码:df = pd.read_csv('lt_stream-1001-full.csv', header=0, encoding='utf8')df['reg_date'] = pd.to_datetime(df['reg_date'] , format='%Y-%m-%d %H:%M:%S')df.set_index('reg_date', inplace=True)df_h = df.resample(rule='H').mean()df_h['Day']=df_h.index.strftime('%Y-%m')print(df_h)f, ax = plt.subplots()my_plot = df_h.boxplot(by='Day',figsize=(12,5), showfliers=False, rot=90, ax=ax)locs, labels = plt.xticks()i = 0new_labels = list()for l in labels: if i % 3 == 0: label = labels[i] i += 1 new_labels.append(label) else: label = '' i += 1 new_labels.append(label)ax.set_xticklabels(new_labels)plt.show()你会得到这个图表:但我注意到这是按月而不是按天分组的。它可能不是你想要的。将 day 组件添加到字符串“Day”会使图表变得混乱,因为似乎有太多的框。df = pd.read_csv('lt_stream-1001-full.csv', header=0, encoding='utf8')df['reg_date'] = pd.to_datetime(df['reg_date'] , format='%Y-%m-%d %H:%M:%S')df.set_index('reg_date', inplace=True)df_h = df.resample(rule='H').mean()df_h['Day']=df_h.index.strftime('%Y-%m-%d')print(df_h)f, ax = plt.subplots()my_plot = df_h.boxplot(by='Day',figsize=(12,5), showfliers=False, rot=90, ax=ax)locs, labels = plt.xticks()i = 0new_labels = list()for l in labels: if i % 15 == 0: label = labels[i] i += 1 new_labels.append(label) else: label = '' i += 1 new_labels.append(label)ax.set_xticklabels(new_labels)plt.show()for 循环根据需要每隔任意多个周期创建价格变动标签。在第一个图表中,它们每3个月设置一次。在第二个中,每15天一次。如果您希望看到更少的网格线:df = pd.read_csv('lt_stream-1001-full.csv', header=0, encoding='utf8')df['reg_date'] = pd.to_datetime(df['reg_date'] , format='%Y-%m-%d %H:%M:%S')df.set_index('reg_date', inplace=True)df_h = df.resample(rule='H').mean()df_h['Day']=df_h.index.strftime('%Y-%m-%d')print(df_h)f, ax = plt.subplots()my_plot = df_h.boxplot(by='Day',figsize=(12,5), showfliers=False, rot=90, ax=ax)locs, labels = plt.xticks()i = 0new_labels = list()new_locs = list()for l in labels: if i % 3 == 0: label = labels[i] loc = locs[i] i += 1 new_labels.append(label) new_locs.append(loc) else: i += 1 ax.set_xticks(new_locs)ax.set_xticklabels(new_labels)ax.grid(axis='y')plt.show()我已经在Pandas图中阅读了x_compat以应用Matplotlib格式化程序,但是我在尝试应用它时遇到了错误。我稍后再试一次。旧的不成功的答案刻度标签似乎是日期。如果在数据帧中将它们设置为日期时间,您可以:months = mdates.MonthLocator(1,4,7,10) #Choose the months you like the mostax.xaxis.set_major_locator(months)否则,您可以通过以下方式让 Matplotlib 知道它们是日期:ax.xaxis_date()您的评论:我添加了其他信息:我忘了显示DataFrame内部的内容。我有三列:reg_Date - datetime64(索引)温度 - float64 Day - 从reg_Date转换为字符串的日期,它看起来像'2017-10' *(YYYY-MM) *箱形图按“日”分组日期,我想将值“日”显示为标签,但不是所有值,例如每三个值。根据您上面斜体的评论,我将使用reg_Date作为输入和以下行:days = mdates.DayLocator(interval=3)daysFmt = mdates.DateFormatter('%Y-%m') #to format displayax.xaxis.set_major_locator(days)ax.xaxis.set_major_formatter(daysFmt)我忘了提到你需要:import matplotlib.dates as mdates这有效吗?
喵喵时光机
你快到了。只需设置股票代码。多重定位器。熊猫。DataFrame.boxplot 也返回 ,这是类 的对象。因此,您可以使用此代码段来自定义标签:axesmatplotlib.axes.Axesimport numpy as npimport matplotlib.pyplot as pltimport matplotlib.ticker as tickercenter = np.random.randint(50,size=(10, 20))spread = np.random.rand(10, 20) * 30flier_high = np.random.rand(10, 20) * 30 + 30flier_low = np.random.rand(10, 20) * -30y = np.concatenate((spread, center, flier_high, flier_low))fig, ax = plt.subplots(figsize=(10, 5))ax.boxplot(y)x = ['Label '+str(i) for i in range(20)]ax.set_xticklabels(x)ax.set_xlabel('Day')# Set a tick on each integer multiple of a base within the view interval.ax.xaxis.set_major_locator(ticker.MultipleLocator(5))plt.xticks(rotation=90)