matplotlib
做为Python
生态中最流行的数据可视化框架,虽然功能很是强大,但默认样式比较简陋,想要制做具备简洁商务风格的图表每每须要编写众多的代码来调整各类参数。python
而今天要为你们介绍的dufte
,就是用来经过简短的代码,对默认的matplotlib
图表样式进行自动改造的Python
库:框架
经过pip install dufte
安装完成后,咱们就能够将dufte
的几个关键API
穿插在常规matplotlib
图表的绘制过程当中,目前主要有如下几种功能:dom
dufte
最重要的功能是其自带的主题风格,而在matplotlib
中有两种设置主题的方式,一种是利用plt.style.use(主题)
来全局设置,通常不建议这种方式。code
另外一种方式则是如下列方式来在with
的做用范围内局部使用主题:orm
# 局部主题设置 with plt.style.context(主题): # 绘图代码 ...
咱们今天就都使用第二种方式,首先导入演示所需的依赖库,并从本地注册思源宋体
:blog
import dufte import numpy as np import matplotlib.pyplot as plt from matplotlib import font_manager # 注册本地思源宋体 fontproperties = font_manager.FontProperties(fname='NotoSerifSC-Regular.otf')
接下来咱们以折线图和柱状图为例:ip
# 折线图示例 with plt.style.context(dufte.style): x = range(100) y = np.random.standard_normal(100).cumsum() fig, ax = plt.subplots(figsize=(10, 5), facecolor='white', edgecolor='white') ax.plot(x, y, linestyle='-.', color='#607d8b') ax.set_xlabel('x轴示例', fontproperties=fontproperties, fontsize=16) ax.set_ylabel('y轴示例', fontproperties=fontproperties, fontsize=16) ax.set_title('折线图示例', fontproperties=fontproperties, fontsize=20) fig.savefig('图2.png', dpi=300, bbox_inches='tight')
# 柱状图示例 with plt.style.context(dufte.style): x = range(25) y = np.random.standard_normal(25) fig, ax = plt.subplots(figsize=(10, 5), facecolor='white', edgecolor='white') ax.bar(x, y) ax.set_xlabel('x轴示例', fontproperties=fontproperties, fontsize=16) ax.set_ylabel('y轴示例', fontproperties=fontproperties, fontsize=16) ax.set_title('柱状图示例', fontproperties=fontproperties, fontsize=20) fig.savefig('图3.png', dpi=300, bbox_inches='tight')
能够看到,dufte
自带了一套简洁的绘图风格,主张去除多余的轴线,只保留必要的参考线,适用于咱们平常工做中的通用出图需求。开发
除了前面介绍的总体主题风格以外,dufte
还自带了一套图例风格化策略,只须要在绘图过程当中利用dufte.legend()
来代替matplotlib
原有的legend()
便可,如下面的折线图为例:it
# 折线图示例 with plt.style.context(dufte.style): x = range(100) y1 = np.random.randint(-5, 6, 100).cumsum() y2 = np.random.randint(-5, 10, 100).cumsum() y3 = np.random.randint(-5, 6, 100).cumsum() fig, ax = plt.subplots(figsize=(10, 5), facecolor='white', edgecolor='white') ax.plot(x, y1, linestyle='dotted', label='Series 1') ax.plot(x, y2, linestyle='dashed', label='Series 2') ax.plot(x, y3, linestyle='dashdot', label='Series 3') ax.set_xlabel('x轴示例', fontproperties=fontproperties, fontsize=16) ax.set_ylabel('y轴示例', fontproperties=fontproperties, fontsize=16) dufte.legend() ax.set_title('dufte.legend()示例', fontproperties=fontproperties, fontsize=20) fig.savefig('图4.png', dpi=300, bbox_inches='tight')
能够看到,对于多系列图表,只须要一行dufte.legend()
就能够自动添加出下列别致的图例说明:pip
不少时候咱们在绘制柱状图时,但愿把每一个柱体对应的y值标注在柱体上,而经过dufte.show_bar_values()
,只要其以前的绘图流程中设置了xticks
,它就会帮咱们自动往柱体上标注信息:
# 柱状图示例 with plt.style.context(dufte.style): x = range(15) y = np.random.randint(5, 15, 15) fig, ax = plt.subplots(figsize=(10, 5), facecolor='white', edgecolor='white') ax.bar(x, y) ax.set_xticks(x) ax.set_xticklabels([f'项目{i}' for i in x], fontproperties=fontproperties, fontsize=10) dufte.show_bar_values() ax.set_xlabel('x轴示例', fontproperties=fontproperties, fontsize=16) ax.set_ylabel('y轴示例', fontproperties=fontproperties, fontsize=16) ax.set_title('柱状图示例', fontproperties=fontproperties, fontsize=20) fig.savefig('图5.png', dpi=300, bbox_inches='tight')
做为一个处于开发初期的库,dufte
将来势必会加入更多的实用功能,感兴趣的朋友能够对其持续关注。
以上就是本文的所有内容,欢迎在评论区分享你的观点与建议。