Matplotlib Line partially not plotted

Matplotlib Line partially not plotted,matplotlib,python-3.5,line-plot,Matplotlib,Python 3.5,Line Plot,Problem: Plotting a closed 2D polygon works fine. When I try plotting it 1D (by replacing the second dimension with a constant), it fails in some special cases: Then the polygon (which becomes a line) is not drawn completely. What I tried: Different plotting-styles like . , o -- of which only the latter one reproduces the problem while non-line-like-styles work. Shifted/rolled the polygon-array which did not work when shifting by 1 or 2, but for 3 and 4 that solved the problem. Asked a frien

Problem: Plotting a closed 2D polygon works fine. When I try plotting it 1D (by replacing the second dimension with a constant), it fails in some special cases: Then the polygon (which becomes a line) is not drawn completely.

What I tried:

  • Different plotting-styles like
    . , o --
    of which only the latter one reproduces the problem while non-line-like-styles work.
  • Shifted/rolled the polygon-array which did not work when shifting by 1 or 2, but for 3 and 4 that solved the problem.
  • Asked a friend who is clueless ^^
  • Helped myself so far by directly plotting a line between
    x.min()
    and
    x.max()
    .
  • For simplicity I removed the z-Data. When plotting x in combination with z, it also works - as long as z is far away from being constant. But then I get a wobbly graph instead of a straight line.

Still How can it be, that whether the data is fully plotted or not depends on its order? What did I do wrong? 1-D Polygon plotted with line-style '-' is not completely drawn by pyplot

I tried reducing the data for one of the special cases, but did not get very far. Here's the working mini-example to produce the image, I'm sorry for the long dataset but could not figure out which values to remove while keeping the problem reproducable.

import numpy as np
from matplotlib import pyplot as plt

s = np.array([
       [-0.08527125,  0.08810856],
       [-0.08967261, -0.06748633],
       [-0.08772675, -0.08396971],
       [-0.08766724, -0.08440267],
       [-0.08748191, -0.08521958],
       [-0.08438602, -0.09529874],
       [-0.08385044, -0.09684308],
       [-0.08202714, -0.10180355],
       [-0.07874478, -0.1066276 ],
       [-0.07857811, -0.10686354],
       [-0.07789635, -0.10778213],
       [-0.07781094, -0.10789337],
       [-0.07710836, -0.10880646],
       [-0.07007289, -0.11655674],
       [-0.06962708, -0.11703841],
       [-0.06727917, -0.11933749],
       [-0.06584873, -0.12070607],
       [-0.06552574, -0.12100522],
       [-0.06527846, -0.12121293],
       [-0.06401669, -0.12214381],
       [-0.06349801, -0.12245992],
       [-0.06328962, -0.12258028],
       [-0.0627093 , -0.12290901],
       [-0.06225359, -0.12314782],
       [-0.06116991, -0.12368258],
       [-0.06041895, -0.12403051],
       [-0.06017477, -0.12413955],
       [-0.05992829, -0.12424804],
       [-0.04659197, -0.13006904],
       [-0.04634663, -0.13017319],
       [-0.04628731, -0.1301966 ],
       [-0.04567821, -0.13041967],
       [-0.04552972, -0.13047215],
       [-0.04521702, -0.13058213],
       [-0.00325617, -0.14513102],
       [ 0.00180445, -0.14683444],
       [ 0.00950078, -0.14923653],
       [ 0.01392647, -0.15030046],
       [ 0.01518804, -0.15045976],
       [ 0.02734024, -0.15177574],
       [ 0.02813995, -0.15177997],
       [ 0.02882764, -0.15176844],
       [ 0.02947446, -0.15171012],
       [ 0.03001744, -0.15165147],
       [ 0.0309922 , -0.15149313],
       [ 0.03121784, -0.15145567],
       [ 0.031327  , -0.15143527],
       [ 0.03142507, -0.15141573],
       [ 0.03150791, -0.15139799],
       [ 0.03222764, -0.15124224],
       [ 0.03798838, -0.14988557],
       [ 0.038991  , -0.1496481 ],
       [ 0.0391266 , -0.14961472],
       [ 0.03920601, -0.14959498],
       [ 0.03938681, -0.14954736],
       [ 0.03991586, -0.14940614],
       [ 0.05465674, -0.14538513],
       [ 0.05500815, -0.14528457],
       [ 0.05512499, -0.14524203],
       [ 0.05522656, -0.14520209],
       [ 0.05558739, -0.14504381],
       [ 0.05580602, -0.1449474 ],
       [ 0.05609256, -0.14478439],
       [ 0.05713959, -0.14412759],
       [ 0.05743526, -0.14392656],
       [ 0.05889567, -0.14277855],
       [ 0.06059875, -0.14143159],
       [ 0.06530732, -0.13766523],
       [ 0.06933674, -0.13424303],
       [ 0.0702827 , -0.13335664],
       [ 0.07036637, -0.13327235],
       [ 0.07405533, -0.12951652],
       [ 0.07810863, -0.125367  ],
       [ 0.08087981, -0.12226141],
       [ 0.08176499, -0.12123622],
       [ 0.08381519, -0.1150717 ],
       [ 0.08734418, -0.0927558 ],
       [ 0.08230393,  0.09131774],
       [ 0.08183173,  0.097691  ],
       [ 0.08169106,  0.09931977],
       [ 0.08130204,  0.10177584],
       [ 0.07914312,  0.11230088],
       [ 0.07895476,  0.11316267],
       [ 0.07514703,  0.12076318],
       [ 0.07464758,  0.12166996],
       [ 0.0733333 ,  0.12382546],
       [ 0.07297538,  0.12425848],
       [ 0.06620658,  0.13199037],
       [ 0.05294461,  0.14660768],
       [ 0.05262742,  0.14694802],
       [ 0.05097036,  0.14849098],
       [ 0.04972845,  0.14937964],
       [ 0.04815287,  0.15002114],
       [ 0.04783434,  0.15013311],
       [ 0.04757239,  0.15022502],
       [ 0.02928349,  0.15634586],
       [ 0.02842644,  0.15662035],
       [ 0.02776939,  0.15677726],
       [ 0.0270936 ,  0.15690821],
       [ 0.02666639,  0.15698384],
       [ 0.02610376,  0.15707915],
       [ 0.02601474,  0.15709266],
       [ 0.025126  ,  0.15722434],
       [ 0.02474755,  0.15727606],
       [ 0.02296123,  0.15742128],
       [ 0.02202472,  0.15744494],
       [ 0.02086636,  0.15744469],
       [ 0.01967163,  0.15742308],
       [ 0.01872141,  0.15737815],
       [ 0.01568162,  0.15718458],
       [-0.00722516,  0.15361993],
       [-0.00785781,  0.15350418],
       [-0.02865655,  0.14966376],
       [-0.02928172,  0.1495445 ],
       [-0.02970399,  0.14942282],
       [-0.03124785,  0.14896281],
       [-0.03160079,  0.14884731],
       [-0.03311488,  0.14832349],
       [-0.04643741,  0.14369722],
       [-0.04831254,  0.14301643],
       [-0.04846789,  0.14294296],
       [-0.04902343,  0.14264339],
       [-0.04980039,  0.14218488],
       [-0.05127431,  0.14122099],
       [-0.05343824,  0.13978482],
       [-0.06641953,  0.1306598 ],
       [-0.06719992,  0.13009146],
       [-0.06732267,  0.12997761],
       [-0.06842394,  0.12872382],
       [-0.06895024,  0.12808815],
       [-0.07854825,  0.11574519],
       [-0.08365013,  0.10504   ],
       [-0.0837306 ,  0.10486896],
       [-0.08384365,  0.10417084],
       [-0.08444646,  0.09768441]])

