OSMnx: Create rectangular building footprints - osmnx

I'm using the sample "make_plot" function shown in the example page on the OSMnx repo page to generate maps of building footprints. The output is a square image, is there any way to adjust the height and width to produce a rectangular file?
I made some changes to the example to use the geometries module instead of the deprecated footprints:
def make_plot(place, point, dist, network_type='all', bldg_color='#FF0000', dpi=300,
default_width=1,
street_widths = {
"footway": 0.5,
"steps": 0.5,
"pedestrian": 0.5,
"service": 0.5,
"path": 0.5,
"track": 0.5,
"primary": 0.5,
"secondary": 0.5,
"trunk": 1,
"motorway": 2 ,
}):
gdf = ox.geometries.geometries_from_point(center_point=point, tags={'building':True}, dist=dist)
fig, ax = ox.plot_figure_ground(point=point, dist=dist, network_type=network_type,
default_width=default_width, street_widths=street_widths, save=False, show=False, close=True, bgcolor='#343434')
fig, ax = ox.plot.plot_footprints(gdf, ax=ax, color=bldg_color,
save=True, show=False, close=True, filepath="images/{}.png".format(place), dpi=dpi)
make_plot(place, point, dist)

The output is a square image, is there any way to adjust the height and width to produce a rectangular file?
You are querying a square area, so your plot of the results is correspondingly square. If you query a non-square area, you get a non-square plot:
import osmnx as ox
ox.config(use_cache=True, log_console=True)
gdf = ox.geometries_from_place('Piedmont, CA, USA', tags={'building':True})
fig, ax = ox.plot_footprints(gdf)
Note that you get a fig, ax back, and you can of course resize your figure the usual matplotlib way: fig.set_size_inches(9, 3). Note that this resizes your figure rather than stretch it (e.g., from a square to a rectangle). Also note that all OSMnx plotting functions take an optional figsize argument. See the documentation.
I made some changes to the example to use the geometries module instead of the deprecated footprints
The examples were updated a month ago when OSMnx v0.16.0 was released, to reflect the new geometries module. Any references to the deprecated footprints module were removed then as well.

Related

Margin between plot and scale in matplotlib [duplicate]

Look at the chart below. In this chart, I want to draw to circle with the centre coordinates at (45, 0). But, as you see, bottom limit of chart is zero and half of my circle is not shown. So, I need to extend this chart to the bottom a little bit. "Margins" method of axes (ax.margins()) doesn't work, since bottom line of this chart is zero and zero multiplied by any number is equal to zero.
Note: Please, do not post replies like ax.set_ylim(...) or ax.set_yticks(...). I am looking for a general solution for this like problems.
Code
import matplotlib.pyplot as plt
import numpy as np
values = np.array([0.00388632352941, 0.00375827941176, 0.00355033823529, 0.00328273529412, 0.00294677941176, 0.00272142647059, 0.00246463235294, 0.00227766176471, 0.00213151470588, 0.00202594117647, 0.00183544117647, 0.00162102941177, 0.00148372058824, 0.00128380882353, 0.00112252941176, 0.000931544117647, 0.000786573529412, 0.000658220588235, 0.000584485294118, 0.000524044117647, 0.000562485294118, 0.000716441176471, 0.000872617647059, 0.00109039705882, 0.00124138235294, 0.00136894117647, 0.00143985294118, 0.00134760294118, 0.00121794117647, 0.00112772058824, 0.00109435294118, 0.00102432352941, 0.00101069117647, 0.00102417647059, 0.00104895588235, 0.00101776470588, 0.00101494117647, 0.000885558823529, 0.00078075, 0.000752647058824, 0.000667691176471, 0.000593220588236, 0.000658647058823, 0.000742117647059, 0.000651470588235, 0.000604647058824, 0.000584573529412, 0.00049530882353, 0.000281235294118, 0.000355029411765])
fig, ax = plt.subplots()
ax.bar(np.arange(values.shape[0]), values)
plt.show()
In the question there is no circle, so my answer doesn't include any circle either.
In order to have some space around the data in the plot, you can use ax.margins(y=ymargin), where ymargin is the percentage of space to add on each side of the data. I.e. if data goes from 0 to 1 and you add ymargin = 0.1 of margin, the ylimits will be (-0.1, 1.1). (This is independent on whether or not one limit would be zero or not.)
Now, by default this does not work for a bar plot, as it would in the general case be undesireable to have the bars start somewhere in the air, as opposed to the bottom axis. This behaviour is steered using a flag called use_sticky_edges. We can set this flag to False to get back the behaviour of margins being applied to both ends of the axis. For taking effect, we need to call ax.autoscale_view afterwards.
import matplotlib.pyplot as plt
import numpy as np
values = np.array([3.89, 3.76, 3.55, 3.28, 2.95, 2.72, 2.46, 2.28, 2.13, 2.03, 1.84,
1.62, 1.48, 1.28, 1.12, 0.93, 0.79, 0.66, 0.58, 0.52, 0.56, 0.72,
0.87, 1.09, 1.24, 1.37, 1.44, 1.35, 1.22, 1.13, 1.09, 1.02, 1.01,
1.02, 1.05, 1.02, 1.01, 0.89, 0.78, 0.75, 0.67, 0.59, 0.66, 0.74,
0.65, 0.60, 0.58, 0.50, 0.28, 0.36])
fig, ax = plt.subplots()
ax.bar(np.arange(values.shape[0]), values)
ax.margins(y=0.3)
ax.use_sticky_edges = False
ax.autoscale_view(scaley=True)
plt.show()

