Formatting string in relplot subplot title - string

Please note this is an edited version of my previous post.
I am trying to format the title of subplots generated using relplot in Seaborn. I am using the set_titles option and am including the {col_name} element. I would like to precede {col_name} with a dynamic string from the array of formatted strings "plotvarnames" (e.g., "I_{bs}=") before {col_name}. The following lines of code results in the error message "name 'col_name' is not defined":
plotvars=['nis','Ds','Ibs','kVssH','kVssL','kHL']
plotvarstrings=["r'$\nu_s$'","r'$D_s$'","r'$I_{bs}$'","r'$k_{vSSH}$'","r'$k_{vSSL}$'","r'$k_{HL}$'"]
for i,vname in enumerate (plotvars): g=sns.relplot(x="xvar",y="yvar",col=vname,hue="huevar",col_wrap=3,kind="scatter",palette=["b","r"],data=df,legend="full").set_titles(f"plotvarstrings[i]={col_name}")
How can I obtain the composed, formatted string?

Aha, I figured it out: you need to escape the brackets. Change plotvarstrings to the below (note the double {{ and }}:
plotvarstrings=['r[$\nu_s$]','r[$D_s$]','r'$I_{{bs}}$]','r[$k_{{vSSH}}$]',
'r[$k_{{vSSL}}$]','r[$k_{{HL}}$]']
Then you can do this:
.set_titles(plotvarstrings[i] + "={col_name}")

Related

how do I get rid of leading/trailing spaces in SAS search terms?

I have had to look up hundreds (if not thousands) of free-text answers on google, making notes in Excel along the way and inserting SAS-code around the answers as a last step.
The output looks like this:
This output contains an unnecessary number of blank spaces, which seems to confuse SAS's search to the point where the observations can't be properly located.
It works if I manually erase superflous spaces, but that will probably take hours. Is there an automated fix for this, either in SAS or in excel?
I tried using the STRIP-function, to no avail:
else if R_res_ort_txt=strip(" arild ") and R_kom_lan=strip(" skåne ") then R_kommun=strip(" Höganäs " );
If you want to generate a string like:
if R_res_ort_txt="arild" and R_kom_lan="skåne" then R_kommun="Höganäs";
from three variables, let's call them A B C, then just use code like:
string=catx(' ','if R_res_ort_txt=',quote(trim(A))
,'and R_kom_lan=',quote(trim(B))
,'then R_kommun=',quote(trim(C)),';') ;
Or if you are just writing that string to a file just use this PUT statement syntax.
put 'if R_res_ort_txt=' A :$quote. 'and R_kom_lan=' B :$quote.
'then R_kommun=' C :$quote. ';' ;
A saner solution would be to continue using the free-text answers as data and perform your matching criteria for transformations with a left join.
proc import out=answers datafile='my-free-text-answers.xlsx';
data have;
attrib R_res_ort_txt R_kom_lan length=$100;
input R_res_ort_txt ...;
datalines4;
... whatever all those transforms will be performed on...
;;;;
proc sql;
create table want as
select
have.* ,
answers.R_kommun_answer as R_kommun
from
have
left join
answers
on
have.R_res_ort_txt = answers.res_ort_answer
& have.R_kom_lan = abswers.kom_lan_answer
;
I solved this by adding quotes in excel using the flash fill function:
https://www.youtube.com/watch?v=nE65QeDoepc

How set a standard format specifier to be used throughout a module when using f-strings?

I am using f-strings for formatting strings and injecting variable values into the string, is there a way to set a format-spec for an entire module in python?
I am aware of Standard Format Specifiers which can be used to specify formats for each string, but how do I do it at once for the whole module?
Eg:
f""" Some random string {value1}, some more text {value2:.2f} ... """
Here I am specifying the format for value2, but I want to set a format for all globally.
f"""% profits are {profit}"""
Had I set format spec to {profit:.2f} this will be set to two decimal places, but I want to set that :.2f globally so that number decimal places can be changed with one variable update.
Something like format-spec = ':.2f' and all the f-string injected values should be displayed as floats with two decimals (if its a numbers).
If I understand you correctly, you want to define a string format one time that is usable throughout a script. I would create a function, callable from anywhere in your script that produces the formatted string as shown below:
def create_string(v_str: str, v_num: float) -> str:
return f""" Some random string {v_str}, some more text {v_num:.2f} ... """
You can then use the create_string function from anywhere in your script and produce the desired output as illustrated below:
print(create_string('Some words here', 2.56))
which yields:
Some random string Some words here, some more text 2.56 ...

Insert bracket before a =TEXT()

