Home > other >  How to make a multiline title with strings
How to make a multiline title with strings

Time:03-21

I am new to using Python and am converting some of my MatLab code. I have 3 strings that I would like to use as a multi-line title. I keep getting the error"TypeError: unhashable type: 'list'".

# importing the required module 
import matplotlib.pyplot as plt 
    
# x axis values 
x = [1,2,3] 
# corresponding y axis values 
y = [2,4,1] 

wx = 1
wy = 2
wz = 3
v0 = 50

# Create title strings:
line1 = ['Position']
line2 = ['$w_x =$ ',str(wx),' ft/s, $w_y =$ ', str(wy),' ft/s,  $w_z =$ ', str(wz),' ft/s']
line3 = ['$V_0$ = ',str(v0),' ft/sec'] 

# plotting the points  
plt.plot(x, y) 
    
# naming the x axis 
plt.xlabel('x - axis') 
# naming the y axis 
plt.ylabel('y - axis') 
    
# giving a title to my graph 
plt.title({line1, line2, line3})
    
# function to show the plot 
plt.show() 

Thank you in advance for your help!

CodePudding user response:

This should work:

import matplotlib.pyplot as plt 
    
# x axis values 
x = [1,2,3] 
# corresponding y axis values 
y = [2,4,1] 

wx = 1
wy = 2
wz = 3
v0 = 50

# Create title strings:
line1 = ['Position', '\n']
line2 = ['$w_x =$ ',str(wx),' ft/s, $w_y =$ ', str(wy),' ft/s,  $w_z =$ ', str(wz),' ft/s', '\n']
line3 = ['$V_0$ = ',str(v0),' ft/sec', '\n']

# plotting the points  
plt.plot(x, y) 
    
# naming the x axis 
plt.xlabel('x - axis') 
# naming the y axis 
plt.ylabel('y - axis') 
    
# giving a title to my graph 
title = ''
for i in line1: title  = i   ' '
title  = '\n'
for i in line2: title  = i   ' '
title  = '\n'
for i in line3: title  = i   ' '
plt.title(title)
    
# function to show the plot 
plt.show() 

Output:

  • Related