我有以下功能來說明一些等高線:如何在python matplotlib中添加線條到輪廓圖中?
"""
Illustrate simple contour plotting, contours on an image with
a colorbar for the contours, and labelled contours.
See also contour_image.py.
"""
import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'
X = np.arange(-1.2, 1.2, 0.005)
Y = np.arange(-1.2, 1.2, 0.005)
X, Y = np.meshgrid(X, Y)
Z = (np.ones([np.shape(X)[0],np.shape(X)[1]])-X)**2+100*(Y-(X)**2)**2
# Create a simple contour plot with labels using default colors. The
# inline argument to clabel will control whether the labels are draw
# over the line segments of the contour, removing the lines beneath
# the label
levels = np.arange(-100.0, 600, 1.0)
plt.figure()
CS = plt.contour(X,
Y,
Z,
levels=levels,
)
plt.clabel(CS,
np.array(filter(lambda lev: lev <5.0, levels)),
inline=0.5,
fontsize=10,
fmt='%1.1f'
)
plt.hold(True)
plt.plot(np.arange(-1.0, 1.0, 0.005),
np.arange(-1.0, 1.0, 0.005),
np.ones(len(np.arange(-1.0, 1.0, 0.005)))*100, '-k')
plt.title('Contour Lines and Constraint of Rosenbrock Optimiztion Problem')
plt.show()
,如果你行註釋掉的等高線圖看起來不錯....:
# plt.hold(True)
# plt.plot(np.arange(-1.0, 1.0, 0.005),
# np.arange(-1.0, 1.0, 0.005),
# np.ones(len(np.arange(-1.0, 1.0, 0.005)))*100, '-k')
...但我不能讓線條顯示疊加在情節,就像我需要他們。我只是簡單地需要將它們疊加在等高線圖上。做這個的最好方式是什麼?
我知道這是possible in R,但如何在Python
使用matplotlib
做到這一點?
你爲什麼要將三個數組傳遞給'plt.plot'?什麼是第三個數組'np.ones(len(np.arange(-1.0,1.0,0.005)))* 100'應該表示? –
很好的編輯,@ hrbrmstr – guimption
不要垃圾標籤 – hrbrmstr