How to add newline symbol and multiplication symbol together in matplotlib - python

I am trynig to create a matplotlib label where I can use both newline symbol and multiplication symbol. However, when I use them together then I only see multiplication symbol with '\n' as a part of text. The code that i use to create the symbol is below.
r"L1+\nL1$\times$L2"
Can someone point where I am wrong.

Looks like the solution was easier than I thought. All I had to do was to make a string as a combination of individual strings like "L1+\n"+r'L1$\times$L2 and it works

Related

String formating and Tex rendering

I am running into some issues getting things to display correctly when combining string formating and tex rendering in python. I want to index a series of energy levels by integers. This works fine for single-digit integers, however when I try for example:
s = r"$E_{}$".format(10)
the result looks like E10 while I want it to look like E10. I have tried using double braces but this doesn't seem to work since
s = r"$E_{{}}$".format(10)
results in "E" without any subscript at all, and something like
s = r"$E_{{k}}$".format(k = 10)
predictably gives Ek.
To me it seems the problem here is that both the string formatting and Tex syntax make use of curly braces, and while doubling the braces does escape the formatting, this will not work for me, since I still want to insert the value of k somehow. Is there any way around this issue, or will I have to resort to the old school method of formatting strings?
Literal { can be included with {{ - and then you need another {} to get the formatting placeholder - so you need 3:
s = r"$E_{{{}}}$".format(10)
print(s)
Results in:
$E_{10}$
Which should be rendered into what you wanted.

How to keep track of a symbol in a string for the correct operation? [duplicate]

This question already has answers here:
How to calculate an equation in a string, python
(2 answers)
Closed 2 years ago.
I'm using Python 3.7.4. I was wondering about how to keep track of symbols like: +, /, -, * in a string. But I mean with out the '' and "" in front and behind of it. I'm creating a calculator as my project. This is what it looks like:
When ever you click on one of the buttons it adds that number to a string like user_text = ''. So like a blank string. So say you have in 9 + 9 the string when ever you added it together you get 18. But the problem lies with: +, /, -, *. Cause I know how to turn a string into a number and then add them together or any other way. But, how would you keep track of the symbols in the string and add the numbers in the string to each other with the symbol with it. So, with the correct operation.
I've tried to do: if '+' in len(user_text): print("Yes") but then I realize that it can't iterate a string for int. Anything with range is out of the question I realized too. I was thinking about having like a back up line, but as a list then append what ever was entered onto the list. Then keep track of it. Like say user_list = [] then you added 4 + 4 onto the list user_list = ['4', '+', '4']. But then again how would I keep track of the symbols I said, but then add the 2 strings numbers together as an int to get 8. I just can't think of a way to do something like this. I might be overthinking this but I just can't think of it.
If I can provide anymore information on my issue or anything, let me know. I appreciate the advice and help. Thank you.
Have you considered using python's eval()? Since your calculator probably doesn't use the same operator symbols as python you might have to tweak the resulting string from your calculator to make it work, but it sounds like eval() should do the job for you.

how to pass a string to to an xpath?

I have an xpath in the following format
xpath='//*[#id="peoplegrid"]/div[5]/div[1]/a'
I want to pass a string to the first div in the x path in the following manner
x=[1,2,3,4,5]
for i in x:
xpath='//*[#id="peoplegrid"]/div[',+str(i),']/div[1]/a'
print(xpath)
However when I run this, it states that bad operand type for unary +: 'str'
How can I pass a string to this xpath
Following is the full piece of code I am working on
x=[1,2,3,4]
for i in x:
python_button=driver.find_element_by_xpath('//*[#id="peoplegrid"]/div,'+[str(x)]+'/div[1]/a')
python_button.click()
driver.execute_script("window.history.go(-1)")
You're missing the second +. Try something like this.
xpath='//*[#id="peoplegrid"]/div['+str(i)+']/div[1]/a'
I cannot update #Steven's answer with such a subtle (less than 6 character) change, so I've created my own answer.
What you need is the following:
xpath='//*[#id="peoplegrid"]/div['+str(i)+']/div[1]/a'
As you can tell, between my answer and #Steven's answer, mine does not include the commas within the first div portion of the XPATH. For your purposes, it is unnecessary and invalid to place the commas in the string. Commas are typically used in print statements to prevent newlines from being appended to the output.
EDIT: In regards to the change to the original post and the comments therein, concatenating a list to a string is also invalid. Merely use an interpreter and try compiling the code, and a TypeError: must be str, not list will be thrown. This makes sense, since you cannot append a list directly to a str; however, if the contents of the list are strings, or can be converted to strings, then you can concatenate these. See here for an explanation of string concatentation in Python.
It's best to avoid constructing the XPath expression by string concatenation. Disadvantages:
(a) you need to worry about escaping strings that contain special characters
(b) you need to REALLY worry about it if there's any possibility of people inserting bad strings maliciously (XPath injection attacks)
(c) you have to compile the expression each time it's used.
I don't know the Python API you are using, but most XPath APIs allow you to compile an expression containing variables (eg. '//[#id="peoplegrid"]/div[$param]/div[1]/a') and then bind a value to the variable separately. If you can do this then it's much preferable and avoids the above disadvantages.

python .format including float formatting

I have been aware of .format for a long while now but when I tried to include float formatting in a string like below:
"ab_{}_cd{}_e_{}_{}_{}_val={0:.5f}_{}".format(string1,str(2),string2,string3,string4,str(0.12345678),string5)
In the above example string1-5 denote random strings included for this example.
Returned the following error ValueError: cannot switch from automatic field numbering to manual field specification
After some searching this question does seem to hold the solution Using .format() to format a list with field width arguments
The above link shows I need to format all of the {} in the string to achieve the formatting I want. I have glanced at official documentation https://docs.python.org/2/library/string.html and https://docs.python.org/2/library/stdtypes.html#str.format and there isn't much in the way of explaining what I'm looking for.
Desired output:
An explanation as to how I can convert the automatic field specification of the .format option to a manual field specification with only formatting the float variable I have supplied and leaving all other string1-5 variables unformatted. Whilst I could just use something like round() or a numpy equivalent this might cause issues and I feel like I would learn better with the .format example.
in your code remove the zero before the rounded digit
"ab_{}_cd{}_e_{}_{}_{}_val={:.5f}_{}".format(string1,str(2),string2,string3,string4,str(0.12345678),string5)
NOTE: why it did not work is , you can either mention index in {} as {0},{1} or leave all of them, but you cannot keep few {} with index and few without any index.

python matplotlib axis label subscript based on loop counter

I'm using python and matplotlib to generate graphical output.
I am creating multiple plots within a loop and would like the loop counter to serve as an index on the y-axis label. How do I get the loop counter (a variable) to appear as a subscript?
Here's what I have:
axis_ylabel = plt.ylabel(u"\u03b1 [\u00b0]", rotation='horizontal', position=(0.0,0.9))
resulting in:
α [°]
(I'm using unicode instead of Tex because dvipng is not available on this system.)
I would like something like this:
for i in range(1,3):
axis_ylabel = plt.ylabel(u"\u03b1" + str(i) + u" [\u00b0]", rotation='horizontal', position=(0.0,0.9))
No surprise, this gives:
α1 [°]
α2 [°]
What I really want is the numbers to be subscripts. How do I combine the conversion to a string with a command to create a subscript? Including a '_' is not recognized in the unicode environment to generate a subscript. Additionally, I still need python/matplotlib to recognize that this subscript-command should affect the following variable.
Any thoughts?
Edit
I got this far:
axis_ylabel = plt.ylabel(u"\u03b1" + r"$_" + str(i) + r"$" + u" [\u00b0]", rotation='horizontal', position=(0.0,0.9))
-- this results in a subscript character. However, it is NOT a conversion of the integer value but a different symbol.
Edit 2
I am using python 2.6.6 and matplotlib 0.99.1.1. Inserting any kind of string at <> in r"$<>$" will not result in the display of that string but an entirely different character. I have posted this issue as a new question.
Matplotlib ships its own mathematical expression engine, called mathtext.
From the documentation:
Note that you do not need to have TeX installed, since matplotlib
ships its own TeX expression parser, layout engine and fonts.
So maybe try to use the following:
for i in range(1,3):
plt.ylabel(
r"$\alpha_{:d} [\degree]$".format(i),
rotation='horizontal',
position=(0.0,0.9))
You can also use Unicode in mathtext:
If a particular symbol does not have a name (as is true of many of the
more obscure symbols in the STIX fonts), Unicode characters can also
be used:
ur'$\u23ce$'

Categories

Resources