我是一个绝对的pandas/matplotlib初学者,我在搜索了好几次之后还是搞不懂这个问题。在
刚刚了解到,为了格式化日期(基本上是间隔时间),我需要做一个额外的工作,称为fig
(用于图):
fig, tg = plt.subplots(1)
tg.plot(pandoc['date_time'], pandoc['total_goals'], kind="bar")
tg.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
fig.autofmt_xdate()
但是,当我试图将数据kind
更改为`bar时,我得到了以下错误:
AttributeError: Unknown property kind
当我这么做的时候,它完美地工作了
^{pr2}$但是mdates.DateFormatter
就不起作用了。在
我遗漏了一些东西。它是什么?在
Pandas数据帧,例如} parameter 的
pandoc
,有一个带有a ^{plot
方法。 所以可以用注意,
ax=tg
用于告诉pandoc
在matplotlib轴上绘制tg
。在相反,matplotlib轴,比如} parameter 。相反,要使用Axes对象绘制条形图,请调用它的^{} method 。在
tg
,有一个plot
方法,但是tg.plot
does not have a ^{使用
^{pr2}$pandoc.plot
方法,您可以使用类似于注意,
tg.set_xticklabels
用于设置xticklabels,而不是mdates.DateFormatter
。绘制条形图时,基本条形图xtick
值是整数:当} 这样的动态格式化程序没有任何好处;您最好使用
xtick
值是日期时,只能使用mdates.DateFormatter
。 由于条形图具有固定数量的条形图,因此使用^{Axes.set_xticklabels
方法设置xticklabels
。在创建
N
空字符串的列表。例如,['']*2
的计算结果为['', '']
。在是equivalent to
因此
labels, skip = ['']*N, 10
相当于Python切片表示法,例如
x[start:end:step]
是explained here。例如所以在上面的代码中,}是一个新的序列,它以
pandoc['date_time'].dt.strftime('%Y-%m-%d')
是一个字符串序列,如果我们称之为x
,那么{x[skip//2]
开始,然后按skip
数量递增。在skip//2
使用integer-division将skip
除以2。在labels
以N
空字符串的列表开头。使用skip=10
,赋值将每10个元素(从
skip//2
开始)替换为pandoc['date_time'].dt.strftime('%Y-%m-%d')
中的日期字符串。在pandoc['date_time']
是一个时间序列。pandoc['date_time'].dt.strftime('%Y-%m-%d')
使用^{%Y-%m-%d
格式的日期字符串。在相关问题 更多 >
编程相关推荐