0

Firstly,

import matplotlib.pyplot as plt
import matplotlib

In an elementary example on how to plot with matplotlib in our lecture I encountered the following line

plt.ylabel(f'g(x)')

What I have tried

In the documentation there's no mention of an additional parameter to be inserted before the actual label text. I further checked out examples where plt.ylabel was used and discovered here that also r seems to be a valid option. Also, I discovered in this example that the "parameter" r can also be used in plt.title, but also in the corresponding documentation I didn't find anything.

Entire Code

plt.figure(figsize=(6,4))
X = np.arange(0,2*np.pi,0.2)
plt.plot(X,np.sin(X),'o-',label='$\sin(x)$')
plt.plot(X,np.cos(X),'*-',c='g', label='$\cos(x)$')
plt.ylabel(f'f(x)' rotation=0)
plt.grid() # adds grid 
_=plt.legend(loc=3)

enter image description here

itroulli
  • 2,044
  • 1
  • 10
  • 21
ViktorStein
  • 103
  • 8
  • Well, not really. In the case of `f'g(x)'` it is completely equivalent to `'g(x)'`. So probably someone just forgot to remove the `f` from some previous code. You can safely ignore the `f` and continue with your course. – ImportanceOfBeingErnest Feb 16 '20 at 21:04
  • @roganjosh I added the whole example, can you please explain what you mean by "whatever that function evaluation returns"? If the label would be $\sin(x)$ or $\cos(x)$, I would get what you are saying, but as it really only says f(x), I don't. – ViktorStein Feb 16 '20 at 21:05
  • @ImportanceOfBeingErnest Ok, can you then explain its usage in the two examples I linked? Surely they don't contain a very similar typo, right? If I use ```plt.ylabel(r'f(x)')``` instead of ```plt.ylabel(f'f(x)')``` the labels looks the same. – ViktorStein Feb 16 '20 at 21:06
  • As you can observe by removing the `f`, it does nothing. – Alexander Feb 16 '20 at 21:13
  • @Alexander Please see my response to 'ImportanceOfBeingErnes'. – ViktorStein Feb 16 '20 at 21:14
  • `f(x)` is the _name_ of the axis, as can be seen on the far left side of the chart. Try `plt.ylabel(f'my label name', rotation=0)` and it becomes obvious. – Alexander Feb 16 '20 at 21:16
  • @ViktorGlombik [r-strings](https://stackoverflow.com/questions/2081640/what-exactly-do-u-and-r-string-flags-do-and-what-are-raw-string-literals) are typically used to tell python you don't want the special interpretation of backslash. In matplotlib very often used when you want mathematical expression in latex format. – JohanC Feb 16 '20 at 21:18
  • It does nothing =) I mean, it could be doing something if you had a string interpolation expression inside the string like print( f"The value of A is ${A}" ). – Todd Feb 16 '20 at 21:20

2 Answers2

3

f stands for format string u can use it everywhere it is not matplotlib specific

example:

x = 12
print(f'Hello {x}') # That will print 'Hello 12'

Also:

print(f'Hello') # Also Works Just fine That will print 'Hello'

check if it is a left over f

  • No, you've made the same mistake as me. It can't be an `f-string`. The original example is missing `{}` – roganjosh Feb 16 '20 at 21:07
  • 2
    @roganjosh No it definitely is an f-string. It's just not formatting anything. – Samie Bencherif Feb 16 '20 at 21:12
  • 1
    Fair. @SamyBencherif the answer doesn't really communicate that, though. It's likely a typo from the person delivering the course and it's just hidden by the fact that it ends up being valid syntax (python 3.6+) – roganjosh Feb 16 '20 at 21:15
  • @roganjosh This is the answer I was looking for, thank you! – ViktorStein Feb 16 '20 at 21:22
  • I doubt it's a "typo". It's either an effort for "consistency" or maybe the author is new to Python and doesn't know exactly why it's there, they only put it there because they saw examples of other format strings. – Todd Feb 16 '20 at 21:23
1

As others have mentioned, the f in your code is probably a leftover from an f-string. You can read more on f-strings here.

As you mentioned, you can also find an r (or R) before a string. This defines a raw string. Raw strings are raw string literals that treat backslash (\) as a literal and not as an escape character.

Example:

dummy_str = "This is a \n normal string"
print(dummy_str)

raw_dummy_str = r"This is a \n raw string"
print(raw_dummy_str)

The above code will print out:

This is a
 normal string
This is a \n raw string

You can read more about raw string here.

itroulli
  • 2,044
  • 1
  • 10
  • 21