指定facecolor后,mplot3d surf中的边线消失 [英] Edgelines vanish in mplot3d surf when facecolors are specified

查看:101
本文介绍了指定facecolor后,mplot3d surf中的边线消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在matlab中制作了以下曲面图:

I have produced the following surface plot in matlab:

,我需要在.NET中创建它.我希望使用IronPython来做到这一点.但是首先,我只是想在Python(PyLab)中创建情节.这是我到目前为止的内容:

and I need to create this in .NET instead. I'm hoping to use IronPython to do this. But first I am just trying to create the plot in Python (PyLab). This is what I have so far:

请查看我的代码,并告诉我如何获取python以显示黑色边缘线.当我将facecolors=UWR(heatmap)属性添加到surf(...)时,这些似乎消失了.这是mplot3d中的bug还是设计使然?无论哪种方式,我该如何找回线路?

Please have a look at my code and tell me how I can get python to show the black edge lines. It appear that these disappear when I add the facecolors=UWR(heatmap) property to the surf(...). Is this a bug in mplot3d or is it by design? Either way how do I get the lines back?

这是我的代码(对巨大数据矩阵的歉意):

Here is my code (Apologies for the huge data matrices):

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import matplotlib
from pylab import *
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

#Sample colour data
heatmap = np.array([(0.304, 0.288,  0.284,  0.26,   0.248,  0.224,  0.204,  0.184,  0.18,   0.18,   0.156,  0.148,  0.144,  0.136,  0.136,  0.128,  0.124,  0.124,  0.128,  0.124,  0.124),
                    (0.356, 0.348,  0.332,  0.328,  0.308,  0.292,  0.288,  0.272,  0.252,  0.232,  0.216,  0.204,  0.16,   0.148,  0.152,  0.148,  0.132,  0.124,  0.124,  0.132,  0.144),
                    (0.396, 0.384,  0.372,  0.36,   0.34,   0.316,  0.312,  0.312,  0.3,    0.272,  0.244,  0.236,  0.216,  0.192,  0.176,  0.168,  0.148,  0.148,  0.156,  0.156,  0.16),
                    (0.452, 0.444,  0.428,  0.408,  0.388,  0.376,  0.364,  0.348,  0.336,  0.336,  0.3,    0.284,  0.264,  0.256,  0.24,   0.244,  0.212,  0.2,    0.22,   0.224,  0.224),
                    (0.488, 0.476,  0.464,  0.444,  0.424,  0.4,    0.4,    0.384,  0.38,   0.372,  0.356,  0.324,  0.312,  0.312,  0.312,  0.312,  0.308,  0.292,  0.304,  0.332,  0.344),
                    (0.492, 0.492,  0.48,   0.468,  0.452,  0.432,  0.424,  0.412,  0.404,  0.396,  0.396,  0.392,  0.376,  0.356,  0.356,  0.36,   0.368,  0.372,  0.392,  0.404,  0.42),
                    (0.5,   0.5,    0.5,    0.484,  0.46,   0.452,  0.444,  0.436,  0.44,   0.44,   0.44,   0.452,  0.44,   0.436,  0.424,  0.42,   0.404,  0.44,   0.452,  0.468,  0.5),
                    (0.484, 0.48,   0.46,   0.444,  0.44,   0.44,   0.44,   0.44,   0.444,  0.44,   0.456,  0.456,  0.46,   0.448,  0.448,  0.448,  0.436,  0.456,  0.468,  0.492,  0.492),
                    (0.405737704918033, 0.401639344262295,  0.409836065573771,  0.418032786885246,  0.434426229508197,  0.438524590163934,  0.438524590163934,  0.44672131147541,   0.454918032786885,  0.471311475409836,  0.467213114754098,  0.479508196721311,  0.487704918032787,  0.487704918032787,  0.479508196721311,  0.483606557377049,  0.495901639344262,  0.516393442622951,  0.520491803278689,  0.532786885245902,  0.536885245901639),
                    (0.320987654320988, 0.329218106995885,  0.349794238683128,  0.362139917695473,  0.374485596707819,  0.395061728395062,  0.42798353909465,   0.440329218106996,  0.465020576131687,  0.477366255144033,  0.48559670781893,   0.493827160493827,  0.506172839506173,  0.518518518518519,  0.51440329218107,   0.518518518518519,  0.547325102880658,  0.555555555555556,  0.555555555555556,  0.584362139917696,  0.580246913580247),
                    (0.282700421940928, 0.29535864978903,   0.30379746835443,   0.320675105485232,  0.337552742616034,  0.354430379746835,  0.383966244725738,  0.434599156118144,  0.464135021097046,  0.485232067510549,  0.493670886075949,  0.514767932489452,  0.527426160337553,  0.535864978902954,  0.544303797468354,  0.561181434599156,  0.594936708860759,  0.59915611814346,   0.590717299578059,  0.60337552742616,   0.607594936708861),
                    (0.230434782608696, 0.256521739130435,  0.273913043478261,  0.304347826086957,  0.334782608695652,  0.360869565217391,  0.373913043478261,  0.408695652173913,  0.469565217391304,  0.504347826086957,  0.521739130434783,  0.539130434782609,  0.552173913043478,  0.560869565217391,  0.578260869565217,  0.6,    0.617391304347826,  0.61304347826087,   0.61304347826087,   0.617391304347826,  0.643478260869565),
                    (0.161137440758294, 0.175355450236967,  0.218009478672986,  0.28436018957346,   0.327014218009479,  0.341232227488152,  0.388625592417062,  0.436018957345972,  0.488151658767773,  0.516587677725119,  0.549763033175356,  0.573459715639811,  0.578199052132701,  0.592417061611374,  0.611374407582938,  0.649289099526066,  0.658767772511848,  0.658767772511848,  0.677725118483412,  0.66824644549763,   0.691943127962085),
                    (0.224719101123596, 0.269662921348315,  0.303370786516854,  0.365168539325843,  0.382022471910112,  0.404494382022472,  0.443820224719101,  0.48876404494382,   0.5,    0.556179775280899,  0.567415730337079,  0.612359550561798,  0.612359550561798,  0.629213483146067,  0.634831460674157,  0.646067415730337,  0.662921348314607,  0.685393258426966,  0.707865168539326,  0.707865168539326,  0.724719101123596),
                    (0.333333333333333, 0.363636363636364,  0.401515151515152,  0.431818181818182,  0.446969696969697,  0.46969696969697,   0.515151515151515,  0.53030303030303,   0.553030303030303,  0.583333333333333,  0.613636363636364,  0.621212121212121,  0.636363636363636,  0.643939393939394,  0.651515151515152,  0.651515151515152,  0.666666666666667,  0.666666666666667,  0.674242424242424,  0.681818181818182,  0.696969696969697),
                    (0.373626373626374, 0.406593406593407,  0.483516483516484,  0.505494505494506,  0.527472527472528,  0.54945054945055,   0.571428571428571,  0.582417582417583,  0.593406593406593,  0.637362637362637,  0.659340659340659,  0.681318681318681,  0.692307692307692,  0.692307692307692,  0.703296703296703,  0.692307692307692,  0.703296703296703,  0.736263736263736,  0.736263736263736,  0.703296703296703,  0.67032967032967),
                    (0.484375,  0.5625, 0.578125,   0.578125,   0.578125,   0.625,  0.625,  0.640625,   0.65625,    0.671875,   0.703125,   0.734375,   0.75,   0.734375,   0.734375,   0.75,   0.734375,   0.640625,   0.65625,    0.625,  0.609375),
                    (0.617647058823529, 0.617647058823529,  0.617647058823529,  0.617647058823529,  0.617647058823529,  0.588235294117647,  0.588235294117647,  0.588235294117647,  0.617647058823529,  0.647058823529412,  0.676470588235294,  0.705882352941177,  0.676470588235294,  0.705882352941177,  0.705882352941177,  0.735294117647059,  0.705882352941177,  0.705882352941177,  0.735294117647059,  0.705882352941177,  0.647058823529412),
                    (0.6,   0.6,    0.6,    0.6,    0.6,    0.6,    0.6,    0.5,    0.5,    0.5,    0.5,    0.5,    0.4,    0.4,    0.4,    0.4,    0.3,    0.3,    0.3,    0.4,    0.4)
]);

