python海运图标题或脚注是否包含变量?

ni65a41a  于 2022-12-02  发布在  Python
关注(0)|答案(1)|浏览(121)

I'd like to be able to include a variable in either the title (subtitle) or footnote text on a seaborn pairplot -- I'm selecting for a date range and specifically want to add the dates and a name. I can easily put a static title on the pairplot and it runs as expected.
Here's the code:

subData = rslt_avgData[['averageSpO2', 'averageHR', 'averageRR', 'averagePVi', 'averagePi']]
g=sns.pairplot(subData, hue = 'averageSpO2', palette= 'dark:salmon')
g.fig.suptitle("Average SpO2 compared to average of other variables, selected dates", y=1.05, fontweight='bold')
g.fig.text('Event Name: ', eventName,
           '\nStart Date: ', startDate,
          "\nEnd Date: ", endDate)

Here's the error:

----> 7 g.fig.text('Event Name: ', eventName,
      8            '\nStart Date: ', startDate,
      9           "\nEnd Date: ", endDate)

TypeError: text() takes from 4 to 5 positional arguments but 7 were given

If I try just using the fixed text, I get this error:

----> 7 g.fig.text('Event Name: \nStart Date: \nEnd Date: ')

TypeError: text() missing 2 required positional arguments: 'y' and 's'

If I try when specifying location (x,y), and the text (s), like this: g.fig.text(x=0, y=-0.5, s='Event Name: ',eventName,'\nStart Date: \nEnd Date: ') (whether I put the x and y at the beginning or the end...)
I get this error:

g.fig.text(x=0, y=-0.5, s='Event Name: ',eventName,'\nStart Date: \nEnd Date: ')
                                                                                   ^
SyntaxError: positional argument follows keyword argument
iyzzxitl

iyzzxitl1#

Seaborn is based on Matplotlib and you can use the syntax from matplotlib.pyplot.text()
text(x, y, s, fontdict=None, **kwargs) Add text to the Axes.
Add the text s to the Axes at location x, y in data coordinates.
Parameters:

x, y: float

The position to place the text. By default, this is in data coordinates. >The coordinate system can be changed using the transform parameter.

s: str

The text.

fontdictdict, default: None

A dictionary to override the default text properties. If fontdict is None, the defaults are determined by rcParams.
To use this function, you have to give always the positions x and y ans a string s . Keyword Arguments are optional, ant with this you can set the font style and text size.
It is not allowed to write something like text(x=1, 2) . All parameters, without a parameter assignment have to be at the beginning of the function call. Afterwars all arguments have to have a keayword. text(x=1, y=2) is valid.
In your last line the problem is this:

g.fig.text(x=0, y=-0.5, s='Event Name: ', eventName,'\nStart Date: \nEnd Date: ')
                                                  ^ This has to be "="

Because eventName is not a valid keyword, nothing should happen. See the linked documentation for more details.

相关问题