你能在庞大酒吧情节中错开小/ xtick标签吗?

时间:2017-03-06 16:43:16

标签: pandas plot

告诉this previous question有没有办法让大熊猫绘制小型和主要的xticklabels,让日期,月份和年份相互叠加成条形图?有点像This,但对于熊猫生成的条形图?

---- ---- EDIT

添加到场景..

我有一个csv,显示在一段时间内随机挑战的获胜者

--- record.csv ---

date,Team
12/29/2017,BLUE
12/30/2017,GREEN
12/30/2017,GREEN
12/31/2017,GREEN
12/31/2017,BLUE
12/31/2017,BLUE
1/1/2017,GREEN
1/1/2017,BLUE
1/1/2017,GREEN
1/1/2017,BLUE
1/2/2017,GREEN
1/2/2017,BLUE
1/2/2017,GREEN
1/3/2017,GREEN
1/3/2017,BLUE
1/3/2017,BLUE
1/3/2017,GREEN
1/3/2017,GREEN
1/3/2017,GREEN
1/3/2017,BLUE
1/3/2017,GREEN
1/4/2017,BLUE
1/4/2017,BLUE
1/4/2017,GREEN
1/4/2017,GREEN
1/4/2017,BLUE
1/4/2017,GREEN
1/4/2017,GREEN
1/4/2017,GREEN
1/4/2017,GREEN
1/4/2017,GREEN
1/4/2017,GREEN
1/4/2017,GREEN
1/4/2017,BLUE
1/4/2017,GREEN
1/4/2017,GREEN
1/4/2017,BLUE
1/5/2017,GREEN
1/5/2017,BLUE
1/5/2017,GREEN
1/5/2017,BLUE
1/5/2017,GREEN
1/6/2017,GREEN
1/6/2017,GREEN
1/6/2017,BLUE
1/6/2017,GREEN
1/6/2017,BLUE
1/6/2017,GREEN
1/6/2017,GREEN
1/6/2017,GREEN


import pandas as pd
import matplotlib.pyplot as plt

record = pd.read_csv('/desktop/record.csv',header=0,index_col=['date'])
#create a blank dataframe with the entire date range
blank_df = pd.DataFrame(pd.date_range(start=record.date.values.min(),end=record.date.values.max(),freq='D',columns=['date'])

#create df for record grouped by the count of blue and green team for each day
df2 = pd.DataFrame(record.groupby([pd.to_datetime(record['date']), record['Team']]).size())
df2.columns = ['Count']

#combine the blank with the full dataframe
idx = pd.MultiIndex.from_product([blank_df['date'],df2['Team']])
df3 = df2.reindex('date','Team','Count')

# create the pivot table
pivot = df2.pivot(index='date',columns='Team',values='Count').fillna(0)

#create figure time
fig = plt.Figure((3,7),tight_layout=True)
ax = fig.add_subplot(111)

#create the plot
pl = pivot.plot(ax=ax,kind='bar',stacked=True,grid=False,legend=True,colormap='winter')

plt.show()

好吧......所以这只是我在飞行中创建的一个演示......最后一点是将这个绘图放入PyQt4中的QGraphicsScene。我无法真正得到xticklabels错开甚至设置次要和主要xticklabels像这里的例子:

ax.xaxis.set_minor_locator(matplotlib.dates.WeekdayLocator(byweekday=(1),
                                                       interval=1))
ax.xaxis.set_minor_formatter(matplotlib.dates.DateFormatter('%d\n%a'))
ax.xaxis.grid(True, which="minor")
ax.xaxis.grid(False, which="major")
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('\n\n\n%b%Y'))

正如this question中所述。

我真的很想弄清楚如何自定义任何pandas图表xtick和y tick标签,这些标签将在PyQt GUI中很好地显示。但就目前而言,我想弄清楚如何将这个条形图的xticklabels呈现为this线图。你知道吗?

1 个答案:

答案 0 :(得分:2)

让我们压缩并纠正你的例子:

from io import StringIO

import pandas
from matplotlib import pyplot

fakefile = StringIO("""\
date,Team
12/29/2016,BLUE\n12/30/2016,GREEN\n12/30/2016,GREEN
12/31/2016,GREEN\n12/31/2016,BLUE\n12/31/2016,BLUE
1/1/2017,GREEN\n1/1/2017,BLUE\n1/1/2017,GREEN
1/1/2017,BLUE\n1/2/2017,GREEN\n1/2/2017,BLUE
1/2/2017,GREEN\n1/3/2017,GREEN\n1/3/2017,BLUE
1/3/2017,BLUE\n1/3/2017,GREEN\n1/3/2017,GREEN
1/3/2017,GREEN\n1/3/2017,BLUE\n1/3/2017,GREEN
1/4/2017,BLUE\n1/4/2017,BLUE\n1/4/2017,GREEN
1/4/2017,GREEN\n1/4/2017,BLUE\n1/4/2017,GREEN
1/4/2017,GREEN\n1/4/2017,GREEN\n1/4/2017,GREEN
1/4/2017,GREEN\n1/4/2017,GREEN\n1/4/2017,GREEN
1/4/2017,BLUE\n1/4/2017,GREEN\n1/4/2017,GREEN
1/4/2017,BLUE\n1/5/2017,GREEN\n1/5/2017,BLUE
1/5/2017,GREEN\n1/5/2017,BLUE\n1/5/2017,GREEN
1/6/2017,GREEN\n1/6/2017,GREEN\n1/6/2017,BLUE
1/6/2017,GREEN\n1/6/2017,BLUE\n1/6/2017,GREEN
1/6/2017,GREEN\n1/6/2017,GREEN
""")

record = pandas.read_csv(fakefile, header=0, parse_dates=[0])
dates = pandas.date_range(start=record.date.min(), end=record.date.max(), freq='D')
full_index = pandas.MultiIndex.from_product([dates, record['Team'].unique()], names=['date', 'Team'])
df = (
    record.groupby(by=['date', 'Team'])
        .size()
        .reindex(full_index)
        .fillna(0)
        .unstack(level='Team')
)
df.head()

现在我们有:

Team        BLUE  GREEN
date                   
2016-12-29   1.0    0.0
2016-12-30   0.0    2.0
2016-12-31   2.0    1.0
2017-01-01   2.0    2.0
2017-01-02   1.0    2.0
2017-01-03   3.0    5.0
2017-01-04   5.0   11.0
2017-01-05   2.0    3.0
2017-01-06   2.0    6.0

我的理解是,大熊猫用分类轴完成所有条形图。这意味着,无论有多少或者后续值之间的定量差距有多大,x值都会被离散地处理并且间隔相等。

但我们可以轻松地使用适当的时间序列(定量)轴来绘制区域图:

fig, ax = pyplot.subplots(figsize=(7, 3), tight_layout=True)
df.plot.area(ax=ax, stacked=True, grid=False, legend=True, colormap='winter')

pandas area plot

如果你真的想要酒吧,请跳过pandas并自己编写matplotlib代码:

fig, ax = pyplot.subplots(figsize=(7, 3), tight_layout=True)
ax.bar(left=df.index, height=df['BLUE'])
ax.bar(left=df.index, height=df['GREEN'], bottom=df['BLUE'])
fig.autofmt_xdate()

enter image description here