#Sample Z data
volatility = np.array([(0.2964396,  0.28628612, 0.27630128, 0.26648508, 0.25683752, 0.2473586,  0.23804832, 0.22890668, 0.21993368, 0.21112932, 0.2024936,  0.19402652, 0.18572808, 0.17759828, 0.16963712, 0.1618446,  0.15422072, 0.14676548, 0.13947888, 0.13236092, 0.1254116),
                       (0.2979793,  0.287974509333333,  0.278154444,    0.268519104,    0.259068489333333,  0.2498026,  0.240721436,    0.231824997333333,  0.223113284,    0.214586296,    0.206244033333333,  0.198086496,    0.190113684,    0.182325597333333,  0.174722236,    0.1673036,  0.160069689333333,  0.153020504,    0.146156044,    0.139476309333333,  0.1329813),
                       (0.299519,   0.289662898666667,  0.280007608,    0.270553128,    0.261299458666667,  0.2522466,  0.243394552,    0.234743314666667,  0.226292888,    0.218043272,    0.209994466666667,  0.202146472,    0.194499288,    0.187052914666667,  0.179807352,    0.1727626,  0.165918658666667,  0.159275528,    0.152833208,    0.146591698666667,  0.140551),
                       (0.3010587,  0.291351288,    0.281860772,    0.272587152,    0.263530428,    0.2546906,  0.246067668,    0.237661632,    0.229472492,    0.221500248,    0.2137449,  0.206206448,    0.198884892,    0.191780232,    0.184892468,    0.1782216,  0.171767628,    0.165530552,    0.159510372,    0.153707088,    0.1481207),
                       (0.3025984,  0.293039677333333,  0.283713936,    0.274621176,    0.265761397333333,  0.2571346,  0.248740784,    0.240579949333333,  0.232652096,    0.224957224,    0.217495333333333,  0.210266424,    0.203270496,    0.196507549333333,  0.189977584,    0.1836806,  0.177616597333333,  0.171785576,    0.166187536,    0.160822477333333,  0.1556904),
                       (0.3041381,  0.294728066666667,  0.2855671,  0.2766552,  0.267992366666667,  0.2595786,  0.2514139,  0.243498266666667,  0.2358317,  0.2284142,  0.221245766666667,  0.2143264,  0.2076561,  0.201234866666667,  0.1950627,  0.1891396,  0.183465566666667,  0.1780406,  0.1728647,  0.167937866666667,  0.1632601),
                       (0.3056778,  0.296416456,    0.287420264,    0.278689224,    0.270223336,    0.2620226,  0.254087016,    0.246416584,    0.239011304,    0.231871176,    0.2249962,  0.218386376,    0.212041704,    0.205962184,    0.200147816,    0.1945986,  0.189314536,    0.184295624,    0.179541864,    0.175053256,    0.1708298),
                       (0.3008828768,   0.292424567021333,  0.284187283338667,  0.276171025752, 0.268375794261333,  0.260801588866667,  0.253448409568, 0.246316256365333,  0.239405129258667,  0.232715028248, 0.226245953333333,  0.219997904514667,  0.213970881792, 0.208164885165333,  0.202579914634667,  0.1972159702,   0.192073051861333,  0.187151159618667,  0.182450293472, 0.177970453421333,  0.173711639466667),
                       (0.2960879536,   0.288432678042667,  0.280954302677333,  0.273652827504, 0.266528252522667,  0.259580577733333,  0.252809803136, 0.246215928730667,  0.239798954517333,  0.233558880496, 0.227495706666667,  0.221609433029333,  0.215900059584, 0.210367586330667,  0.205012013269333,  0.1998333404,   0.194831567722667,  0.190006695237333,  0.185358722944, 0.180887650842667,  0.176593478933333),
                       (0.2912930304,   0.284440789064, 0.277721322016, 0.271134629256, 0.264680710784, 0.2583595666,   0.252171196704, 0.246115601096, 0.240192779776, 0.234402732744, 0.22874546, 0.223220961544, 0.217829237376, 0.212570287496, 0.207444111904, 0.2024507106,   0.197590083584, 0.192862230856, 0.188267152416, 0.183804848264, 0.1794753184),
                       (0.2864981072,   0.280448900085333,  0.274488341354667,  0.268616431008, 0.262833169045333,  0.257138555466667,  0.251532590272, 0.246015273461333,  0.240586605034667,  0.235246584992, 0.229995213333333,  0.224832490058667,  0.219758415168, 0.214772988661333,  0.209876210538667,  0.2050680808,   0.200348599445333,  0.195717766474667,  0.191175581888, 0.186722045685333,  0.182357157866667),
                       (0.281703184,    0.276457011106667,  0.271255360693333,  0.26609823276,  0.260985627306667,  0.255917544333333,  0.25089398384,  0.245914945826667,  0.240980430293333,  0.23609043724,  0.231244966666667,  0.226444018573333,  0.22168759296,  0.216975689826667,  0.212308309173333,  0.207685451,    0.203107115306667,  0.198573302093333,  0.19408401136,  0.189639243106667,  0.185238997333333),
                       (0.2769082608,   0.272465122128, 0.268022380032, 0.263580034512, 0.259138085568, 0.2546965332,   0.250255377408, 0.245814618192, 0.241374255552, 0.236934289488, 0.23249472, 0.228055547088, 0.223616770752, 0.219178390992, 0.214740407808, 0.2103028212,   0.205865631168, 0.201428837712, 0.196992440832, 0.192556440528, 0.1881208368),
                       (0.279132175333333,  0.27446485122,  0.26979833968,  0.265132640713333,  0.26046775432,  0.2558036805,   0.251140419253333,  0.24647797058,  0.24181633448,  0.237155510953333,  0.2324955,  0.22783630162,  0.223177915813333,  0.21852034258,  0.21386358192,  0.209207633833333,  0.20455249832,  0.19989817538,  0.195244665013333,  0.19059196722,  0.185940082),
                       (0.281356089866667,  0.276464580312, 0.271574299328, 0.266685246914667,  0.261797423072, 0.2569108278,   0.252025461098667,  0.247141322968, 0.242258413408, 0.237376732418667,  0.23249628, 0.227617056152, 0.222739060874667,  0.217862294168, 0.212986756032, 0.208112446466667,  0.203239365472, 0.198367513048, 0.193496889194667,  0.188627493912, 0.1837593272),
                       (0.2835800044,   0.278464309404, 0.273350258976, 0.268237853116, 0.263127091824, 0.2580179751,   0.252910502944, 0.247804675356, 0.242700492336, 0.237597953884, 0.23249706, 0.227397810684, 0.222300205936, 0.217204245756, 0.212109930144, 0.2070172591,   0.201926232624, 0.196836850716, 0.191749113376, 0.186663020604, 0.1815785724),
                       (0.285803918933333,  0.280464038496, 0.275126218624, 0.269790459317333,  0.264456760576, 0.2591251224,   0.253795544789333,  0.248468027744, 0.243142571264, 0.237819175349333,  0.23249784, 0.227178565216, 0.221861350997333,  0.216546197344, 0.211233104256, 0.205922071733333,  0.200613099776, 0.195306188384, 0.190001337557333,  0.184698547296, 0.1793978176),
                       (0.288027833466667,  0.282463767588, 0.276902178272, 0.271343065518667,  0.265786429328, 0.2602322697,   0.254680586634667,  0.249131380132, 0.243584650192, 0.238040396814667,  0.23249862, 0.226959319748, 0.221422496058667,  0.215888148932, 0.210356278368, 0.204826884366667,  0.199299966928, 0.193775526052, 0.188253561738667,  0.182734073988, 0.1772170628),
                       (0.290251748,    0.28446349668,  0.27867813792,  0.27289567172,  0.26711609808,  0.261339417,    0.25556562848,  0.24979473252,  0.24402672912,  0.23826161828,  0.2324994,  0.22674007428,  0.22098364112,  0.21523010052,  0.20947945248,  0.203731697,    0.19798683408,  0.19224486372,  0.18650578592,  0.18076960068,  0.175036308)
]);

