Joseph Kelsey
Joseph Kelsey

Reputation: 73

How do I include multiple variables in the title of my plot?

I want the title to say "Target Electron Temperature =Te_t [ev] Target Density=n_t [m^-3]" Where Te_t and n_t are the values for the input variables.

I can get it to work with just one of the variables in the title, just not both.

Te_t = float(input("Enter electron tartget temperature [ev]\n"))
n_t = float(input("Enter target density [m^-3]\n")) 

plt.title("Target Electron Temperature =%1.0f" %Te_t ,"[ev] \nTarget Density=%1.1f"%n_t,"[m^-3]")
plt.plot(Ti_t/Te_t, q_par*1e-6)
plt.xlabel("Ti_t/Te_t")
plt.ylabel("Parallel Heat Flux [MW/m^2]")
plt.show()

I am getting the following error in the console: "ValueError: "[m^-3]' is not a valid location"

Upvotes: 1

Views: 10176

Answers (2)

Martin
Martin

Reputation: 644

I like the way of using strings format method:

plt.title("Target Electron Temperature={Te_t}[ev] \nTarget Density={n_t},[m^-3]".format(Te_t=Te_t, n_t=n_t))

Here the {Te_t} and {n_t}is a placeholder, where the values defined in the format method, are inserted.

Upvotes: 1

Sven Harris
Sven Harris

Reputation: 2939

In this case you need to concatenate them into a single string using the + operator instead of passing them in as 3 separate parameters into the title function:

plt.title("Target Electron Temperature =%1.0f" %Te_t + "[ev] \nTarget Density=%1.1f"%n_t + "[m^-3]")

Upvotes: 3

Related Questions