how to remove the white space of invisiable axes in matplotlib during active plot?

I want to completely remove white space around my axes during active plot (not save_fig as others asked).
Here we cannot use bbox_inches='tight'. I can use tight_layout(pad=0).
When axis is on, it works fine, it shows all the ticks and x-y labels.
However, in some cases, I set the axis off. What I expected is to see the contents expand to fill up the empty space where the axes are. However, this does not work. It still keep the padding as there are still x-y labels and axes.
How can I remove the white space of invisible axes objects?
edit:
I am aware that I can use ax.set_yticks([]) and ax.set_xticks([]) to turn those off. But this is clumsy, I have to remember the the ticks before I clear them. And if I remove-then-add those ticks. The ticks cannot automatically update any more.
I wonder is there any more straightforward way to do this?
We can still see there is a small border spacing even after removing all ticks. If someone can come up a way to remove that too. It will be fantastic.
I would also like to keep the title if there is one. Thus the hard-coded ax.set_position([0,0,1,x]) is not very good for this usage. Surely we can still try to get the top spacing when there is a title, but if someone can provide a more direct/simple way to handle this, it will be preferred.
Example code:
def demo_tight_layout(w=10, h=6, axisoff=False, removeticks=False):
fig,ax = plt.subplots()
fig.set_facecolor((0.8, 0.8, 0.8))
rect = patches.Rectangle((-w/2, -h/2), w, h, color='#00ffff', alpha=0.5)
ax.add_patch(rect)
ax.plot([-w/2,w/2], [-h/2,h/2])
ax.plot([-w/2,w/2], [h/2,-h/2])
ax.set_ylabel("ylabel")
ax.margins(0)
_texts = []
if axisoff:
ax.set_axis_off()
_texts.append("axisoff")
if removeticks:
ax.set_xticks([])
ax.set_yticks([])
ax.set_ylabel("")
_texts.append("removeticks")
fig.text(0.5, 0.6, " ".join(_texts))
fig.tight_layout(pad=0)
plt.show()
return fig, ax, text
You may adjust the subplot parameters depending on whether you turned the axis off or not.
import matplotlib.pyplot as plt
from matplotlib import patches
def demo_tight_layout(w=10, h=6, axisoff=False):
fig,ax = plt.subplots()
fig.set_facecolor((0.8, 0.8, 0.8))
rect = patches.Rectangle((-w/2, -h/2), w, h, color='#00ffff', alpha=0.5)
ax.add_patch(rect)
ax.plot([-w/2,w/2], [-h/2,h/2])
ax.plot([-w/2,w/2], [h/2,-h/2])
ax.set_ylabel("ylabel")
ax.margins(0)
_texts = []
fig.tight_layout()
if axisoff:
ax.set_axis_off()
_texts.append("axisoff")
params = dict(bottom=0, left=0, right=1)
if ax.get_title() == "":
params.update(top=1)
fig.subplots_adjust(**params)
fig.text(0.5, 0.6, " ".join(_texts))
plt.show()
Now demo_tight_layout(axisoff=True) produces
and demo_tight_layout(axisoff=False) produces
You need to set the axes position to fill the figure. If you create your figure and plot with
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca()
ax.plot(some_x_data, some_y_data)
you need to add the following line to fill the figure with the axes:
ax.set_position([0, 0, 1, 1], which='both')
This sets the axes location relative to the figure size in the following way:
[left, bottom, width, height]
So to completely fill the figure use [0, 0, 1, 1] as shown above.
So taking your code, it should look like this (using fill_figure bool to check):
def demo_tight_layout(w=10, h=6, axisoff=False, removeticks=False, fill_figure=False):
fig,ax = plt.subplots()
fig.set_facecolor((0.8, 0.8, 0.8))
rect = patches.Rectangle((-w/2, -h/2), w, h, color='#00ffff', alpha=0.5)
ax.add_patch(rect)
ax.plot([-w/2,w/2], [-h/2,h/2])
ax.plot([-w/2,w/2], [h/2,-h/2])
ax.set_ylabel("ylabel")
ax.margins(0)
_texts = []
if axisoff:
ax.set_axis_off()
_texts.append("axisoff")
if removeticks:
ax.set_xticks([])
ax.set_yticks([])
ax.set_ylabel("")
_texts.append("removeticks")
fig.text(0.5, 0.6, " ".join(_texts))
fig.tight_layout(pad=0)
if fill_figure:
ax.set_position([0, 0, 1, 1], which='both')
plt.show()
return fig, ax, text
ax.set_position needs to be after fig.tight_layout.
If a figure title is needed, there is no direct way to do it. This unluckily can't be avoided. You need to adapt the height parameters manually so that the title fits in the figure, for example with:
ax.set_position([0, 0, 1, .9], which='both')

