两个程序都能单独运行,但是糅合在一起就出问题了,以下是代码:
#-*-coding:utf-8-*-
from matplotlib.patches import Circle
import matplotlib.pyplot as plt
import xlrd
from matplotlib.animation import FuncAnimation
fig=plt.figure()#一个窗口
ax=fig.add_subplot(111)#一个表,一行一列第一块
#============================================# read data
lats = []
lons = []
data=xlrd.open_workbook("C:\untitled\sd.xlsx")
table=data.sheets()[0]
ncols=table.ncols
nrows=table.nrows
for i in range(nrows):
cir1=Circle(xy=(float(table.col(1)[i].value),float(table.col(0)[i].value)),radius=2,alpha=0.5)#线粗
lons.append(cir1)
ax.add_patch(lons[i])#填充多边形内部
def update(framedata):
global lons
for i in range(nrows):
lons[i].radius+=1
if lons[i].radius==15:
lons[i].radius=2
return lons
plt.axis('equal')
animation = FuncAnimation(fig, update, interval=50)
plt.show()
#-*-coding:utf-8-*-
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import cartopy.crs as ccrs
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
from matplotlib.animation import FuncAnimation
from matplotlib.patches import Circle
import xlrd
ax = plt.axes(projection=ccrs.Mercator())
ax.coastlines()
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
linewidth=2, color='gray', alpha=15, linestyle='--')
gl.xlabels_top = False
gl.ylabels_left = False
gl.xlines = False
gl.xlocator = mticker.FixedLocator([-180, -45, 0, 45, 180])
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
gl.xlabel_style = {'size': 15, 'color': 'gray'}
gl.xlabel_style = {'color': 'red', 'weight': 'bold'}
plt.show()
3个回答
︿
0
赞
如果我想设置动态效果的时间该怎么写呢?就是让这样的效果显示多少秒或者多少次之后把这个效果移除
暂无评论!
提交评论
︿
0
赞
噢,抱歉,我没表达清楚,我的意思是说把画的圆跟圆的动态效果都在多久时间之后移除
暂无评论!
提交评论
︿
0
赞
- 把 add_patch 改成 add_artist
- 画圆的时候你把经纬度写反了,应该是经度在前,纬度在后,最后的 transform 是告诉程序输入的是经纬度,不是普通坐标:cir1=Circle(xy=(float(table.col(1)[i].value),float(table.col(0)[i].value)),radius=2,alpha=0.5) 改为 cir1=Circle(xy=(float(table.col(0)[i].value),float(table.col(1)[i].value)),radius=2,alpha=0.5,transform=ccrs.PlateCarree())
- 感觉用 PlateCarree 投影比较好看点,个人意见,仅供参考哈
完整程序:
#-*-coding:utf-8-*-
from matplotlib.patches import Circle
import matplotlib.pyplot as plt
import xlrd
from matplotlib.animation import FuncAnimation
import matplotlib.ticker as mticker
import cartopy.crs as ccrs
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
fig=plt.figure()
ax = plt.axes(projection=ccrs.Mercator())
ax.coastlines()
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
linewidth=2, color='gray', alpha=15, linestyle='--')
gl.xlabels_top = False
gl.ylabels_left = False
gl.xlines = False
gl.xlocator = mticker.FixedLocator([-180, -45, 0, 45, 180])
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
gl.xlabel_style = {'size': 15, 'color': 'gray'}
gl.xlabel_style = {'color': 'red', 'weight': 'bold'}
lats = []
lons = []
data=xlrd.open_workbook("D:\ChromeDownload\sd.xlsx")
table=data.sheets()[0]
ncols=table.ncols
nrows=table.nrows
for i in range(nrows):
cir1=Circle(xy=(float(table.col(0)[i].value),float(table.col(1)[i].value)),radius=2,alpha=0.5,transform=ccrs.PlateCarree())
lons.append(cir1)
ax.add_artist(lons[i])#填充多边形内部
def update(framedata):
global lons
for i in range(nrows):
lons[i].radius+=1
if lons[i].radius==15:
lons[i].radius=2
return lons
animation = FuncAnimation(fig, update, interval=50)
plt.show()
使用 Mercator 效果:
使用 PlateCarree 效果:
暂无评论!
提交评论