如何用字符串制作多行标题
How to make a multiline title with strings
我是 Python 的新手,正在转换我的一些 MatLab 代码。我有 3 个字符串想用作 multi-line 标题。我不断收到错误消息“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()
输出:
我是 Python 的新手,正在转换我的一些 MatLab 代码。我有 3 个字符串想用作 multi-line 标题。我不断收到错误消息“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()
输出: