String Literals and Plotting with Matplotlib

Question:

I’d like to plot an amplitude spectral density for a device with matplotlib. Typical units for these kinds of plots are sensor_units/Hz^(1/2). Because I have a few different kinds of sensors, I’d like to pass the correct sensor_units as each time I plot, so I’ve written the axis label as

r'Coupling {}$/sqrt{mathrm{Hz}}$'.format(units)

this gives the error

ValueError: unexpected '{' in field name

What do I need to change to get this working?

Asked By: user3517167

||

Answers:

Double each { and } that should be interpreted literally.

r'Coupling {}$/sqrt{{mathrm{{Hz}}}}$'.format(units)
Answered By: Unmitigated
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.