问题描述
我需要在Molleweide投影上使用matplotlib-basemap标记特定数据点的帮助。我已经使用底图在Molleweide投影上生成了contourf图,但是无法向地图上的特定数据点添加标签。 数据由蛋白质原子的直角坐标及其各自的静电势组成。我已经转换为经度和纬度,并生成了轮廓线图。提供该图的代码和数据供您参考。
文件-1(APBSANLYS-Clus1-CRD_CLSP_BS1-3.txt)由要在地图投影上绘制的主要数据组成 我正在粘贴样本数据以更好地理解
文件2(CA_HYD-APBSANLYS_10620-Clus1-CRD_CLSP.txt)包含要在图中标记的点的特定数据。
非常感谢您在解决此代码方面的帮助。下面是我用于绘制数据的代码。
谢谢。
import numpy as np
import pandas as pd
from scipy.interpolate import griddata
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
lons = np.arange(-180,181,1)
lats = np.arange(-90,91,1)
map = Basemap(projection='moll',lon_0=0)
x,y = map(*np.meshgrid(lons,lats))
def read_surf(surfFile):
dots = np.loadtxt(surfFile,skiprows = 0,usecols = (0,1,2))
mhp = np.loadtxt(surfFile,usecols = (3,))
return dots,mhp
def spheric(D):
D_c = D - D.mean(axis = 0) # Transforming coordinates into the sphere origin
c = [] # D_c are radius-vectors for surface dots
lon_0 = 0
for d in D_c:
x,y,z,r = d[0],d[3],np.sqrt((d**2).sum())
lat = np.arcsin(z / r) * 180 / np.pi # Latitudes
lon = np.arctan2(y,x) * 180 / np.pi # Longitudes
c.append([lon,lat]);
return np.array(c),np.array(h)
# For labels - Adding specific data points with labels to a contour plot
dflab = pd.read_csv('CA_HYD-APBSANLYS_10620-Clus1-CRD_CLSP.txt',sep = "\t",header=None) # File 2[enter image description here][3]
dflab.columns = ["x","y","z","ep","reslab","res"]
dflab1 = dflab.filter(['x','y','z'],axis = 1)
dflab1["r"] = (dflab1['x']**2 + dflab1['y']**2 + dflab1['z']**2)**(1/2)
dflab1["r"]
dflab1["lat"] = np.arcsin(dflab1['z'] / dflab1['r']) * 180 / np.pi # Latitudes
dflab1["lon"] = np.arctan2(dflab1['y'],dflab1['x']) * 180 / np.pi # Longitudes
xl = np.array(dflab1['lon'])
yl = np.array(dflab1['lat'])
resl = np.array(dflab['res'])
xl,yl,resl
# Plotting data on the projection
def make_map(Surface,Mhp,saveMap = False):
DS,sH = spheric(Surface) # Dots on the sphere
MhpS = griddata(DS,(lons[None,:],lats[:,None]),method = 'cubic')
if saveMap == True:
f = plt.figure()
CS = plt.contourf(x,MhpS,levels = np.arange(-600,400,10),extend = 'both',zorder=4,alpha=0.4,cmap = plt.cm.bwr_r)
CB = plt.colorbar(CS,shrink = 0.8,orientation = 'horizontal')
map.drawparallels(np.arange(-60.,90.,30.),labels = [1,0])
map.drawmeridians(np.arange(0.,420.,60.),labels = [0,1])
map.drawmapboundary()
plt.savefig("test.jpeg",format="jpeg",transparent=True,dpi=300)
plt.savefig("test-2.pdf",format = 'pdf') #Here,map is saved as PDF figure. It may be shown with plt.show() command
return MhpS
# Executing the code
Dots,Mhp = read_surf('APBSANLYS-Clus1-CRD_CLSP_BS1-3.txt') # Input data file - File 1
MhpS = make_map(Dots,True)
plt.show()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)