![](/img/trans.png)
[英]I'm having trouble plotting a 2D graph alongside a 3D graph using mat plot lib
[英]I am plotting a 3d plot and i want the colours to be less 'distinct'
我的代碼可以在這里看到:
import numpy as np
from matplotlib import cm
import mpl_toolkits.mplot3d.axes3d as axes3d
from matplotlib.ticker import LinearLocator, FormatStrFormatter
xlist = [+30,+20,+10,0,-10,-20,-30]
ylist = [0.0008,0.0009, 0.001, 0.0012, 0.0013]
total_costs=[[2084.8771849999903, 17314.19051000003, 26026.73173, 65340.709810000015, 108130.0746, 143560.64033000002, 188387.24033], [2129.155209999997, 17314.301310000024, 26026.996729999984, 65341.17821, 108130.792, 143561.44293000002, 188388.11793], [6637.1766100000095, 17314.412110000034, 26027.26173, 65341.646609999996, 108131.5094, 143562.24553000001, 188388.99553], [6623.21941000002, 17314.63371000004, 26027.791729999997, 65342.58341000001, 108132.9442, 150322.81264000002, 191661.16901], [6637.240810000003, 17314.744510000033, 26028.05673000001, 65343.05181000002, 110971.15911000001, 146393.01711000002, 191661.93621]]
Z = np.array(total_costs)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
X, Y = np.meshgrid(xlist, ylist)
ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False,alpha=0.5,
`rstride=1,cstride=1, label='skata')`
ax.set_xlabel('System-1 imbalance')
ax.set_ylabel('Penalization factor [€/MWh]')
ax.set_zlabel('Total balancing costs [€]')
#ax.set_legend('upper left', fontsize=15)
#ax.tick_params(axis='both', labelsize=15)
plt.show()
當我運行這個我得到這樣的數字:
我想要得到的數字是這樣的:
我想這與我的結果是具有離散值的列表中的列表有關。 有人知道嗎? 先感謝您
我想您希望圖形上的色調逐漸變化-我知道該怎么做的方法是“簡單地”增加使用插值法繪制的點數:
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d
import scipy.interpolate as interp
xlist = np.array([+30, +20, +10, 0, -10, -20, -30])
ylist = np.array([0.0008, 0.0009, 0.001, 0.0012, 0.0013])
total_costs = [[2084.8771849999903, 17314.19051000003, 26026.73173,
65340.709810000015, 108130.0746, 143560.64033000002,
188387.24033],
[2129.155209999997, 17314.301310000024, 26026.996729999984,
65341.17821, 108130.792, 143561.44293000002, 188388.11793],
[6637.1766100000095, 17314.412110000034, 26027.26173,
65341.646609999996, 108131.5094, 143562.24553000001,
188388.99553],
[6623.21941000002, 17314.63371000004, 26027.791729999997,
65342.58341000001, 108132.9442, 150322.81264000002,
191661.16901],
[6637.240810000003, 17314.744510000033, 26028.05673000001,
65343.05181000002, 110971.15911000001, 146393.01711000002,
191661.93621]]
X, Y = np.meshgrid(xlist, ylist)
Z = np.asarray(total_costs)
Zfunc = interp.interp2d(X, Y, Z, kind='cubic', copy=False)
n_points = 100 # change this to change the "resolution"
xnew = np.linspace(start=min(xlist), stop=max(xlist), num=n_points)
ynew = np.linspace(start=min(ylist), stop=max(ylist), num=n_points)
Xnew, Ynew = np.meshgrid(xnew, ynew)
Znew = Zfunc(xnew, ynew)
fig = plt.figure(figsize=(11, 8))
ax = plt.axes([0.05, 0.05, 0.9, 0.9], projection='3d')
surface = ax.plot_surface(Xnew, Ynew, Znew, rstride=1, cstride=1,
cmap='coolwarm', linewidth=0.25)
fig.colorbar(surface, shrink=0.75, aspect=9)
plt.show()
曲面圖的面根據Z
值着色。
要在面孔上獲得混合色或隨機色,可以提供帶有facecolors
參數的顏色數組,而不是顏色圖。
colors=np.random.rand(X.shape[0]-1,X.shape[1]-1, 3)
ax.plot_surface(X, Y, Z, facecolors=colors,
linewidth=0, antialiased=False,alpha=0.5,
rstride=1,cstride=1, label='skata')
產生
為了使顏色看起來更加接近,解決方案是不使用顏色圖的完整范圍。 例如,您可以在對plot_surface
的調用中設置vmin=0.5*Z.min(), vmax=2*Z.max(),
以便將顏色映射到比圖像中顯示的顏色大得多的范圍,從而實際值僅覆蓋顏色圖的一部分。
ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, vmin=0.5*Z.min(), vmax=2*Z.max(),
linewidth=0, antialiased=False,alpha=0.5,
rstride=1,cstride=1, label='skata')
你是這個意思嗎?
def stackQuestion():
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d
xlist = np.array([+30,+20,+10,0,-10,-20,-30])
ylist = np.array([0.0008,0.0009, 0.001, 0.0012, 0.0013])
total_costs=[[2084.8771849999903, 17314.19051000003, 26026.73173, 65340.709810000015, 108130.0746, 143560.64033000002, 188387.24033],
[2129.155209999997, 17314.301310000024, 26026.996729999984, 65341.17821, 108130.792, 143561.44293000002, 188388.11793],
[6637.1766100000095, 17314.412110000034, 26027.26173, 65341.646609999996, 108131.5094, 143562.24553000001, 188388.99553],
[6623.21941000002, 17314.63371000004, 26027.791729999997, 65342.58341000001, 108132.9442, 150322.81264000002, 191661.16901],
[6637.240810000003, 17314.744510000033, 26028.05673000001, 65343.05181000002, 110971.15911000001, 146393.01711000002, 191661.93621]]
X, Y = np.meshgrid(xlist, ylist)
Z = np.array(total_costs)
fig = plt.figure(figsize = (11, 8))
ax = plt.axes([0.05, 0.05, 0.9, 0.9], projection = '3d')
surface = ax.plot_surface(X, Y, Z, rstride = 1, cstride = 1,\
cmap = 'coolwarm', linewidth = 0.25)
fig.colorbar(surface, shrink = 0.75, aspect = 9)
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.