如何使用matplotlib在python中的图例中的x标记内显示“o”?

2024-09-28 01:26:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要通过在“x”中显示“o”来编辑图例,我用它来标记曲线的某些部分,但我无法解决这个问题,因为我使用了一种复杂的方法来突出显示高和低的部分

fig = plt.figure(figsize=(20, 15))
ax = fig.add_subplot( 2, 1, 1 )
bx = fig.add_subplot( 2, 1, 2 )

ax.plot( time1List, tempList, marker='x', linestyle='', zorder=100 )
ax.plot( time2List, fit_func( time2List, *sol ), zorder=0 )
ax.set_title('Fitting whole MPs on standrad thermal profile ', fontweight='bold', fontsize=25)
ax.set_xlabel('cycles', fontsize=20)
ax.set_ylabel('Thermal regime', fontsize=20)
ax.set_yticks( [-40,-20,0,25,50,75,100,125,150] )
#patch = Patch(facecolor='orange', edgecolor='r', label='Color patch')
#lgd = ax.legend(handles=[time1List, time2List, patch], loc='lower right')
#add_patch(lgd)
#ax.legend(loc='best')

red_patch = mpatches.Patch(color='red', label='High regime')
blue_patch = mpatches.Patch(color='blue', label='Low regime')
plt.legend(handles=[red_patch, blue_patch], loc='best', fontsize=20)

bx.plot( time1List, tempList, marker='x', linestyle='' )
bx.plot( time2List, fit_func( time2List, *sol ) )
bx.plot( rampX, rampY, linestyle='', marker='o', markersize=10, fillstyle='none', color='r')
bx.plot( topX, topY, linestyle='', marker='o', markersize=10, fillstyle='none', color='#00FFAA')
bx.plot( botX, botY, linestyle='', marker='o', markersize=10, fillstyle='none', color='#80DD00')
bx.set_title('Fitting part of MPs on standrad thermal profile ', fontweight='bold', fontsize=25)
bx.set_xlabel('cycles', fontsize=20)
bx.set_ylabel('Temperature [℃]', fontsize=20)
bx.set_xlim( [ 0, 800 ] )
plt.show() 

img

有什么建议可以解决这个问题吗


Tags: addplotfigpltaxmarkerpatchcolor
1条回答
网友
1楼 · 发布于 2024-09-28 01:26:36

你两次密谋要得到那个十字准线标记。你将无法用任何标准方法创建带有十字准线的图例,这种方法与你创建该标记的方法相同

可以使用$\\bigotimes$直接获取标记。那么使用legend就很简单了。只需为绘图中的每个对象定义label,以便在图例中显示:

fig, ax = plt.subplots()

X = np.arange(10)
Y1 = np.arange(10)
Y2 = np.arange(10)/2 + 3

attrs = {'marker': '$\\bigotimes$', 'linestyle': '', 'markersize': 10, 'markeredgewidth': 0.5}
ax.plot(X, Y1, c = 'g', label='Y1', **attrs)
ax.plot(X, Y2, c = 'r', label='Y2', **attrs)
ax.legend()

enter image description here

相关问题 更多 >

    热门问题