繁体   English   中英

如何用字符串制作多行标题

[英]How to make a multiline title with strings

我是 Python 的新手,正在转换我的一些 MatLab 代码。 我有 3 个字符串,我想用作多行标题。 我不断收到错误“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() 

预先感谢您的帮助!

这应该工作:

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:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM