matplotlib-坐标轴样式探索
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.rcParams['font.sans-serif'] = ['SimHei']
data = pd.read_excel('示例文件.xlsx', sheet_name=2, encoding='GBK')
data['日期'] = pd.to_datetime(data['日期'])
data['毛利润'] = data['销量数量'] * (data['单价'] - data['进价'])
simple_profit = data.groupby(data['日期'].dt.date)[['毛利润']].sum()
simple_profit.index = pd.to_datetime(simple_profit.index)
时间格式的x坐标刻度
一般时间格式
plt.figure(figsize=(10, 6))
plt.style.use('ggplot')
plt.bar(x=simple_profit.index, height=simple_profit['毛利润'], width=0.4)
plt.title('毛利润', fontdict={'fontsize': 40})
plt.yticks(size=16)
plt.grid(False)
plt.show()
自定义时间显示格式
样式一
plt.figure(figsize=(10, 6))
plt.style.use('ggplot')
plt.bar(x=simple_profit.index, height=simple_profit['毛利润'], width=0.4)
plt.title('毛利润', fontdict={'fontsize': 40})
plt.xticks(simple_profit.index, labels=simple_profit.index.strftime('%Y年%m月%d日'), size=16, rotation=45)
plt.yticks(size=16)
plt.grid(False)
plt.show()
样式二
plt.figure(figsize=(10, 6))
plt.style.use('ggplot')
plt.bar(x=simple_profit.index, height=simple_profit['毛利润'], width=0.4)
plt.title('毛利润', fontdict={'fontsize': 40})
plt.xticks(simple_profit.index, labels=simple_profit.index.strftime('%m月%d日'), size=16, rotation=45)
plt.yticks(size=16)
plt.grid(False)
plt.show()
y坐标刻度样式
- 普通样式(正常数值型)
- 带单位
- 数级
带单位
plt.figure(figsize=(10, 6))
plt.style.use('ggplot')
plt.bar(x=simple_profit.index, height=simple_profit['毛利润'], width=0.4)
plt.title('毛利润', fontdict={'fontsize': 40})
plt.xticks(simple_profit.index, labels=simple_profit.index.strftime('%m月%d日'), size=16, rotation=45)
plt.yticks(size=16)
value = [i for i in range(2500, 20001, 2500)]
plt.yticks(ticks=value, labels=['{}元'.format(i) for i in value])
plt.grid(False)
plt.show()
K代表千
plt.figure(figsize=(10, 6))
plt.style.use('ggplot')
plt.bar(x=simple_profit.index, height=simple_profit['毛利润'], width=0.4)
plt.title('毛利润', fontdict={'fontsize': 40})
plt.xticks(simple_profit.index, labels=simple_profit.index.strftime('%m月%d日'), size=16, rotation=30)
plt.yticks(size=16)
value = [i for i in range(2500, 20001, 2500)]
plt.yticks(ticks=value, labels=['{}K'.format(i/1000) for i in value])
plt.grid(False)
plt.show()