#Create X and Y data
x = np.arange(80, 121, 2)
y = np.arange(3, 12.01, 0.5)
X, Y = np.meshgrid(x, y)

#Create a color map that goes from blue to white to red
cdict = {'red':   ((0,    0, 0),  #i.e. at value 0, red component is 0. First parameter is the value, second is the color component. Ignore the third parameter, it is for discontinuities.
                   (0.5,  1, 1),  #     at value 0.5, red component is 1.
                   (1,    1, 1)), #     at value 1, red component is 1
         'green': ((0,    0, 0),
                   (0.5,  1, 1),
                   (1,    0, 0)),
         'blue':  ((0,    1, 1),
                   (0.5,  1, 1),
                   (1,    0, 0))}
#Make the color map and register it                
cmap1 = matplotlib.colors.LinearSegmentedColormap('UWR',cdict,256)
cm.register_cmap(name='UWR', cmap=cmap1)
UWR = cm.get_cmap('UWR')
#Create a variable for the colorbar
m = cm.ScalarMappable(cmap=UWR)
m.set_array(heatmap)

#Create the surface, multiply vol by 100 so axis label can be in units of %.
surf = ax.plot_surface(X, Y, volatility*100, rstride=1, cstride=1, facecolors=UWR(heatmap), linewidth=1, shade=False, edgecolors='#000000', antialiased=True)