Changing only the line properties inside the circle when using pie in matplotlib

When I am segmenting a circle with pie from matplotlib I would like to change the properties of the lines only inside the circle:
plt.rcParams['patch.edgecolor'] = 'lightgrey'
plt.rcParams['patch.linewidth'] = 1
Affect all the lines including the line of the circle itsef.
Step 1 - changing 'inner' lines
As usual it is a good idea to look at the matplotlib API documentation, where we find that pie plot provides a lot of arguments, one of which is wedgeprops
wedgeprops: [ None | dict of key value pairs ]
Dict of arguments passed to the wedge objects making the pie. For example, you can pass in wedgeprops = { ‘linewidth’ : 3 } to set the width of the wedge border lines equal to 3. For more details, look at the doc/arguments of the wedge object.
One of the arguments to Wedge is edgecolor, another is linewidth.
So in total you have to call
plt.pie([215, 130], colors=['b', 'r'],
wedgeprops = { 'linewidth' : 1 , 'edgecolor' : 'lightgrey'} )
However, since this also changes the outline of the pie diagram we need...
Step 2 - setting circonference circle
Now, in order to get a circle around the pie, or restore the initial linestyle for the circonference of the pie, we can set a new Circle patch with the desired properties on top of the pie.
The complete solution then looks something like this
import matplotlib.pyplot as plt
import matplotlib.patches
fig, ax = plt.subplots(figsize=(3,3))
ax.axis('equal')
slices, labels = ax.pie([186, 130, 85], colors=['b', 'r','y'],
wedgeprops = { 'linewidth' : 1 , 'edgecolor' : 'lightgrey'} )
# get the center and radius of the pie wedges
center = slices[0].center
r = slices[0].r
# create a new circle with the desired properties
circle = matplotlib.patches.Circle(center, r, fill=False, edgecolor="k", linewidth=2)
# add the circle to the axes
ax.add_patch(circle)
plt.show()
For a solution that works also with any pie chart, including exploded pie charts, e.g.
import numpy as np
import matplotlib as plt
data = [1, 2, 3, 1, 4, 2]
explode = [0.05] * len(data)
labels = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ'[:len(data)])
fig, ax = plt.subplots()
pie = ax.pie(data, labels=labels, explode=explode)
use one of the following options:
Option A, add lines for each wedge of the pie
pie = ax.pie(data, labels=labels, explode=explode)
for wedge in pie[0]:
ax.plot([wedge.center[0], wedge.r*np.cos(wedge.theta1*np.pi/180)+wedge.center[0]], [wedge.center[1], wedge.r*np.sin(wedge.theta1*np.pi/180)+wedge.center[1]], color='k')
ax.plot([wedge.center[0], wedge.r*np.cos(wedge.theta2*np.pi/180)+wedge.center[0]], [wedge.center[1], wedge.r*np.sin(wedge.theta2*np.pi/180)+wedge.center[1]], color='k')
fig.show()
Option B, add edges to the pie wedges then overwrite the radial edge with another color (e.g. white)
from matplotlib import patches
pie = ax.pie(data, labels=labels, explode=explode, wedgeprops=dict(ec='k')
for wedge in pie[0]:
arc = patches.Arc(wedge.center, 2*wedge.r, 2*wedge.r, 0, theta1=wedge.theta1, theta2=wedge.theta2, ec='w', lw=1.5)
ax.add_patch(arc)
fig.show()

How to hide contour lines / data from a specific area on Basemap

I am working some meteorological data to plot contour lines on a basemap. The full working example code I have done earlier is here How to remove/omit smaller contour lines using matplotlib. All works fine and I don’t complain with the contour plot. However there is a special case that I have to hide all contour lines over a specific region (irregular lat & lon) on a Basemap.
The only possible solution I can think of is to draw a ploygon lines over a desired region and fill with the color of same as Basemap. After lot of search I found this link How to draw rectangles on a Basemap (code below)
from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
def draw_screen_poly( lats, lons, m):
x, y = m( lons, lats )
xy = zip(x,y)
poly = Polygon( xy, facecolor='red', alpha=0.4 )
plt.gca().add_patch(poly)
lats = [ -30, 30, 30, -30 ]
lons = [ -50, -50, 50, 50 ]
m = Basemap(projection='sinu',lon_0=0)
m.drawcoastlines()
m.drawmapboundary()
draw_screen_poly( lats, lons, m )
plt.show()
It seems to work partially. However, I want to draw a region which is irregular.
Any solution is appreciated.
Edit: 1
I have understood where the problem is. It seems that any colour (facecolor) filled within the polygon region does not make it hide anything below. Always it is transparent only, irrespective of alpha value used or not. To illustrate the problem, I have cropped the image which has all three regions ie. contour, basemap region and polygon region. Polygon region is filled with red colour but as you can see, the contour lines are always visible. The particular line I have used in the above code is :-
poly = Polygon(xy, facecolor='red', edgecolor='b')
Therefore the problem is not with the code above. It seem the problem with the polygon fill. But still no solution for this issue. The resulting image (cropped image) is below (See my 2nd edit below the attached image):-
Edit 2:
Taking clue from this http://matplotlib.1069221.n5.nabble.com/Clipping-a-plot-inside-a-polygon-td41950.html which has the similar requirement of mine, I am able to remove some the data. However, the removed data is only from outside of polygon region instead of within. Here is the code I have taken clue from:-
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon
data = np.arange(100).reshape(10, 10)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.contourf(data)
poly = RegularPolygon([ 0.5, 0.5], 6, 0.4, fc='none',
ec='k', transform=ax.transAxes)
for artist in ax.get_children():
artist.set_clip_path(poly)
Now my question is that what command is used for removing the data within the polygon region?
Didn't noticed there was a claim on this so I might just give the solution already proposed here. You can tinker with the zorder to hide stuff behind your polygon:
import matplotlib
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)
# Create a simple contour plot with labels using default colors. The
# inline argument to clabel will control whether the labels are draw
# over the line segments of the contour, removing the lines beneath
# the label
fig = plt.figure()
ax = fig.add_subplot(111)
CS = plt.contour(X, Y, Z,zorder=3)
plt.clabel(CS, inline=1, fontsize=10)
plt.title('Simplest default with labels')
rect1 = matplotlib.patches.Rectangle((0,0), 2, 1, color='white',zorder=5)
ax.add_patch(rect1)
plt.show()
, the result is:

hatched rectangle patches without edges in matplotlib

When trying to add a rectangle patch with a hatch pattern to a plot it seems that it is impossible to set the keyword argument edgecolor to 'none' when also specifying a hatch value.
In other words I am trying to add a hatched rectangle WITHOUT an edge but WITH a pattern filling. This doesnt seem to work. The pattern only shows up if I also allow an edge to be drawn around the rectangle patch.
Any help on how to achieve the desired behaviour?
You should use the linewidth argument, which has to be set to zero.
Example (based on your other question's answer):
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
# generate some data:
x,y = np.meshgrid(np.linspace(0,1),np.linspace(0,1))
z = np.ma.masked_array(x**2-y**2,mask=y>-x+1)
# plot your masked array
ax.contourf(z)
# plot a patch
p = patches.Rectangle((20,20), 20, 20, linewidth=0, fill=None, hatch='///')
ax.add_patch(p)
plt.show()
You'll get this image: