Nick W
Nick W

Reputation: 146

matplotlib tripcolor gives long triangles

I have a series of x, y, and z data that I want to plot with matplotlib.tripcolor, but I get some very long triangles when it looks like it should be possible to make much more balanced triangles. My data looks like this:

x:

array([ 129.,  341.,   65.,  210.,  213.,  315.,  167.,  251.,  103.,
         39.,   82.,  294.,  146.,  358.,  210.,  190.,  211.,  359.,
        147.,  338.,  126.,  275.,   63.,   10.,  158.,  306.,   74.,
        222.,  297.,   30.,  262.,  114.,  326.,  178.,   30.,   85.,
        233.,  318.,  106.,   21.,  169.,  190.,  254.,   42.,  265.,
        349.,   53.,  201.,  338.,  190.,  126.,   42.,  254.,  277.,
         10.,  222.,  179.,   31.,  327.,  306.,  158.,  243.,   95.,
        341.,   65.,  213.,    2.,  150.,  298.,   86.,  234.,   74.,
        129.,  277.,  315.,  103.,  251.,   39.,  188.,  336.,  124.,
        272.,   60.,  254.,   85.,  297.,  169.,   21.,   53.,  265.,
        286.,  138.,  349.,  201.,   10.,  222.,   74.,  167.,  178.,
        326.,  283.,  347.,  199.,  114.,  262.,  103.,  167.,  315.,
        231.,   19.,  294.,   82.,  358.,  146.,  233.,   42.])

y:
array([ 7.267,  8.034,  5.733,  8.034,  4.966,  7.267,  8.034,  5.733,
        6.5  ,  4.966,  4.966,  5.733,  6.5  ,  7.267,  8.034,  6.5  ,
        2.665,  1.898,  1.131,  5.733,  4.966,  4.199,  3.432,  6.5  ,
        5.733,  4.966,  8.034,  7.267,  8.034,  8.034,  4.966,  5.733,
        6.5  ,  7.267,  8.034,  7.267,  6.5  ,  4.199,  3.432,  5.733,
        4.966,  1.131,  2.665,  1.898,  7.267,  4.966,  6.5  ,  5.733,
        5.733,  6.5  ,  4.966,  7.267,  8.034,  6.5  ,  6.5  ,  7.267,
        1.898,  2.665,  1.131,  4.966,  5.733,  3.432,  4.199,  8.034,
        5.733,  4.966,  4.199,  3.432,  2.665,  1.898,  1.131,  8.034,
        7.267,  6.5  ,  7.267,  6.5  ,  5.733,  4.966,  4.199,  3.432,
        2.665,  1.898,  1.131,  8.034,  7.267,  8.034,  4.966,  5.733,
        6.5  ,  7.267,  3.432,  4.199,  4.966,  5.733,  1.131,  1.898,
        2.665,  8.034,  7.267,  6.5  ,  1.131,  2.665,  3.432,  5.733,
        4.966,  1.131,  2.665,  1.898,  4.199,  3.432,  5.733,  4.966,
        7.267,  6.5  ,  6.5  ,  7.267])

z:
array([  330.799,   340.835,   379.063,  1303.114,   538.557,   662.126,
        1205.669,   506.408,   284.009,   367.73 ,   279.106,   501.668,
         265.382,   753.944,  1303.114,   425.441,   456.172,   523.323,
         420.912,   444.822,   317.4  ,   539.05 ,   304.462,   505.717,
         429.093,   470.069,   677.916,   561.185,   257.518,   361.085,
        1042.523,   328.222,   677.291,   554.068,   361.085,   419.371,
         478.143,   510.053,   263.701,   340.502,   445.629,   433.228,
         546.332,   372.264,   628.328,   448.621,   438.824,   538.858,
         444.822,   425.441,   317.4  ,   645.457,   871.476,   560.89 ,
         505.717,   561.185,   359.103,   427.205,   645.609,   470.069,
         429.093,   547.388,   273.086,   340.835,   379.063,   538.557,
         473.329,   315.204,   549.825,   349.944,   466.532,   677.916,
         330.799,   560.89 ,   662.126,   284.009,   506.408,   367.73 ,
         410.995,   487.467,   249.311,   519.988,   455.012,   871.476,
         419.371,   257.518,   445.629,   340.502,   438.824,   628.328,
         684.842,   396.418,   448.621,   538.858,   562.608,   580.925,
         330.082,  1205.669,   554.068,   677.291,   586.144,   468.201,
         379.195,   328.222,  1042.523,   137.109,   296.036,   560.577,
         464.407,   428.956,   501.668,   279.106,   753.944,   265.382,
         478.143,   645.457])

I use these commands:

plt.tripcolor(x, y, z)

plt.plot(x,y, 'ko')

The plot I get is this:

Tripcolor with long triangles

The plot is of data from a cylindrical surface (360 degree x-axis), so if there's also a way to make the plot wrap around and eliminate the long triangles at the edges, that would also be great.

Upvotes: 2

Views: 315

Answers (1)

Nick W
Nick W

Reputation: 146

Figured out how to to do it with the original axes. I divide the x-axis data by 35 to get the scales of the x- and y-axes close to each other. Then I create a triangle object with the scaled axis data to get the correct neighbors for the triangulation, and finally I use that triangle object when plotting with the original x-axis data.

Using the data above:

x2 = x/35.
triangle = matplotlib.tri.Triangulation(x2,y)
plt.tripcolor(x, y, triangle.triangles, z)

Looks like this now.

Upvotes: 1

Related Questions