Insert Function Variable into Graph Title

Insert function variable into graph title

You should use pasteinstead of c:

plot(..., main=paste("Jaccard vs. Tsim for depths",  min.depth, "to",max.depth,"m", sep=" "))

With c you create a vector of strings (hence the stacking), with paste you concatenate them into one single string.

How to name a graph's title using the variable name supplied to as function argument in R?

Just use substitute

labs(title = substitute(system))

How to add a variable to Python plt.title?

You can change a value in a string by using %. Documentation can be found here.

For example:

num = 2
print "1 + 1 = %i" % num # i represents an integer

This will output:

1 + 1 = 2

You can also do this with floats and you can choose how many decimal place it will print:

num = 2.000
print "1.000 + 1.000 = %1.3f" % num # f represents a float

gives:

1.000 + 1.000 = 2.000

Using this in your example to update t in the figure title:

plt.figure(1)
plt.ylabel('y')
plt.xlabel('x')

for t in xrange(50,61):
plt.title('f model: T=%i' %t)

for i in xrange(4,10):
plt.plot(1.0/i,i**2,'ro')

plt.legend
plt.show()

Adding input variables to plot title/legend in Python

Use string formatting with the .format() method:

plt.text(0.1, 2.8, "The gradient is {}, the intercept is {}".format(m, c))

Where m and c are the variables you want to substitute in.

You can directly write the variables like this in Python 3.6+ if you prefix the string with an f whcih denotes a formatted string literal:

f"the gradient is {m}, the intercept is {c}"

R plot title involving a subscript and value of a variable

I don't believe you can paste an expression to a variable (paste/print/cat/bquote/etc). As a workaround, you could use the "Fbelow=" expression as the title and use mtext to insert the value of MyVar, e.g.

MyVar<-0.23
plot(mtcars[2:3], main = expression('F'[below]*'='))
mtext(text = MyVar, side = 3, adj = 0.625, padj = -1.75, cex = 1.5)

example.png

Obviously this isn't ideal, but unless someone else has a clever way of solving your issue this will at least give you a potential option

Pasting value into R plot title

Answer by @MarcoSandri is correct. The correct way of using the example codes you have written are,

title(main= paste("My plot \n mean =", mean(x)))

and

avg <- mean(x)
title(paste("My Plot \n Mean = ", avg))


Related Topics



Leave a reply



Submit