https://matplotlib.org/3.1.1/tutorials/introductory/pyplot.html
Why
f, ax = plt.subplots()
f, ax = plt.subplots()
f, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')
plt.subplots()
is a function that returns a tuple containing a figure and axes object(s). Thus when using fig, ax = plt.subplots()
you unpack this tuple into the variables fig
and ax
.Having
fig
is useful if you want to change figure-level attributes or save the figure as an image file later (e.g. with fig.savefig('yourfilename.png')
).
You certainly don't have to use the returned figure object but many
people do use it later so it's common to see.Also, all axes objects (the objects that have plotting methods), have a parent figure object anyway, thus:
fig, ax = plt.subplots()
is more concise than this:
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
Plot Geodataframe kleur
https://matplotlib.org/tutorials/colors/colormaps.html
kleure 'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds', 'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu', 'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn']
kleure 'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds', 'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu', 'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn']
cmap='Blues'gdf2.plot(column='myval',cmap=cmap)
Plot Legend mooi
Zet legend op true print niet heel mooi
fig, ax = plt.subplots(1, 1)
gdf2.plot(column='myval',ax=ax,legend=True)
Aanpassen op de volgende manier
from mpl_toolkits.axes_grid1 import make_axes_locatable
fig, ax = plt.subplots(1, 1,figsize = (10, 10))
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.4)
gdf2.plot(column='myval',ax=ax,legend=True,cax=cax,cmap='Blues')
Plot een marker in het midden van een shape
from shapely.geometry import Point
from shapely.geometry import shape
gdfP=gdf2.copy()
gdfP['geometry']=gdfP['geometry'].centroid
fig, ax = plt.subplots(figsize=(15, 15))
base = gdf2.plot(column='myval',ax=ax,legend=True)
gdfP.plot(ax=base,marker='o',color='red', markersize=4)
Print een cloropleth en een markers over elkaar
ax1 = gdf2.plot(figsize = (15, 15),column='myval',cmap='Blues')
texts = []
for x, y, label in zip(gdfP.geometry.x, gdfP.geometry.y, gdfP["Gebied"]):
texts.append(plt.text(x, y, label, fontsize = 8))
dit werkt ook : zonder teksts.append
ax1 = gdf2.plot(figsize = (15, 15),column='myval',cmap='Blues')
texts = []
for x, y, label in zip(gdfP.geometry.x, gdfP.geometry.y, gdfP["Gebied"]):
plt.text(x, y, label, fontsize = 8)
Geen opmerkingen:
Een reactie posten