Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

python - Edgelines vanish in mplot3d surf when facecolors are specified

I have produced the following surface plot in matlab:

Matlab Figure

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 Figure

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.23

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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')

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


Solved - Moved up from comments:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...