matplotlib dataframe 2 column [dates, non-numerical-data] 堆积条形图定义属性

matplotlib dataframe 2 column [dates, non-numerical-data] stacked bar chart defining attributes

背景:

我已经成功创建了下图,但我对某些元素有困难

免责声明:

下图是我想要实现的,但是我想将我的问题整合到图中 如果有其他方法可以获取包含所有日期的堆叠图,请免费与我分享代码.

问题:

如何定义以下内容:

MWE:

import datetime as dt
import mysql.connector
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime


mycursor.execute(query)
data = mycursor.fetchall()

df = pd.DataFrame(data, columns=['date', 'Operation'])

df['date'] = pd.to_datetime(df.date)

all_dates = pd.date_range('2020-05-01','2020-05-31', freq='D').date

(pd.crosstab(df.date,df.Operation)
.reindex(all_dates)
.plot.bar(stacked=True, color=COLOR_LIST)
)

filename = "\TEST_month_of_{}.png".format("May").lower()
plt.savefig(CURRENT_DIRECTORY + filename)
print("\n\nGenerated: {}".format(CURRENT_DIRECTORY + filename))

数据集:

print(df) 产生以下结果:

date          Operation
2020-05-07        A
2020-05-08        B
2020-05-08        A
2020-05-12        A
2020-05-12        A
2020-05-12        B
2020-05-13        C
2020-05-13        A
2020-05-13        B
2020-05-14        A
2020-05-19        B
2020-05-21        A
2020-05-25        A
2020-05-26        B
2020-05-26        C
2020-05-26        A
2020-05-26        A
2020-05-29        A

至于日期格式,由于地区不同,我无法做到我想要的。此外,使条形宽度变粗的唯一方法是减少数据数量,从而删除不必要的线条。

import pandas as pd
import numpy as np
import io

data = '''
date Operation
2020-05-07 A
2020-05-08 B
2020-05-08 A
2020-05-12 A
2020-05-12 A
2020-05-12 B
2020-05-13 C
2020-05-13 A
2020-05-13 B
2020-05-14 A
2020-05-19 B
2020-05-21 A
2020-05-25 A
2020-05-26 B
2020-05-26 C
2020-05-26 A
2020-05-26 A
2020-05-29 A
'''

df = pd.read_csv(io.StringIO(data), sep='\s+')
df['date'] = pd.to_datetime(df['date'])
all_dates = pd.date_range('2020-05-01','2020-05-31', freq='D').date
df2 = pd.crosstab(df.date,df.Operation).reindex(all_dates)

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(4,8),dpi=100) # Define the chart size (400 by 800)

df2.dropna(inplace=True) # Make the bars more wide

ax = df2.plot.bar(stacked=True)

ax.set_title('this is my chart') # Add a this is my chart title to the chart

ax.set_xlabel('this is x-axis') # Add labels (this is x axis, this is y-axis) to the x & y axis ?
ax.set_ylabel('this is y-axis')

start, end = ax.get_ylim()
ax.yaxis.set_ticks(np.arange(start, end, 1)) # Make the y-axis integers