Matplotlib 画图不是玄学折线、柱状、散点、饼图一套带走“为什么我的中文全是方框” “图例怎么跑到画面外面去了”Matplotlib 功能强大但上手曲线有点陡。今天把最常用的四种图讲透外加中文乱码解决方案。基础概念figure 和 axesimportmatplotlib.pyplotaspltimportnumpyasnp# figure 整张画布axes 坐标系一张图fig,axplt.subplots(figsize(8,5))ax.plot([1,2,3],[1,4,9])ax.set_title(简单折线图)plt.show()# 多子图fig,axesplt.subplots(2,2,figsize(10,8))axes[0,0].plot([1,2,3],[1,2,3])axes[0,1].bar([A,B,C],[10,20,15])axes[1,0].scatter([1,2,3],[5,8,6])axes[1,1].pie([30,40,30],labels[A,B,C])plt.tight_layout()plt.show()中文乱码一行解决plt.rcParams[font.sans-serif][SimHei]# 黑体plt.rcParams[axes.unicode_minus]False# 负号正常显示Windows 一般有 SimHei/微软雅黑Mac 用[Arial Unicode MS]或[PingFang SC]。不行就下载中文字体文件指定路径。折线图months[1月,2月,3月,4月,5月,6月]sales[120,135,158,142,175,190]profit[30,38,45,40,52,60]fig,axplt.subplots(figsize(10,5))ax.plot(months,sales,markero,linewidth2,label销售额,color#378ADD)ax.plot(months,profit,markers,linewidth2,label利润,color#D85A30)ax.set_title(2026上半年销售趋势,fontsize14,fontweightbold)ax.set_xlabel(月份)ax.set_ylabel(金额万元)ax.legend()ax.grid(True,alpha0.3)plt.tight_layout()plt.savefig(line_chart.png,dpi150,bbox_inchestight)plt.show()柱状图products[手机,电脑,耳机,平板,手表]sales_q1[150,120,200,80,60]sales_q2[180,110,220,95,75]xnp.arange(len(products))width0.35fig,axplt.subplots(figsize(10,5))bars1ax.bar(x-width/2,sales_q1,width,labelQ1,color#378ADD)bars2ax.bar(xwidth/2,sales_q2,width,labelQ2,color#D85A30)# 柱子上标数值forbarinbars1:ax.text(bar.get_x()bar.get_width()/2,bar.get_height()2,str(bar.get_height()),hacenter,fontsize9)forbarinbars2:ax.text(bar.get_x()bar.get_width()/2,bar.get_height()2,str(bar.get_height()),hacenter,fontsize9)ax.set_xticks(x)ax.set_xticklabels(products)ax.set_title(各产品季度销量对比,fontsize14,fontweightbold)ax.legend()plt.tight_layout()plt.show()散点图np.random.seed(42)n200heightnp.random.normal(170,8,n)weightheight*0.55np.random.normal(0,8,n)fig,axplt.subplots(figsize(8,6))scatterax.scatter(height,weight,cweight,cmapReds,alpha0.6,edgecolorswhite)plt.colorbar(scatter,label体重(kg))ax.set_xlabel(身高(cm))ax.set_ylabel(体重(kg))ax.set_title(身高与体重关系,fontsize14,fontweightbold)plt.tight_layout()plt.show()饼图categories[手机,电脑,耳机,平板,其他]values[35,25,20,12,8]colors[#378ADD,#D85A30,#1D9E75,#BA7517,#888780]explode(0.05,0,0,0,0)# 突出第一块fig,axplt.subplots(figsize(8,8))wedges,texts,autotextsax.pie(values,labelscategories,autopct%1.1f%%,colorscolors,explodeexplode,startangle90,textprops{fontsize:11})ax.set_title(产品销售额占比,fontsize14,fontweightbold)plt.tight_layout()plt.show()样式全局设置plt.style.use(ggplot)# 换个主题# 可用主题: ggplot, seaborn-v0_8, fivethirtyeight, bmh, dark_background# 自定义全局参数plt.rcParams.update({figure.figsize:(10,6),font.size:12,axes.titlesize:14,axes.titleweight:bold,})新手常见坑坑1不调tight_layout图例、标签被裁掉加一行plt.tight_layout()。坑2plt.show()后画布清空了想在plt.show()后再保存不行——画布已清空。要么先savefig再show要么用面向对象式fig, ax。坑3中文字体不存在importmatplotlib.font_managerasfm# 查看可用中文字体fonts[f.nameforfinfm.fontManager.ttflistifany(kinf.nameforkin[Hei,Song,Ming,YaHei])]print(fonts)写在最后Matplotlib 不优雅但万能。画简单图用它的 pyplot API 就够复杂图用面向对象式figure axes。下一篇聊 Seaborn——Matplotlib 的高级封装一行代码画出统计图。