1: matplotlib实现折线图数据展示
2: 简单封装, 没有时间整理,
3: 字体的创建, 需要按照版本/系统的不同来配置, 大同小.
code:
import random
import matplotlib
from matplotlib import pyplot as plt
from matplotlib import font_manager
class matplotlib_9527():
def __init__(self):
self.plt = plt
self.set_font()
self.set_canvas_size()
self.set_grid()
# plt.plot( 参数
self.linestyle = "--" # 线条样式 可以16进制标识
self.linewidth = 1 # 线条大小
self.color = 'green' # 字体颜色 或者颜色
self.rotation = 45 # 字体角度
self.loc = 2 # 图例方向
def __del__(self):
try:
self.plt.close()
except:
pass
def set_font(self):
"""设置字体"""
# font = {'family': 'FZYTK.TTF',
# 'weight': 'bold',
# 'size': 'larger'}
# matplotlib.rc('font', family='FZYTK.TTF', weight='bold', size='larger') # pass in the font dict as kwargs
self.my_font = font_manager.FontProperties(fname=r"C:\Windows\Fonts\simsun.ttc", size=18)
matplotlib.rc('figure', figsize=(14, 7))
self.plt.rc('font', size=18)
self.plt.rc('axes', grid=False)
#设置字体为楷体
self.plt.rcParams['font.sans-serif'] = ['KaiTi']
def set_canvas_size(self):
"""设置画布大小"""
self.plt.figure(figsize=(20, 8), dpi=80)
def set_grid(self, clarity_value=0.1):
"""设置网格
clarity_value: 透明度,, 0~1 1 为不透明
"""
self.plt.grid(clarity_value, linestyle="--")
def save_draw(self, save_path="123.png"):
"""保存绘制的图"""
plt.savefig("save_path")
def auto_data(self):
"""自动生成数据"""
x = range(0, 120)
y = [random.randint(20, 35) for i in range(120)]
def set_xy_tick(self, x_data=None, y_data=None):
"""设置刻度标志"""
_xtick_labels = ["10点{}分".format(i) for i in range(60)]
_xtick_labels += ["11点{}分".format(i) for i in range(60)]
_ytick_labels = ["11点{}分".format(i) for i in range(60)]
return _xtick_labels, _ytick_labels
def draw_tick(self, x, y, _xtick_labels, _ytick_labels):
"""画刻度标记"""
self.plt.xticks(x, _xtick_labels, rotation=45, fontproperties=self.my_font)
def set_add_legend(self, loc=2):
"""添加图例 loc 为图例位置 int """
self.plt.legend(loc=loc)
def _max_min(self):
"""标记最大最小点"""
pass
def start_draw(self):
"""开始画图"""
y_1 = [random.randint(0, 6) for i in range(20)]
y_2 = [random.randint(0, 6) for i in range(20)]
# y_3 = [random.randint(0, 6) for i in range(20)]
x = range(11, 31)
self.plt.plot(x, y_1, label="大神的数据", color='green', linestyle=":", linewidth=4)
self.plt.plot(x, y_2, label="屌丝的数据", color='cyan', linestyle="--", linewidth=4)
# self.plt.plot(x, y_3, label="暖男的数据")
_xtick_labels = ["{}岁".format(i) for i in x]
self.plt.xticks(x, _xtick_labels, fontproperties=self.my_font)
# 添加标签
self.plt.xlabel("年龄", fontproperties=self.my_font)
self.plt.ylabel("数量", fontproperties=self.my_font, rotation=45)
self.plt.title("撩妹大师", fontproperties=self.my_font)
# 添加图例
self.set_add_legend(2)
# 显示图形
self.plt.show()
if __name__=="__main__":
plt_obj = matplotlib_9527()
plt_obj.start_draw()