I'm plotting some hydrodynamical simulation data run in spherical coordinates and sometimes prefer to use contourf over pcolormesh because it looks nice and smooth instead of pixelated. However, I notice that contourf always extends my data to r=0 in a polar plot, yet my data never includes r=0. I have reproduced this issue with the simple example below:
from pylab import *
fig = figure(figsize=(6, 6))
ax = fig.add_subplot(111,projection='polar')
# generate some data
Nt,Nr = 150,150
r_axis = np.linspace(0.5,1.,Nr)
t_axis = np.linspace(0.,0.5*np.pi,Nt)
r_grid, t_grid = np.meshgrid(r_axis,t_axis)
data = np.zeros((Nt,Nr))
sin_theta = np.sin(t_axis)
for i in range(Nr):
data[:,i] = sin_theta
if 1: # polar plot using contourf - plots incorrectly from r = 0
scale = np.linspace(0.,1.,100)
polar = ax.contourf(t_grid,r_grid,data,scale,cmap='Spectral')
else: # correctly plots the data
polar = ax.pcolormesh(t_grid,r_grid,data,cmap='Spectral')
show()
Is there a quick fix? Thanks
One can set the axes limits. The radial scale is set as y, therefore
ax.set_ylim(0,1)
will set the origin to 0.
Related
I'm trying to set the r-axis in a polar plot using Matplotlib. At this time, the best result I got is the following one :
I would like to modify three things :
draw a thicker line for the axis with labels,
add ticks to others r-axis,
move the legend outside of the plot.
I expect something like that :
Thanks for your help.
JD
'''
r = [0.07109986, 0.07186792, 0.07128804, 0.07093468, 0.11061314,\
0.11480423, 0.09913993, 0.13417775, 0.07485087, 0.07140557,\
0.08117919, 0.1235301 , 0.07109986]
theta = 2.0*np.pi*np.arange(len(r))/(len(r)-1)
titles = ['$a$','$\\alpha$','$b^{1}$','$b^{2}$',\
'$c^{1}_{1}$','$c^{1}_{2}$','$c^{1}_{3}$','$c^{1}_{4}$',\
'$c^{2}_{1}$','$c^{2}_{2}$','$c^{2}_{3}$','$c^{2}_{4}$']
fig = plt.figure()
ax = fig.add_subplot(111,polar='True')
ax.plot(theta,r)
ax.spines['polar'].set_visible(False)
ax.set_theta_zero_location(loc='N')
ax.set_xticks(np.arange(0,2.0*np.pi,2.0*np.pi/len(titles)))
ax.set_xticklabels(titles)
ax.yaxis.grid(False)
ax.set_rlabel_position(0)
plt.tick_params(axis='y',labelsize=12)
plt.show()
'''
Example:
using PyPlot
fig = gcf(); ax0=subplot(2,2,2)
ax1 = subplot(2,2,4)
ax0tr = ax0[:transAxes]; ax1tr = ax1[:transAxes]
figtr = fig[:transFigure]
# 2. Transform arroww start point from axis 0 to figure coordinates
ptB = figtr[:transform](ax0tr[:transform]((20., -0.5)))
# 3. Transform arroww end point from axis 1 to figure coordinates
ptE = figtr[:transform](ax1tr[:transform]((20., 1.)))
# 4. Create the patch
arroww = matplotlib[:patches][:FancyArrowPatch](
ptB, ptE, transform=figtr, # Place arroww in figure coord system
fc = "C0", alpha = 0.25, connectionstyle="arc3,rad=0.2",
arrowstyle="simple",
mutation_scale = 40.0)
# 5. Add patch to list of objects to draw onto the figure
push!(fig[:patches], arroww)
fig[:show]()
How can I add a patch object to a figure and actually see it on the figure? This doesn't work. It doesn't throw any errors but I cannot see any arrows.
(I also cannot use the function arrow because I want to create an arrow that goes from subplot to subplot).
Because this is still a top search result, here is a solution (using Julia 1.5.3)
import PyPlot; const plt = PyPlot
fig = plt.figure(figsize=(6,8), dpi=150)
ax0 = fig.add_subplot(2,2,2)
ax1 = fig.add_subplot(2,2,4)
arrow = patches.ConnectionPatch(
[0.2,1],
[0.6,0.5],
coordsA=ax0.transData,
coordsB=ax1.transData,
color="black",
arrowstyle="-|>",
mutation_scale=30,
linewidth=3,
)
fig.patches = [arrow]
It produces the following plot.
I am trying to plot points on a map using matplotlib and Basemap, where the points represent the lat/long for specific buildings. My map does indeed plot the points, but puts them in the wrong location. When I use the same data and do the same thing using Bokeh, instead of matplotlib and basemap, I get the correct plot.
Here is the CORRECT result in Bokeh:
Bokeh Version
And here is the INCORRECT result in Basemap:
Basemap Version
I have seen discussion elsewhere on StackOverflow that suggested this might be related to the fact that plot() "shifts" the longitude somehow. I've tried the suggestion from there, which was to include the line:
lons, lats = m.shiftdata(long, lat)
and then use the shifted data. That didn't have any visible impact.
My full sample code which generates both of the plots in Basemap and Bokeh is here:
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import pandas as pd
from bokeh.plotting import figure, show
from bokeh.sampledata.us_states import data as states
from bokeh.models import ColumnDataSource, Range1d
# read in data to use for plotted points
buildingdf = pd.read_csv('buildingdata.csv')
lat = buildingdf['latitude'].values
long = buildingdf['longitude'].values
# determine range to print based on min, max lat and long of the data
margin = .2 # buffer to add to the range
lat_min = min(lat) - margin
lat_max = max(lat) + margin
long_min = min(long) - margin
long_max = max(long) + margin
# create map using BASEMAP
m = Basemap(llcrnrlon=long_min,
llcrnrlat=lat_min,
urcrnrlon=long_max,
urcrnrlat=lat_max,
lat_0=(lat_max - lat_min)/2,
lon_0=(long_max-long_min)/2,
projection='merc',
resolution = 'h',
area_thresh=10000.,
)
m.drawcoastlines()
m.drawcountries()
m.drawstates()
m.drawmapboundary(fill_color='#46bcec')
m.fillcontinents(color = 'white',lake_color='#46bcec')
# convert lat and long to map projection coordinates
lons, lats = m(long, lat)
# plot points as red dots
m.scatter(lons, lats, marker = 'o', color='r')
plt.show()
# create map using Bokeh
source = ColumnDataSource(data = dict(lat = lat,lon = long))
# get state boundaries
state_lats = [states[code]["lats"] for code in states]
state_longs = [states[code]["lons"] for code in states]
p = figure(
toolbar_location="left",
plot_width=1100,
plot_height=700,
)
# limit the view to the min and max of the building data
p.y_range = Range1d(lat_min, lat_max)
p.x_range = Range1d(long_min, long_max)
p.xaxis.visible = False
p.yaxis.visible = False
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None
p.patches(state_longs, state_lats, fill_alpha=0.0,
line_color="black", line_width=2, line_alpha=0.3)
p.circle(x="lon", y="lat", source = source, size=4.5,
fill_color='red',
line_color='grey',
line_alpha=.25
)
show(p)
I don't have enough reputation points to post a link to the data or to include it here.
In the basemap plot the scatter points are hidden behind the fillcontinents. Removing the two lines
#m.drawmapboundary(fill_color='#46bcec')
#m.fillcontinents(color = 'white',lake_color='#46bcec')
would show you the points. Because this might be undesired, the best solution would be to place the scatter on top of the rest of the map by using the zorder argument.
m.scatter(lons, lats, marker = 'o', color='r', zorder=5)
Here is the complete code (and I would like to ask you to include this kind of runnable minimal example with hardcoded data next time asking a question, as it saves everyone a lot of work inventing the data oneself):
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import pandas as pd
import io
u = u"""latitude,longitude
42.357778,-71.059444
39.952222,-75.163889
25.787778,-80.224167
30.267222, -97.763889"""
# read in data to use for plotted points
buildingdf = pd.read_csv(io.StringIO(u), delimiter=",")
lat = buildingdf['latitude'].values
lon = buildingdf['longitude'].values
# determine range to print based on min, max lat and lon of the data
margin = 2 # buffer to add to the range
lat_min = min(lat) - margin
lat_max = max(lat) + margin
lon_min = min(lon) - margin
lon_max = max(lon) + margin
# create map using BASEMAP
m = Basemap(llcrnrlon=lon_min,
llcrnrlat=lat_min,
urcrnrlon=lon_max,
urcrnrlat=lat_max,
lat_0=(lat_max - lat_min)/2,
lon_0=(lon_max-lon_min)/2,
projection='merc',
resolution = 'h',
area_thresh=10000.,
)
m.drawcoastlines()
m.drawcountries()
m.drawstates()
m.drawmapboundary(fill_color='#46bcec')
m.fillcontinents(color = 'white',lake_color='#46bcec')
# convert lat and lon to map projection coordinates
lons, lats = m(lon, lat)
# plot points as red dots
m.scatter(lons, lats, marker = 'o', color='r', zorder=5)
plt.show()
I'm working on a figure to show traffic levels on a highway map. The idea is that for each
highway segment, I would plot two lines - one for direction. The thickness of each
line
would correspond to the traffic volume in that direction. I need to plot the lines
so that the left edge (relative to driving direction) of the drawn line follows
the shape of the highway segment. I would like to specify the shape in data coordinates,
but I would like to specify the thickness of the line in points.
My data is like this:
[[((5,10),(-7,2),(8,9)),(210,320)],
[((8,4),(9,1),(8,1),(11,4)),(2000,1900)],
[((12,14),(17,14)),(550,650)]]
where, for example, ((5,10),(-7,2),(8,9)) is a sequence of x,y values giving the shape of a highway segment, and (210,320) is traffic volumes in the forward and reverse direction, respectively
Looks matter: the result should be pretty.
I figured out a solution using matplotlib.transforms.Transform and shapely.geometry.LineString.parallel_offset.
Note that shapely's parallel_offset method can sometimes return a MultiLineString, which
is not handled by this code. I've changed the second shape so it does not cross over itself to avoid this problem. I think this problem would happen rarely happen in my application.
Another note: the documentation for matplotlib.transforms.Transform seems to imply that the
array returned by the transform method must be the same shape as the array passed
as an argument, but adding additional points to plot in the transform method seems
to work here.
#matplotlib version 1.1.0
#shapely version 1.2.14
#Python 2.7.3
import matplotlib.pyplot as plt
import shapely.geometry
import numpy
import matplotlib.transforms
def get_my_transform(offset_points, fig):
offset_inches = offset_points / 72.0
offset_dots = offset_inches * fig.dpi
class my_transform(matplotlib.transforms.Transform):
input_dims = 2
output_dims = 2
is_separable = False
has_inverse = False
def transform(self, values):
l = shapely.geometry.LineString(values)
l = l.parallel_offset(offset_dots,'right')
return numpy.array(l.xy).T
return my_transform()
def plot_to_right(ax, x,y,linewidth, **args):
t = ax.transData + get_my_transform(linewidth/2.0,ax.figure)
ax.plot(x,y, transform = t,
linewidth = linewidth,
solid_capstyle = 'butt',
**args)
data = [[((5,10),(-7,2),(8,9)),(210,320)],
[((8,4),(9,1),(8,1),(1,4)),(2000,1900)],
[((12,14),(17,16)),(550,650)]]
fig = plt.figure()
ax = fig.add_subplot(111)
for shape, volumes in data:
x,y = zip(*shape)
plot_to_right(ax, x,y, volumes[0]/100., c = 'blue')
plot_to_right(ax, x[-1::-1],y[-1::-1], volumes[1]/100., c = 'green')
ax.plot(x,y, c = 'grey', linewidth = 1)
plt.show()
plt.close()
I would like to draw a grid covering all the sphere on an orthographic projection.
The issue is cells outside the projection are not drawed correctly. This happened with drawgreatcircles as pointed here.
I have also tried to use Polygons as described here, but same problem.
Finally, I have coded a custom check based on Wikipedia. The idea is for each point of each segment, we check cos c (cf Wikipedia) and do not plot it if the cosinus is negative.
My question is : can we do this kind of check with basemap own functions ?
This strategy would not work for other projections.
Also, why is this kind of check not included in Basemap ?
Thanks to your example, I took the data and plotted it with cartopy. The following changes were needed to create the plot:
import cartopy.crs as ccrs
ax =plt.axes(projection=ccrs.Orthographic())
plt.pcolormesh(lons, lats,val, edgecolors='k',
linewidths=1, transform=ccrs.PlateCarree())
ax.coastlines()
ax.gridlines()
plt.show()
This is using pcolormesh so is pretty quick (though your example wasn't that slow on my machine in the first place).
Here is a solution using pcolor :
import pylab as plt
from mpl_toolkits.basemap import Basemap
import numpy as np
nb_lat2 = 20
nb_lat = 2*nb_lat2
nb_lon = 3*(2*(nb_lat+1) - 1)
lats = np.zeros((2*nb_lat, nb_lon))
lons = np.zeros((2*nb_lat, nb_lon))
val = np.zeros((2*nb_lat, nb_lon))
dlat = 90./nb_lat2
for i in range(nb_lat):
nb_lon = 2*(i+1)-1
if ((i+1) > nb_lat2):
nb_lon = 2*(nb_lat - i)-1
dlon = 120./nb_lon
lats[2*i][:] = 90 - i*dlat
lats[2*i+1][:] = 90 - (i+1)*dlat
for j in range(nb_lon):
lons[2*i][j] = j*dlon
lons[2*i+1][j] = j*dlon
for k in range(1,3):
lons[2*i][j + k*nb_lon] = j*dlon + 120.*k
lons[2*i+1][j + k*nb_lon] = j*dlon + 120.*k
lons[2*i][3*nb_lon:] = nb_lon*dlon + 240.
lons[2*i+1][3*nb_lon:] = nb_lon*dlon + 240.
lons = lons - 180
val = lats + lons
# Crash
##m = Basemap(projection='robin',lon_0=0,resolution=None)
#m = Basemap(projection='mill',lon_0=0)
m = Basemap(projection='ortho', lat_0=0,lon_0=0)
x, y = m(lons, lats)
m.pcolor(x,y,val, edgecolors='k', linewidths=1)
m.drawcoastlines()
m.drawparallels(np.arange(-90.,91.,30.))
m.drawmeridians(np.arange(-180.,181.,60.))
plt.show()
This does exactly what I want : drawing rectangles and filling them with one color.
But it is very slow (too slow). A lot of cells are unused : at the end of a latidude line, we set the width of unused cells to 0.
Another issue is some projections crash (Robin for example).