# make first and last point the same, to close the circle
s = np.concatenate([s,s[:1]]) 
y = s[:,0]
x = s[:,1]
# plot the polygon xy
plt.plot(x, y, 'b--', label='Polygon with x/y')
# plot only x values of the polygon (does not work)
plt.plot(x, np.zeros(len(x)), 'g-', linewidth=10, label='does not stretch over red dots')
# do the same, but with dots to show x values (does work)
plt.plot(x, np.zeros(len(x)), 'r.') 
# do the same, a little bit lower, but with shifted/rolled x values. 
# rolled by 1 or 2 does not help, but by 3 or 4 does.
plt.plot(np.roll(x,3), np.ones(len(x))-1.02, '-', color="lightgreen", linewidth=10, label="stretches over red dots")
# do the same again with dots to show x values
plt.plot(np.roll(x,3), np.ones(len(x))-1.02, 'r.')
plt.legend(loc='lower center').get_frame().set_alpha(1)
plt.show()

Python 3.5 x64 via Anaconda in Windows10. Matplotlib is v2.0.0


#1

How long is a swimming pool that is 0 mm wide? In theory, it has a length of course, but in practice you couldn't measure it. The same happens here, a polygon without extention in one dimension could potentially be rendered to any length. And you would see the same effect happening to both polygons depending on the figure size, zoom level, axis range.

I think you already found a solution, by plotting a line instead of a polygon. And that would also be my recommendation.


#2

I see your point but cannot understand how it applies here. Is plotting a series of lines that form a polygon different from plotting a series of lines? If I do not "close" the lines to form a polygon (s = np.concatenate([s,s[:1]]) ) but instead just leave min() != max(), it shows the same behaviour.

#3

I agree that this is strange, but it is also working fine if you sort the values, plt.plot(np.sort(x), np.zeros(len(x)).

#4

Yes, though the lines drawn sorted are (internally) very different from unsorted - anyways, they should be visually indistinguishible. I'll leave the question open for a few more days, there might be a pyplotguru coming around, telling us there is a special pruning algorithm in matplotlib viciously dissecting my graphs ;)