一行代码让matplotlib图表变高大上
![](https://filescdn.proginn.com/b3bdd7a9c4ac5da105e18f4c240a3e59/0b2b4fdc7adeb12abbba9c05b844f171.webp)
导读:懒人数据可视化必备。
![](https://filescdn.proginn.com/b544e6f408e639de158cce68c2ac86cf/334bcd485179a0c3d0179a4ecb78d1f1.webp)
02 利用dufte自动改造matplotlib图表
1. 主题设置
# 局部主题设置
with plt.style.context(主题):
# 绘图代码
...
import dufte
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import font_manager
# 注册本地思源宋体
fontproperties = font_manager.FontProperties(fname='NotoSerifSC-Regular.otf')
折线图
# 折线图示例
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')
![](https://filescdn.proginn.com/8cbf6d14a0602fdb0cce876df32bbede/7892ba0c9ce411aab79ecbf79a6e91cd.webp)
柱状图
# 柱状图示例
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')
![](https://filescdn.proginn.com/8306295e2e4b60446152733da70461b5/5d7ce4864f1e269cd45b69c4aacedb9b.webp)
2. 自动图例美化
# 折线图示例
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')
![](https://filescdn.proginn.com/dd0b11990ec4a8f9915d6c2dba33afd2/d3be388077c57174e51ed5d533245ac1.webp)
3. 柱状图自动标注
# 柱状图示例
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')
![](https://filescdn.proginn.com/513c6f087c320ae56fa351a7d3c50be0/5e6b653fac66a6abd00b68b87fa8b8da.webp)
![音符](https://filescdn.proginn.com/57d1e02e957c3b1f88ef6ca2a2caad74/1b9cf6078f0f6d28f92f35ea9987ea5c.webp)
![](https://filescdn.proginn.com/ab75afbe37bed0e6cd38d324903bcfaf/681aa04a227c44cc276de94c5d611418.webp)
评论