问题
I want to know how can i plot a circle with Basemap using latitude and longitude.
import matplotlib.pyplot as plt
fig,ax = plt.subplots()
ax.axis([0,10,0,10])
circle1 = plt.Circle((5, 5), 2, color='black',fill=False)
x = ax.add_artist(circle1)
plt.show()
I want to do the same but with x,y,radius as lon lat in my basemap graph
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
m = Basemap(projection="mill", #miller est une projection connu
llcrnrlat =0,#lower left corner latitude
llcrnrlon =0,
urcrnrlat =10, #upper right lat
urcrnrlon =10,
resolution = "l") #c croud par defaut, l low , h high , f full
m.drawcoastlines() #dessiner les lignes
m.drawcountries()
m.drawstates()
m.drawcounties(color="b")
#m.fillcontinents() #colorier les payes
#m.etopo()
x,y=m(5,5)
m.plot(x,y,"o")
plt.show()
回答1:
Having the two codes ready you can just copy the one into the other. The only problem may be that the circle radius needs to be calculated in map coordinates,
r = 2
x,y=m(5,5)
x2,y2 = m(5,5+r)
circle1 = plt.Circle((x, y), y2-y, ..)
Complete example:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
fig,ax = plt.subplots()
m = Basemap(projection="mill", #miller est une projection connu
llcrnrlat =0,#lower left corner latitude
llcrnrlon =0,
urcrnrlat =10, #upper right lat
urcrnrlon =10,
resolution = "l", ax=ax) #c croud par defaut, l low , h high , f full
m.drawcoastlines() #dessiner les lignes
m.drawcountries()
m.drawstates()
m.drawcounties(color="b")
x,y=m(5,5)
x2,y2 = m(5,5+2)
circle1 = plt.Circle((x, y), y2-y, color='black',fill=False)
ax.add_patch(circle1)
plt.show()
回答2:
So I'm not sure the radius that you want your circle in your map, but this code will draw you a circle polygon on top of your map m
:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
m = Basemap(projection="mill", #miller est une projection connu
llcrnrlat =0,#lower left corner latitude
llcrnrlon =0,
urcrnrlat =10, #upper right lat
urcrnrlon =10,
resolution = "l") #c croud par defaut, l low , h high , f full
For the circle here, I just arbitrarily chose a radius of 1/3 the entire length of your y-axis...
circle = Circle(xy=m(5,5),radius=(m.ymax - m.ymin) / 3, fill=False)
plt.gca().add_patch(circle)
m.drawcoastlines() #dessiner les lignes
m.drawcountries()
m.drawstates()
plt.show()
来源:https://stackoverflow.com/questions/49134634/how-to-draw-circle-in-basemap-or-add-artiste