Cara menggunakan plot polygons python

I have a data frame with latitude, longitude and annual consumption data. I am trying to plot the annual consumption data using GeoPandas with a quantile scheme by making a polygon column. I followed the following tutorial. enter link description here

I used the latitude and longitude data to make polygon shapes using the following code.

full_dataframe["geometry"] = Polygon(list(zip(full_dataframe["long"], full_dataframe["lat"])))

merged2 = GeoDataFrame(full_dataframe)

merged2.plot(column='annual_consume', scheme='quantiles', k=4, edgecolor='k', 
             cmap='OrRd', legend=True,
             legend_kwds={'loc': 'center left', 'bbox_to_anchor':(1,0.5)})

My data frame looks like below

Cara menggunakan plot polygons python

The issue is, when I plot it, it looks like a big mess of interconnecting points. The boundaries are not seperated.

Cara menggunakan plot polygons python

I want a similar boundary separation like the tutorial based on longitude and latitude data.

Cara menggunakan plot polygons python


To plot shapely polygons and objects using matplotlib, the steps are as follows −

  • Create a polygon object using (x, y) data points.

  • Get x and y, the exterior data, and the array using polygon.exterior.xy.

  • Plot x and y data points using plot() method with red color.

Example

from shapely.geometry import Polygon
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
polygon1 = Polygon([(0, 5),
   (1, 1),
   (3, 0),
   (4, 6),
])
x, y = polygon1.exterior.xy
plt.plot(x, y, c="red")
plt.show()

Output

Cara menggunakan plot polygons python

Cara menggunakan plot polygons python

Updated on 06-May-2021 12:51:12

  • Related Questions & Answers
  • How do I plot hatched bars using Pandas and Matplotlib?
  • How do I plot only a table in Matplotlib?
  • How do I plot two countplot graphs side by side in Seaborn using Matplotlib?
  • How do I plot a step function with Matplotlib in Python?
  • How do I let my Matplotlib plot go beyond the axes?
  • How do I plot multiple X or Y axes in Matplotlib?
  • How do I change the axis tick font in a Matplotlib plot when rendering using LaTeX?
  • How can I plot hysteresis threshold in Matplotlib?
  • How do I show logarithmically spaced grid lines at all ticks on a log-log plot using Matplotlib?
  • How do I redraw an image using Python's Matplotlib?
  • How can I plot a confusion matrix in matplotlib?
  • How do I write a Latex formula in the legend of a plot using Matplotlib inside a .py file?
  • How to plot collections.Counter histogram using Matplotlib?
  • How do I make the width of the title box span the entire plot in Matplotlib?
  • How do I plot a spectrogram the same way that pylab's specgram() does? (Matplotlib)

Suscribe for monthly updates:

Plotting shapely polygons is something we will often do, either for its own sake when we need to create a map, of for visual debugging purposes when we are crafting an algorithm.

To illustrate this with a non-trivial example, let’s first create a polygon (which will have a hole in it) by computing the difference between two polygons, and plot the resulting one.

Using Geopandas GeoSeries.plot()

The simplest and more general way to plot a shapely polygon is to resort to Geopandas. More precisely, we will create a GeoSeries that holds our polygon.

There are many things we can do with a GeoSeries object (see the official docs). In particular, the GeoSeries class has a plot() function which relies on matplotlib.

from shapely.geometry import Polygon
import geopandas as gpd
import matplotlib.pyplot as plt

poly1 = Polygon( [(0, 0), (1,0), (1,1), (0,1) ] )
poly2 = Polygon( [(0.25, 0.25), (0.5,0.25), (0.5,0.5), (0.25,0.5) ] )
polydiff = poly1.difference(poly2)

myPoly = gpd.GeoSeries([polydiff])
myPoly.plot()
plt.show()

Cara menggunakan plot polygons python

You can also plot the polygon’s boundary with

myPoly.boundary.plot()
plt.show()

Cara menggunakan plot polygons python

Manually extracting exterior and interior boundaries

An alternative method, in case we don’t want to rely on Geopandas, is to do this manually. We will need to extract both exterior and interior boudnaries (if the polygon has holes in it).

Each polygon object has an exterior ring, and zero or multiple interior rings. We can extract the coordinates of these rings and plot them as follows:

xe,ye = polydiff.exterior.xy
plt.plot(xe,ye)

for LinearRing in polydiff.interiors:
    xi,yi = LinearRing.xy
    plt.plot(xi,yi)

plt.show()

Cara menggunakan plot polygons python