簡體   English   中英

(Python)無法看到Matplotlib圖

[英](Python) Unable to See Matplotlib Plot

我正在使用以下代碼嘗試使用matplotlib生成輪廓圖。

腳本代碼:

#!/usr/bin/python

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.colors as colors
import numpy as np
from matplotlib.mlab import griddata

a = np.loadtxt('/home/weather/site_data')

def show_map(self, a):

    # 'a' is of the format [(lats, lons, data), (lats, lons, data)... (lats, lons, data)]
    lats = [ x[0] for x in a ]
    lons = [ x[1] for x in a ]
    data = [ x[2] for x in a ]

    lat_min = min(lats)
    lat_max = max(lats)
    lon_min = min(lons)
    lon_max = max(lons)
    data_min = min(data)
    data_max = max(data)

    spatial_resolution = 0.5
    fig = plt.figure()

    x = np.array(lons)
    y = np.array(lats)
    z = np.array(data)

    xinum = (lon_max - lon_min) / spatial_resolution
    yinum = (lat_max - lat_min) / spatial_resolution
    xi = np.linspace(lon_min, lon_max + spatial_resolution, xinum)        # same as [lon_min:spatial_resolution:lon_max] in matlab
    yi = np.linspace(lat_min, lat_max + spatial_resolution, yinum)        # same as [lat_min:spatial_resolution:lat_max] in matlab
    xi, yi = np.meshgrid(xi, yi)

    zi = griddata(x, y, z, xi, yi)

    m = Basemap(
        projection = 'merc',
        llcrnrlat=lat_min, urcrnrlat=lat_max,
        llcrnrlon=lon_min, urcrnrlon=lon_max,
        rsphere=6371200., resolution='l', area_thresh=10000
    )

    m.drawcoastlines()
    m.drawstates()
    m.drawcountries()

    lat, lon = m.makegrid(zi.shape[1], zi.shape[0])
    x,y = m(lat, lon)
    m.contourf(x, y, zi)

plt.show()

數據:

[(45.43, -75.65, 75.9), (44.30, -73.63, 85.2), (45.76, -65.76, 68.2)]

我想弄清楚我輸入的數據是否錯誤或手頭是否還有其他潛在問題。

我不斷收到以下錯誤代碼:

ValueError: could not convert string to float: [(45.43,

我自己嘗試對其進行修復,並認為我通過刪除所有括號和逗號來解決了該問題,但是該腳本甚至沒有繪制圖像。

我更改了您的輸入數據文件,使其看起來像這樣,我相信這是np.loadtxt()所尋找的格式

45.43 -75.65 75.9
44.30 -73.63 85.2 
45.76 -65.76 68.2

至於為什么看不到圖像,我從函數參數中刪除了self ,然后調用

show_map(a)

plt.show()之前的plt.show() 您實際上從未在代碼塊中真正調用過函數show_map() ,這就是為什么它沒有執行的原因。 我得到一張看起來像這樣的圖片:

在此處輸入圖片說明

可能不是您要找的東西(我以前從未使用過basemap )。 希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM