Python实例:仅绘制图例而不绘制实际的图形
作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/
Python实例:仅绘制图例而不绘制实际的图形,使用线条来表示不同的数据系列(即使这些数据系列在图中没有实际表示)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | # Python实例:仅绘制图例而不绘制实际的图形 # 使用线条来表示不同的数据系列(即使这些数据系列在图中没有实际表示) # -*- coding: utf-8 -*- # Author:凯鲁嘎吉 Coral Gajic # https://www.cnblogs.com/kailugaji/ import matplotlib.pyplot as plt from matplotlib.legend import Legend plt.rcParams[ 'font.size' ] = 15 plt.rcParams[ 'font.family' ] = 'Times New Roman' fig, ax = plt.subplots(figsize = ( 10 , 6 )) # 图长宽比例 color = [ 'darkgoldenrod' , 'orange' , '#c56cf0' , '#20bf6b' , '#747d8c' , '#f78fb3' , 'b' , 'r' ] label = [ 'Algorithm 1' , 'Algorithm 2' , 'Algorithm 3' , 'Algorithm 4' , 'Algorithm 5' , 'Algorithm 6' , 'Algorithm 7' , 'Algorithm 8' ] # 创建一个空白的线条列表,用于图例 lines = [plt.Line2D([ 0 ], [ 0 ], color = color, label = label) for label, color in zip (label, color)] # 使用ax.add_artist()添加图例到图表上 legend = Legend(ax, lines, [line.get_label() for line in lines], loc = 'center' , # lower center ncol = int ( len (label) / 2 ), # 变成一行:int(len(label)) bbox_to_anchor = ( 0.5 , 0.5 ), # (0.5, -0.1) frameon = False ) ax.add_artist(legend) ax.axis( 'off' ) plt.savefig( "Plot_Legend.jpg" , bbox_inches = 'tight' , dpi = 500 ) plt.show() |
结果:
标签:
Python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决