为了账号安全,请及时绑定邮箱和手机立即绑定

无法在 Matplotlib 中准备正确的标签

无法在 Matplotlib 中准备正确的标签

德玛西亚99 2022-08-02 10:44:30
我有非常简单的代码:from matplotlib import datesimport matplotlib.ticker as tickermy_plot=df_h.boxplot(by='Day',figsize=(12,5), showfliers=False, rot=90)我有:但我想在X轴上有更少的标签。为此,我添加了:my_plot.xaxis.set_major_locator(ticker.MaxNLocator(12))它生成的标签较少,但标签的值有错误的值(=整个列表中的少数标签中的第一个)我做错了什么?我添加了其他信息:我忘了显示DataFrame内部的内容。我有三列:reg_Date - datetime64(索引)温度 - float64 Day - 从reg_Date转换为字符串的日期,它看起来像'2017-10'(YYYY-MM)箱形图按“日”分组日期,我想将值“日”显示为标签,但不是所有值,例如每三个值。
查看完整描述

2 回答

?
青春有我

TA贡献1784条经验 获得超8个赞

我认为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 = 0

new_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()

你会得到这个图表:

//img1.sycdn.imooc.com//62e88fc50001864305960475.jpg

但我注意到这是按月而不是按天分组的。它可能不是你想要的。

将 day 组件添加到字符串“Day”会使图表变得混乱,因为似乎有太多的框。

//img1.sycdn.imooc.com//62e88fd400011de113940776.jpg

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 = 0

new_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 = 0

new_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()

//img1.sycdn.imooc.com//62e88fe20001ae5707150526.jpg

我已经在Pandas图中阅读了x_compat以应用Matplotlib格式化程序,但是我在尝试应用它时遇到了错误。我稍后再试一次。


旧的不成功的答案


刻度标签似乎是日期。如果在数据帧中将它们设置为日期时间,您可以:


months = mdates.MonthLocator(1,4,7,10) #Choose the months you like the most

ax.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 display

ax.xaxis.set_major_locator(days)

ax.xaxis.set_major_formatter(daysFmt)

我忘了提到你需要:


import matplotlib.dates as mdates

这有效吗?


查看完整回答
反对 回复 2022-08-02
?
喵喵时光机

TA贡献1846条经验 获得超7个赞

你快到了。只需设置股票代码。多重定位器。


熊猫。DataFrame.boxplot 也返回 ,这是类 的对象。因此,您可以使用此代码段来自定义标签:axesmatplotlib.axes.Axes


import numpy as np

import matplotlib.pyplot as plt

import matplotlib.ticker as ticker


center = np.random.randint(50,size=(10, 20))

spread = np.random.rand(10, 20) * 30

flier_high = np.random.rand(10, 20) * 30 + 30

flier_low = np.random.rand(10, 20) * -30

y = 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)

//img1.sycdn.imooc.com//62e88ff70001ff3e06030348.jpg

查看完整回答
反对 回复 2022-08-02
  • 2 回答
  • 0 关注
  • 132 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号