我是使用 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()
預先感謝您的幫助!
uj5u.com熱心網友回復:
這應該有效:
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()
輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/447243.html
