python各類經(jīng)緯度轉(zhuǎn)換,具體代碼如下所示:
import math import urllib import json x_pi = 3.14159265358979324 * 3000.0 / 180.0 pi = 3.1415926535897932384626 # π a = 6378245.0 # 長半軸 ee = 0.00669342162296594323 # 扁率 class Geocoding: def __init__(self, api_key): self.api_key = api_key def geocode(self, address): """ 利用高德geocoding服務(wù)解析地址獲取位置坐標 :param address:需要解析的地址 :return: """ geocoding = {'s': 'rsv3', 'key': self.api_key, 'city': '全國', 'address': address} geocoding = urllib.urlencode(geocoding) ret = urllib.urlopen("%s?%s" % ("http://restapi.amap.com/v3/geocode/geo", geocoding)) if ret.getcode() == 200: res = ret.read() json_obj = json.loads(res) if json_obj['status'] == '1' and int(json_obj['count']) >= 1: geocodes = json_obj['geocodes'][0] lng = float(geocodes.get('location').split(',')[0]) lat = float(geocodes.get('location').split(',')[1]) return [lng, lat] else: return None else: return None def gcj02_to_bd09(lng, lat): """ 火星坐標系(GCJ-02)轉(zhuǎn)百度坐標系(BD-09) 谷歌、高德――>百度 :param lng:火星坐標經(jīng)度 :param lat:火星坐標緯度 :return: """ z = math.sqrt(lng * lng + lat * lat) + 0.00002 * math.sin(lat * x_pi) theta = math.atan2(lat, lng) + 0.000003 * math.cos(lng * x_pi) bd_lng = z * math.cos(theta) + 0.0065 bd_lat = z * math.sin(theta) + 0.006 return [bd_lng, bd_lat] def bd09_to_gcj02(bd_lon, bd_lat): """ 百度坐標系(BD-09)轉(zhuǎn)火星坐標系(GCJ-02) 百度――>谷歌、高德 :param bd_lat:百度坐標緯度 :param bd_lon:百度坐標經(jīng)度 :return:轉(zhuǎn)換后的坐標列表形式 """ x = bd_lon - 0.0065 y = bd_lat - 0.006 z = math.sqrt(x * x + y * y) - 0.00002 * math.sin(y * x_pi) theta = math.atan2(y, x) - 0.000003 * math.cos(x * x_pi) gg_lng = z * math.cos(theta) gg_lat = z * math.sin(theta) return [gg_lng, gg_lat] def wgs84_to_gcj02(lng, lat): """ WGS84轉(zhuǎn)GCJ02(火星坐標系) :param lng:WGS84坐標系的經(jīng)度 :param lat:WGS84坐標系的緯度 :return: """ if out_of_china(lng, lat): # 判斷是否在國內(nèi) return lng, lat dlat = _transformlat(lng - 105.0, lat - 35.0) dlng = _transformlng(lng - 105.0, lat - 35.0) radlat = lat / 180.0 * pi magic = math.sin(radlat) magic = 1 - ee * magic * magic sqrtmagic = math.sqrt(magic) dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi) dlng = (dlng * 180.0) / (a / sqrtmagic * math.cos(radlat) * pi) mglat = lat + dlat mglng = lng + dlng return [mglng, mglat] def gcj02_to_wgs84(lng, lat): """ GCJ02(火星坐標系)轉(zhuǎn)GPS84 :param lng:火星坐標系的經(jīng)度 :param lat:火星坐標系緯度 :return: """ if out_of_china(lng, lat): return lng, lat dlat = _transformlat(lng - 105.0, lat - 35.0) dlng = _transformlng(lng - 105.0, lat - 35.0) radlat = lat / 180.0 * pi magic = math.sin(radlat) magic = 1 - ee * magic * magic sqrtmagic = math.sqrt(magic) dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi) dlng = (dlng * 180.0) / (a / sqrtmagic * math.cos(radlat) * pi) mglat = lat + dlat mglng = lng + dlng return [lng * 2 - mglng, lat * 2 - mglat] def bd09_to_wgs84(bd_lon, bd_lat): lon, lat = bd09_to_gcj02(bd_lon, bd_lat) return gcj02_to_wgs84(lon, lat) def wgs84_to_bd09(lon, lat): lon, lat = wgs84_to_gcj02(lon, lat) return gcj02_to_bd09(lon, lat) def _transformlat(lng, lat): ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + \ 0.1 * lng * lat + 0.2 * math.sqrt(math.fabs(lng)) ret += (20.0 * math.sin(6.0 * lng * pi) + 20.0 * math.sin(2.0 * lng * pi)) * 2.0 / 3.0 ret += (20.0 * math.sin(lat * pi) + 40.0 * math.sin(lat / 3.0 * pi)) * 2.0 / 3.0 ret += (160.0 * math.sin(lat / 12.0 * pi) + 320 * math.sin(lat * pi / 30.0)) * 2.0 / 3.0 return ret def _transformlng(lng, lat): ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + \ 0.1 * lng * lat + 0.1 * math.sqrt(math.fabs(lng)) ret += (20.0 * math.sin(6.0 * lng * pi) + 20.0 * math.sin(2.0 * lng * pi)) * 2.0 / 3.0 ret += (20.0 * math.sin(lng * pi) + 40.0 * math.sin(lng / 3.0 * pi)) * 2.0 / 3.0 ret += (150.0 * math.sin(lng / 12.0 * pi) + 300.0 * math.sin(lng / 30.0 * pi)) * 2.0 / 3.0 return ret def out_of_china(lng, lat): """ 判斷是否在國內(nèi),不在國內(nèi)不做偏移 :param lng: :param lat: :return: """ return not (73.66 < lng < 135.05 and lat > 3.86 and lat < 53.55) def baidu_to_google(lng, lat): result5 = bd09_to_wgs84(float(lng), float(lat)) return result5 def google_to_baidu(lng, lat): result5 = wgs84_to_bd09(float(lng), float(lat)) return result5
知識點擴展:Python設(shè)置matplotlib.plot的坐標軸刻度間隔及刻度范圍
一、用默認設(shè)置繪制折線圖
import matplotlib.pyplot as plt x_values=list(range(11)) #x軸的數(shù)字是0到10這11個整數(shù) y_values=[x**2 for x in x_values] #y軸的數(shù)字是x軸數(shù)字的平方 plt.plot(x_values,y_values,c='green') #用plot函數(shù)繪制折線圖,線條顏色設(shè)置為綠色 plt.title('Squares',fontsize=24) #設(shè)置圖表標題和標題字號 plt.tick_params(axis='both',which='major',labelsize=14) #設(shè)置刻度的字號 plt.xlabel('Numbers',fontsize=14) #設(shè)置x軸標簽及其字號 plt.ylabel('Squares',fontsize=14) #設(shè)置y軸標簽及其字號 plt.show() #顯示圖表
這樣制作出的圖表如下圖所示:
我們希望x軸的刻度是0,1,2,3,4……,y軸的刻度是0,10,20,30……,并且希望兩個坐標軸的范圍都能再大一點,所以我們需要手動設(shè)置。
二、手動設(shè)置坐標軸刻度間隔以及刻度范圍
import matplotlib.pyplot as plt from matplotlib.pyplot import MultipleLocator #從pyplot導(dǎo)入MultipleLocator類,這個類用于設(shè)置刻度間隔 x_values=list(range(11)) y_values=[x**2 for x in x_values] plt.plot(x_values,y_values,c='green') plt.title('Squares',fontsize=24) plt.tick_params(axis='both',which='major',labelsize=14) plt.xlabel('Numbers',fontsize=14) plt.ylabel('Squares',fontsize=14) x_major_locator=MultipleLocator(1) #把x軸的刻度間隔設(shè)置為1,并存在變量里 y_major_locator=MultipleLocator(10) #把y軸的刻度間隔設(shè)置為10,并存在變量里 ax=plt.gca() #ax為兩條坐標軸的實例 ax.xaxis.set_major_locator(x_major_locator) #把x軸的主刻度設(shè)置為1的倍數(shù) ax.yaxis.set_major_locator(y_major_locator) #把y軸的主刻度設(shè)置為10的倍數(shù) plt.xlim(-0.5,11) #把x軸的刻度范圍設(shè)置為-0.5到11,因為0.5不滿一個刻度間隔,所以數(shù)字不會顯示出來,但是能看到一點空白 plt.ylim(-5,110) #把y軸的刻度范圍設(shè)置為-5到110,同理,-5不會標出來,但是能看到一點空白 plt.show()
繪制的結(jié)果如圖所示:
總結(jié)
以上所述是小編給大家介紹的Python設(shè)置matplotlib.plot的坐標軸刻度間隔及刻度范圍,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
