Home > Software engineering >  How to convert list to points for plotting a polygon?
How to convert list to points for plotting a polygon?

Time:02-19

I have the following dictionary data, and I want to convert it to x and y to plot a shape from it, the data is taken from the layout and it shows the coordinates of the polygon points.

polygons =    [[(3.898181467043318, 0.2825000000000002), (3.782964280825289, 0.7174999999999996), (3.9418937592617302, 0.758984139490194), (4.2611910136100235, 0.836223504640456), (4.582080393780855, 0.9065560138858539), (4.904412512923213, 0.9699489246280755), (5.228037312533347, 1.0263727249296788), (5.552804132313004, 1.0758011472530957), (5.87856178030783, 1.1182111806892179), (6.205158603293278, 1.1535830816698915), (6.532442557375249, 1.1819003831593484), (6.860261278772615, 1.2031499023202628), (7.1884621547486605, 1.2173217466508868), (7.516892394658411, 1.2244093185904186), (7.681145747868607, 1.2249999999999992), (7.681145747868607, 0.7750000000000002), (7.521747173571027, 0.7744267771542507), (7.2030242313926, 0.7675486900996306), (6.88452387391831, 0.7537957180152307), (6.566394375813476, 0.733174263460058), (6.248783839093534, 0.7056939265466446), (5.931840124176622, 0.6713675004718033), (5.615710781048637, 0.6302109655608887), (5.300542980572812, 0.5822434818283097), (4.986483445975786, 0.5274873800577893), (4.67367838454207, 0.4659681514064702), (4.362273419548701, 0.39771443553778363), (4.052413522471785, 0.32275800728851434)], [(3.898181467043318, 0.2825000000000002), (3.782964280825289, 0.7174999999999996), (3.9418937592617302, 0.758984139490194), (4.2611910136100235, 0.836223504640456), (4.582080393780855, 0.9065560138858539), (4.904412512923213, 0.9699489246280755), (5.228037312533347, 1.0263727249296788), (5.552804132313004, 1.0758011472530957), (5.87856178030783, 1.1182111806892179), (6.205158603293278, 1.1535830816698915), (6.532442557375249, 1.1819003831593484), (6.860261278772615, 1.2031499023202628), (7.1884621547486605, 1.2173217466508868), (7.516892394658411, 1.2244093185904186), (7.681145747868607, 1.2249999999999992), (7.681145747868607, 0.7750000000000002), (7.521747173571027, 0.7744267771542507), (7.2030242313926, 0.7675486900996306), (6.88452387391831, 0.7537957180152307), (6.566394375813476, 0.733174263460058), (6.248783839093534, 0.7056939265466446), (5.931840124176622, 0.6713675004718033), (5.615710781048637, 0.6302109655608887), (5.300542980572812, 0.5822434818283097), (4.986483445975786, 0.5274873800577893), (4.67367838454207, 0.4659681514064702), (4.362273419548701, 0.39771443553778363), (4.052413522471785, 0.32275800728851434)], [(3.898181467043318, 0.2825000000000002), (3.782964280825289, 0.7174999999999996), (3.9418937592617302, 0.758984139490194), (4.2611910136100235, 0.836223504640456), (4.582080393780855, 0.9065560138858539), (4.904412512923213, 0.9699489246280755), (5.228037312533347, 1.0263727249296788), (5.552804132313004, 1.0758011472530957), (5.87856178030783, 1.1182111806892179), (6.205158603293278, 1.1535830816698915), (6.532442557375249, 1.1819003831593484), (6.860261278772615, 1.2031499023202628), (7.1884621547486605, 1.2173217466508868), (7.516892394658411, 1.2244093185904186), (7.681145747868607, 1.2249999999999992), (7.681145747868607, 0.7750000000000002), (7.521747173571027, 0.7744267771542507), (7.2030242313926, 0.7675486900996306), (6.88452387391831, 0.7537957180152307), (6.566394375813476, 0.733174263460058), (6.248783839093534, 0.7056939265466446), (5.931840124176622, 0.6713675004718033), (5.615710781048637, 0.6302109655608887), (5.300542980572812, 0.5822434818283097), (4.986483445975786, 0.5274873800577893), (4.67367838454207, 0.4659681514064702), (4.362273419548701, 0.39771443553778363), (4.052413522471785, 0.32275800728851434)], [(3.898181467043318, 0.2825000000000002), (3.782964280825289, 0.7174999999999996), (3.9418937592617302, 0.758984139490194), (4.2611910136100235, 0.836223504640456), (4.582080393780855, 0.9065560138858539), (4.904412512923213, 0.9699489246280755), (5.228037312533347, 1.0263727249296788), (5.552804132313004, 1.0758011472530957), (5.87856178030783, 1.1182111806892179), (6.205158603293278, 1.1535830816698915), (6.532442557375249, 1.1819003831593484), (6.860261278772615, 1.2031499023202628), (7.1884621547486605, 1.2173217466508868), (7.516892394658411, 1.2244093185904186), (7.681145747868607, 1.2249999999999992), (7.681145747868607, 0.7750000000000002), (7.521747173571027, 0.7744267771542507), (7.2030242313926, 0.7675486900996306), (6.88452387391831, 0.7537957180152307), (6.566394375813476, 0.733174263460058), (6.248783839093534, 0.7056939265466446), (5.931840124176622, 0.6713675004718033), (5.615710781048637, 0.6302109655608887), (5.300542980572812, 0.5822434818283097), (4.986483445975786, 0.5274873800577893), (4.67367838454207, 0.4659681514064702), (4.362273419548701, 0.39771443553778363), (4.052413522471785, 0.32275800728851434)], [(3.898181467043318, 0.2825000000000002), (3.782964280825289, 0.7174999999999996), (3.9418937592617302, 0.758984139490194), (4.2611910136100235, 0.836223504640456), (4.582080393780855, 0.9065560138858539), (4.904412512923213, 0.9699489246280755), (5.228037312533347, 1.0263727249296788), (5.552804132313004, 1.0758011472530957), (5.87856178030783, 1.1182111806892179), (6.205158603293278, 1.1535830816698915), (6.532442557375249, 1.1819003831593484), (6.860261278772615, 1.2031499023202628), (7.1884621547486605, 1.2173217466508868), (7.516892394658411, 1.2244093185904186), (7.681145747868607, 1.2249999999999992), (7.681145747868607, 0.7750000000000002), (7.521747173571027, 0.7744267771542507), (7.2030242313926, 0.7675486900996306), (6.88452387391831, 0.7537957180152307), (6.566394375813476, 0.733174263460058), (6.248783839093534, 0.7056939265466446), (5.931840124176622, 0.6713675004718033), (5.615710781048637, 0.6302109655608887), (5.300542980572812, 0.5822434818283097), (4.986483445975786, 0.5274873800577893), (4.67367838454207, 0.4659681514064702), (4.362273419548701, 0.39771443553778363), (4.052413522471785, 0.32275800728851434)], [(3.898181467043318, 0.2825000000000002), (3.782964280825289, 0.7174999999999996), (3.9418937592617302, 0.758984139490194), (4.2611910136100235, 0.836223504640456), (4.582080393780855, 0.9065560138858539), (4.904412512923213, 0.9699489246280755), (5.228037312533347, 1.0263727249296788), (5.552804132313004, 1.0758011472530957), (5.87856178030783, 1.1182111806892179), (6.205158603293278, 1.1535830816698915), (6.532442557375249, 1.1819003831593484), (6.860261278772615, 1.2031499023202628), (7.1884621547486605, 1.2173217466508868), (7.516892394658411, 1.2244093185904186), (7.681145747868607, 1.2249999999999992), (7.681145747868607, 0.7750000000000002), (7.521747173571027, 0.7744267771542507), (7.2030242313926, 0.7675486900996306), (6.88452387391831, 0.7537957180152307), (6.566394375813476, 0.733174263460058), (6.248783839093534, 0.7056939265466446), (5.931840124176622, 0.6713675004718033), (5.615710781048637, 0.6302109655608887), (5.300542980572812, 0.5822434818283097), (4.986483445975786, 0.5274873800577893), (4.67367838454207, 0.4659681514064702), (4.362273419548701, 0.39771443553778363), (4.052413522471785, 0.32275800728851434)], [(3.898181467043318, 0.2825000000000002), (3.782964280825289, 0.7174999999999996), (3.9418937592617302, 0.758984139490194), (4.2611910136100235, 0.836223504640456), (4.582080393780855, 0.9065560138858539), (4.904412512923213, 0.9699489246280755), (5.228037312533347, 1.0263727249296788), (5.552804132313004, 1.0758011472530957), (5.87856178030783, 1.1182111806892179), (6.205158603293278, 1.1535830816698915), (6.532442557375249, 1.1819003831593484), (6.860261278772615, 1.2031499023202628), (7.1884621547486605, 1.2173217466508868), (7.516892394658411, 1.2244093185904186), (7.681145747868607, 1.2249999999999992), (7.681145747868607, 0.7750000000000002), (7.521747173571027, 0.7744267771542507), (7.2030242313926, 0.7675486900996306), (6.88452387391831, 0.7537957180152307), (6.566394375813476, 0.733174263460058), (6.248783839093534, 0.7056939265466446), (5.931840124176622, 0.6713675004718033), (5.615710781048637, 0.6302109655608887), (5.300542980572812, 0.5822434818283097), (4.986483445975786, 0.5274873800577893), (4.67367838454207, 0.4659681514064702), (4.362273419548701, 0.39771443553778363), (4.052413522471785, 0.32275800728851434)], [(3.898181467043318, 0.2825000000000002), (3.782964280825289, 0.7174999999999996), (3.9418937592617302, 0.758984139490194), (4.2611910136100235, 0.836223504640456), (4.582080393780855, 0.9065560138858539), (4.904412512923213, 0.9699489246280755), (5.228037312533347, 1.0263727249296788), (5.552804132313004, 1.0758011472530957), (5.87856178030783, 1.1182111806892179), (6.205158603293278, 1.1535830816698915), (6.532442557375249, 1.1819003831593484), (6.860261278772615, 1.2031499023202628), (7.1884621547486605, 1.2173217466508868), (7.516892394658411, 1.2244093185904186), (7.681145747868607, 1.2249999999999992), (7.681145747868607, 0.7750000000000002), (7.521747173571027, 0.7744267771542507), (7.2030242313926, 0.7675486900996306), (6.88452387391831, 0.7537957180152307), (6.566394375813476, 0.733174263460058), (6.248783839093534, 0.7056939265466446), (5.931840124176622, 0.6713675004718033), (5.615710781048637, 0.6302109655608887), (5.300542980572812, 0.5822434818283097), (4.986483445975786, 0.5274873800577893), (4.67367838454207, 0.4659681514064702), (4.362273419548701, 0.39771443553778363), (4.052413522471785, 0.32275800728851434)], [(0.0, -0.22499999999999964), (0.0, 0.22499999999999964), (0.15939857429757992, 0.22557322284574965), (0.4781215164760078, 0.23245130990036778), (0.7966218739502973, 0.2462042819847703), (1.1147513720551312, 0.2668257365399409), (1.4323619087750734, 0.294306073453356), (1.7493056236919855, 0.3286324995281955), (2.0654349668199696, 0.36978903443911193), (2.380602767295795, 0.41775651817168935), (2.694662301892821, 0.4725126199422114), (3.0074673633265383, 0.5340318485935285), (3.318872328319906, 0.6022855644622176), (3.628732225396822, 0.677241992711485), (3.782964280825289, 0.7174999999999994), (3.8981814670433184, 0.28250000000000064), (3.739251988606877, 0.2410158605098065), (3.4199547342585834, 0.16377649535954575), (3.0990653540877524, 0.09344398611414384), (2.776733234945394, 0.030051075371924085), (2.4531084353352606, -0.026372724929679237), (2.128341615555603, -0.07580114725309706), (1.8025839675607769, -0.11821118068921699), (1.4759871445753292, -0.15358308166989154), (1.1487031904933585, -0.18190038315934842), (0.8208844690959916, -0.20314990232026098), (0.4926835931199471, -0.21732174665088877), (0.16425335321019655, -0.22440931859041946)], [(0.0, -0.22499999999999964), (0.0, 0.22499999999999964), (0.15939857429757992, 0.22557322284574965), (0.4781215164760078, 0.23245130990036778), (0.7966218739502973, 0.2462042819847703), (1.1147513720551312, 0.2668257365399409), (1.4323619087750734, 0.294306073453356), (1.7493056236919855, 0.3286324995281955), (2.0654349668199696, 0.36978903443911193), (2.380602767295795, 0.41775651817168935), (2.694662301892821, 0.4725126199422114), (3.0074673633265383, 0.5340318485935285), (3.318872328319906, 0.6022855644622176), (3.628732225396822, 0.677241992711485), (3.782964280825289, 0.7174999999999994), (3.8981814670433184, 0.28250000000000064), (3.739251988606877, 0.2410158605098065), (3.4199547342585834, 0.16377649535954575), (3.0990653540877524, 0.09344398611414384), (2.776733234945394, 0.030051075371924085), (2.4531084353352606, -0.026372724929679237), (2.128341615555603, -0.07580114725309706), (1.8025839675607769, -0.11821118068921699), (1.4759871445753292, -0.15358308166989154), (1.1487031904933585, -0.18190038315934842), (0.8208844690959916, -0.20314990232026098), (0.4926835931199471, -0.21732174665088877), (0.16425335321019655, -0.22440931859041946)], [(0.0, -0.22499999999999964), (0.0, 0.22499999999999964), (0.15939857429757992, 0.22557322284574965), (0.4781215164760078, 0.23245130990036778), (0.7966218739502973, 0.2462042819847703), (1.1147513720551312, 0.2668257365399409), (1.4323619087750734, 0.294306073453356), (1.7493056236919855, 0.3286324995281955), (2.0654349668199696, 0.36978903443911193), (2.380602767295795, 0.41775651817168935), (2.694662301892821, 0.4725126199422114), (3.0074673633265383, 0.5340318485935285), (3.318872328319906, 0.6022855644622176), (3.628732225396822, 0.677241992711485), (3.782964280825289, 0.7174999999999994), (3.8981814670433184, 0.28250000000000064), (3.739251988606877, 0.2410158605098065), (3.4199547342585834, 0.16377649535954575), (3.0990653540877524, 0.09344398611414384), (2.776733234945394, 0.030051075371924085), (2.4531084353352606, -0.026372724929679237), (2.128341615555603, -0.07580114725309706), (1.8025839675607769, -0.11821118068921699), (1.4759871445753292, -0.15358308166989154), (1.1487031904933585, -0.18190038315934842), (0.8208844690959916, -0.20314990232026098), (0.4926835931199471, -0.21732174665088877), (0.16425335321019655, -0.22440931859041946)], [(0.0, -0.22499999999999964), (0.0, 0.22499999999999964), (0.15939857429757992, 0.22557322284574965), (0.4781215164760078, 0.23245130990036778), (0.7966218739502973, 0.2462042819847703), (1.1147513720551312, 0.2668257365399409), (1.4323619087750734, 0.294306073453356), (1.7493056236919855, 0.3286324995281955), (2.0654349668199696, 0.36978903443911193), (2.380602767295795, 0.41775651817168935), (2.694662301892821, 0.4725126199422114), (3.0074673633265383, 0.5340318485935285), (3.318872328319906, 0.6022855644622176), (3.628732225396822, 0.677241992711485), (3.782964280825289, 0.7174999999999994), (3.8981814670433184, 0.28250000000000064), (3.739251988606877, 0.2410158605098065), (3.4199547342585834, 0.16377649535954575), (3.0990653540877524, 0.09344398611414384), (2.776733234945394, 0.030051075371924085), (2.4531084353352606, -0.026372724929679237), (2.128341615555603, -0.07580114725309706), (1.8025839675607769, -0.11821118068921699), (1.4759871445753292, -0.15358308166989154), (1.1487031904933585, -0.18190038315934842), (0.8208844690959916, -0.20314990232026098), (0.4926835931199471, -0.21732174665088877), (0.16425335321019655, -0.22440931859041946)], [(0.0, -0.22499999999999964), (0.0, 0.22499999999999964), (0.15939857429757992, 0.22557322284574965), (0.4781215164760078, 0.23245130990036778), (0.7966218739502973, 0.2462042819847703), (1.1147513720551312, 0.2668257365399409), (1.4323619087750734, 0.294306073453356), (1.7493056236919855, 0.3286324995281955), (2.0654349668199696, 0.36978903443911193), (2.380602767295795, 0.41775651817168935), (2.694662301892821, 0.4725126199422114), (3.0074673633265383, 0.5340318485935285), (3.318872328319906, 0.6022855644622176), (3.628732225396822, 0.677241992711485), (3.782964280825289, 0.7174999999999994), (3.8981814670433184, 0.28250000000000064), (3.739251988606877, 0.2410158605098065), (3.4199547342585834, 0.16377649535954575), (3.0990653540877524, 0.09344398611414384), (2.776733234945394, 0.030051075371924085), (2.4531084353352606, -0.026372724929679237), (2.128341615555603, -0.07580114725309706), (1.8025839675607769, -0.11821118068921699), (1.4759871445753292, -0.15358308166989154), (1.1487031904933585, -0.18190038315934842), (0.8208844690959916, -0.20314990232026098), (0.4926835931199471, -0.21732174665088877), (0.16425335321019655, -0.22440931859041946)], [(0.0, -0.22499999999999964), (0.0, 0.22499999999999964), (0.15939857429757992, 0.22557322284574965), (0.4781215164760078, 0.23245130990036778), (0.7966218739502973, 0.2462042819847703), (1.1147513720551312, 0.2668257365399409), (1.4323619087750734, 0.294306073453356), (1.7493056236919855, 0.3286324995281955), (2.0654349668199696, 0.36978903443911193), (2.380602767295795, 0.41775651817168935), (2.694662301892821, 0.4725126199422114), (3.0074673633265383, 0.5340318485935285), (3.318872328319906, 0.6022855644622176), (3.628732225396822, 0.677241992711485), (3.782964280825289, 0.7174999999999994), (3.8981814670433184, 0.28250000000000064), (3.739251988606877, 0.2410158605098065), (3.4199547342585834, 0.16377649535954575), (3.0990653540877524, 0.09344398611414384), (2.776733234945394, 0.030051075371924085), (2.4531084353352606, -0.026372724929679237), (2.128341615555603, -0.07580114725309706), (1.8025839675607769, -0.11821118068921699), (1.4759871445753292, -0.15358308166989154), (1.1487031904933585, -0.18190038315934842), (0.8208844690959916, -0.20314990232026098), (0.4926835931199471, -0.21732174665088877), (0.16425335321019655, -0.22440931859041946)], [(0.0, -0.22499999999999964), (0.0, 0.22499999999999964), (0.15939857429757992, 0.22557322284574965), (0.4781215164760078, 0.23245130990036778), (0.7966218739502973, 0.2462042819847703), (1.1147513720551312, 0.2668257365399409), (1.4323619087750734, 0.294306073453356), (1.7493056236919855, 0.3286324995281955), (2.0654349668199696, 0.36978903443911193), (2.380602767295795, 0.41775651817168935), (2.694662301892821, 0.4725126199422114), (3.0074673633265383, 0.5340318485935285), (3.318872328319906, 0.6022855644622176), (3.628732225396822, 0.677241992711485), (3.782964280825289, 0.7174999999999994), (3.8981814670433184, 0.28250000000000064), (3.739251988606877, 0.2410158605098065), (3.4199547342585834, 0.16377649535954575), (3.0990653540877524, 0.09344398611414384), (2.776733234945394, 0.030051075371924085), (2.4531084353352606, -0.026372724929679237), (2.128341615555603, -0.07580114725309706), (1.8025839675607769, -0.11821118068921699), (1.4759871445753292, -0.15358308166989154), (1.1487031904933585, -0.18190038315934842), (0.8208844690959916, -0.20314990232026098), (0.4926835931199471, -0.21732174665088877), (0.16425335321019655, -0.22440931859041946)], [(0.0, -0.22499999999999964), (0.0, 0.22499999999999964), (0.15939857429757992, 0.22557322284574965), (0.4781215164760078, 0.23245130990036778), (0.7966218739502973, 0.2462042819847703), (1.1147513720551312, 0.2668257365399409), (1.4323619087750734, 0.294306073453356), (1.7493056236919855, 0.3286324995281955), (2.0654349668199696, 0.36978903443911193), (2.380602767295795, 0.41775651817168935), (2.694662301892821, 0.4725126199422114), (3.0074673633265383, 0.5340318485935285), (3.318872328319906, 0.6022855644622176), (3.628732225396822, 0.677241992711485), (3.782964280825289, 0.7174999999999994), (3.8981814670433184, 0.28250000000000064), (3.739251988606877, 0.2410158605098065), (3.4199547342585834, 0.16377649535954575), (3.0990653540877524, 0.09344398611414384), (2.776733234945394, 0.030051075371924085), (2.4531084353352606, -0.026372724929679237), (2.128341615555603, -0.07580114725309706), (1.8025839675607769, -0.11821118068921699), (1.4759871445753292, -0.15358308166989154), (1.1487031904933585, -0.18190038315934842), (0.8208844690959916, -0.20314990232026098), (0.4926835931199471, -0.21732174665088877), (0.16425335321019655, -0.22440931859041946)]]

coord = list(polygons)[0]
coord.append(coord[0]) #repeat the first point to create a 'closed loop'
xs, ys = zip(*coord) #create lists of x and y values

plt.figure()
plt.fill(xs,ys,'k') 
plt.ylim(-5, 5)
plt.xlim(0, 50)

but when I plot it I get only the half of the polygons. How can I get the other half?enter image description here The real shape is in the following image:enter image description here

CodePudding user response:

You have of list of lists in polygon. To create a single list from that you can use extend.

coord = []
for item in polygons:
    coord.extend(item)
coord.append(coord[0]) #repeat the first point to create a 'closed loop'
xs, ys = zip(*coord) #create lists of x and y values

plt.figure()
plt.fill(xs,ys,'k') 
plt.ylim(-5, 5)
plt.xlim(0, 10)
plt.show()
  • Related