How to add marker next to axis label?

Hi all,

I’m trying to add a colored line/marker next to my axis labels to help identify which line corresponds to which axis when I have multiple y-axes and datasets one a plot.

Something like this:

— Label

where the dash would be e.g. red (matching the line color), and the rest of the text stays black.

I seen that there is a “withdash“ argument that’s deprectated but that’s what I would need for my case I guess.

Something like this, with colored dots or dashes next to/in between labels :

Any suggestions?

Thanks

Looks like what you really want is unfortunately an orphaned issue Support placing legend symbols next to axis labels by alexhartl · Pull Request #16005 · matplotlib/matplotlib · GitHub

You might be able to achieve it by text concatenation: Concatenate text objects with different properties — Matplotlib 3.10.8 documentation

1 Like

To be honest, this is only a Matplotlib constraint.

You cannot partly color a dash and leave the remainder black in matplotlib as axis labels are single Text objects until you begin stacking numerous text artists. There is no built-in substitute for withdash.

cleanest solution? Avoid making a rush. To match the line, just color the axis label + ticks:

ax1.set_ylabel("Temperature")
ax1.yaxis.label.set_color("red")
ax1.tick_params(axis='y', colors='red')

Repeat with the color of the second axis. seems cleaner and works well with the layout of matplotlib.

Manual ax.text() hacks are required for anything more than that.