#Axis limits
ax.set_xlim3d(80, 120)
ax.set_ylim3d(0, 12)

#Tick locations. 7 ticks for Y axis, 5 ticks for X. For Z axis maximum 6 ticks, only allow integers and only in steps of either 2, 5 or 10.
ax.yaxis.set_major_locator(LinearLocator(7))
ax.xaxis.set_major_locator(LinearLocator(5))
ax.zaxis.set_major_locator(MaxNLocator(6, interger = True, steps=[2, 5, 10]))

#Format X and Z axis tick labels as percentages and as integers
ax.xaxis.set_major_formatter(FormatStrFormatter('%d%%'))
ax.zaxis.set_major_formatter(FormatStrFormatter('%d%%'))

#Create a color bar with 11 ticks
cbar = plt.colorbar(m, ticks=LinearLocator(11), shrink=0.85)
#Make the tick label go from 0 to 1 in steps of 0.1
cbar.ax.set_yticklabels(arange(0,1.01,0.1))

ax.xaxis.set_label_text("Moneyness (Strike / Future)")
ax.yaxis.set_label_text("Term (Months)")
ax.zaxis.set_label_text("Implied Volatility")

cbar.ax.yaxis.set_label_text("Percentile of current volatility compared with historical levels")

#Set view angle
ax.view_init(20, -40) 

#Show the plot
plt.show()

推荐答案

您可以在表面图中添加线条,并添加关键字参数edgecolors

You can put the lines in the surface plot adding the keyword argument edgecolors

# Add black lines in the edges
surf = ax.plot_surface(X, Y, volatility, rstride=1, cstride=1, facecolors= UWR(heatmap), linewidth=1, shade=False, edgecolors='#000000')

有关如何在此处格式化轴刻度标签和位置的说明: http ://matplotlib.sourceforge.net/api/ticker_api.html#matplotlib.ticker.FormatStrFormatter

Directions over how to format the axis tick labels and locations here: http://matplotlib.sourceforge.net/api/ticker_api.html#matplotlib.ticker.FormatStrFormatter

已解决-从评论上移:

在plot_surface之后添加surf.set_edgecolor('k')会覆盖边缘颜色.我认为这可能与以下事实有关:facecolors是plot_surface的选项,而edgecolors是Poly3DCollection的选项.更多详细信息此处.

Adding surf.set_edgecolor('k') after plot_surface overrides the edge color. I think that may be related to the fact that facecolors is an option of plot_surface, but the edgecolors is an option of Poly3DCollection. More details here.

这篇关于指定facecolor后,mplot3d surf中的边线消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