I have a function that shows a begin- and end date. I wanted to have it between brackets in a cell:
=TEXT(A1;"dd/mm/jjjj")&" - "&TEXT(B1;"dd/mm/jjjj")&")
I have the outher bracket already:
Example ->01/01/2017 - 02/03/2017)
But can't seem to try around and insert a bracket in the beginning.
I have tried:
=TEXT((A1;"dd/mm/jjjj")&" - "&TEXT(B1;"dd/mm/jjjj")&")
=TEXT("("A1;"dd/mm/jjjj")&" - "&TEXT(B1;"dd/mm/jjjj")&")
=TEXT(&"("A1;"dd/mm/jjjj")&" - "&TEXT(B1;"dd/mm/jjjj")&")
"("=TEXT(A1;"dd/mm/jjjj")&" - "&TEXT(B1;"dd/mm/jjjj")&")
Why isn't this working?
Use the concatenate function.
Try this: (Convention Changed)
=concatenate("("; TEXT(A1;"dd/mm/jjjj")&" - "&TEXT(B1;"dd/mm/jjjj"); ")")
You can avoid string concatenation by making the extra characters part of the format mask used by the TEXT function. When using reserved characters or symbols that a format mask usually has another purpose for, precede them with a backslash to convert them to a 'string literal'.
=TEXT(A1; "\(dd/mm/jjjj - ")&TEXT(B1; "dd/mm/jjjj\)")

how to pass date value into my url using python

htmlfile=urllib.request.urlopen("https://hermes.goibibo.com/hotels/v2/search/data/v3/6771549831164675055/{pickUpDate}/{dropOffDate}/1-1_0?s=popularity&cur=INR&f={}&pid=0".format(pickUpDate=pickUpDate, dropOffDate=dropOffDate))
You have three {} pairs but 2 values in your URL. You need to match the {} pairs with the given values.
For instance:
"{v1} is {v2}. {v3}".format(v1="Cat", v2="Animal", v3="Absolutely!")
the string is "Cat is Animal. Absolutely!"
At the end of your string you have "..INR&f={}&pid=0".format().
If this is not a placeholder into which you want to place text via .format(), then change it to have double moustache brackets. i.e:
"...INR&f={{}}&pid=0".format()
This will tell .format() that you really just want the brackets to exist there as a string
So, in general:
>>"{x}: {}".format(x="Hello")
IndexError: tuple index out of range
but
>>"{x}: {{}}".format(x="Hello")
'Hello: {}'
htmlfile=urllib.request.urlopen("https://hermes.goibibo.com/hotels/v2/search/data/v3/6771549831164675055/"+pickUpDate+"/"+dropOffDate+"/1-1_0?s=popularity&cur=INR&f={}&pid=0")

String replacement in latex

I'd like to know how to replace parts of a string in latex. Specifically I'm given a measurement (like 3pt, 10mm, etc) and I'd like to remove the units of that measurement (so 3pt-->3, 10mm-->10, etc).
The reason why I'd like a command to do this is in the following piece of code:
\newsavebox{\mybox}
\sbox{\mybox}{Hello World!}
\newlength{\myboxw}
\newlength{\myboxh}
\settowidth{\myboxw}{\usebox{\mybox}}
\settoheight{\myboxh}{\usebox{\mybox}}
\begin{picture}(\myboxw,\myboxh)
\end{picture}
Basically I create a savebox called mybox. I insert the words "Hello World" into mybox. I create a new length/width, called myboxw/h. I then get the width/height of mybox, and store this in myboxw/h. Then I set up a picture environment whose dimensions correspond to myboxw/h. The trouble is that myboxw is returning something of the form "132.56pt", while the input to the picture environment has to be dimensionless: "\begin{picture}{132.56, 132.56}".
So, I need a command which will strip the units of measurement from a string.
Thanks.
Use the following trick:
{
\catcode`p=12 \catcode`t=12
\gdef\removedim#1pt{#1}
}
Then write:
\edef\myboxwnopt{\expandafter\removedim\the\myboxw}
\edef\myboxhnopt{\expandafter\removedim\the\myboxh}
\begin{picture}(\myboxwnopt,\myboxhnopt)
\end{picture}
Consider the xstring package at https://www.ctan.org/pkg/xstring.
The LaTeX kernel - latex.ltx - already provides \strip#pt, which you can use to strip away any reference to a length. Additionally, there's no need to create a length for the width and/or height of a box; \wd<box> returns the width, while \ht<box> returns the height:
\documentclass{article}
\makeatletter
\let\stripdim\strip#pt % User interface for \strip#pt
\makeatother
\begin{document}
\newsavebox{\mybox}
\savebox{\mybox}{Hello World!}
\begin{picture}(\stripdim\wd\mybox,\stripdim\ht\mybox)
\put(0,0){Hello world}
\end{picture}
\end{document}

Resources