2013-11-24 24 views
2

我正在尝试使用matplotlib的pcolormesh函数绘制一个在2d坐标中显示点的图表,并且点的颜色将由一个数字定义。Matplotlib Pcolormesh - 我应该以什么格式提供数据?

我有三个数组,其中一个具有x坐标,另一个具有y坐标,第三个数组具有应该表示颜色的数字。

xdata = [ 695422. 695423. 695424. 695425. 695426. 695426.] 
ydata = [ 0. -15.4 -15.3 -15.7 -15.5 -19. ] 
colordata = [ 0. 121. 74. 42. 8. 0.] 

现在,显然pcolormesh希望其数据为三个二维数组。 在一些例子中我已经看到了这样的事情正在做:

newxdata, newydata = np.meshgrid(xdata,ydata) 

好吧,但我怎么弄colordata成相似的格式?我试图这样:

newcolordata, zz = np.meshgrid(colordata, xdata) 

但我不完全确定它是否正确。现在,如果我尝试绘制图:

ax.pcolormesh(newxdata, newydata, newcolordata) 

我得到的东西看起来像this。 没有错误,所以我想这很好。它返回的图片显然不像我想要的那样。有人能指出我正确的方向吗?数据数组仍然是错误的格式?

这应该是所有重要的代码:

newxdata, newydata = np.meshgrid(xdata,ydata) 
    newcolordata, zz = np.meshgrid(colordata, xdata) 

    print newxdata 
    print newydata 
    print newcolordata 

    diagram = plt.figure() 







    ax = diagram.add_subplot(111) 

    xformat = DateFormatter('%d/%m/%Y') 

    ax.xaxis_date() 




    plot1 = ax.pcolormesh(newxdata, newydata, newcolordata) 

    ax.set_title("A butterfly diagram of sunspots between dates %s and %s" % (date1, date2)) 

    ax.autoscale(enable=False) 

    ax.xaxis.set_major_formatter(xformat) 
    diagram.autofmt_xdate() 



    if command == "save": 
     diagram.savefig('diagrams//'+name+'.png') 

编辑:我注意到,颜色也对应着数字。现在我只需要将这些大小相同的酒吧变成点。

+0

阅读接受的答案,我认为这个问题是一种误导:如果你想直方图,你不应该期待pcolormesh,meshgrid ......这些都与显示通过插值标量在一个普通的电网 – SAAD

回答

0

如果你想要点,使用scatterpcolormesh绘制网格。 scatter绘制标记颜色和/或按比例缩放。

例如:

import matplotlib.pyplot as plt 

xdata = [695422.,695423.,695424.,695425.,695426.,695426.] 
ydata = [0.,-15.4,-15.3,-15.7,-15.5,-19.] 
colordata = [0.,121.,74.,42.,8.,0.], 

fig, ax = plt.subplots() 
ax.scatter(xdata, ydata, c=colordata, marker='o', s=200) 
ax.xaxis_date() 
fig.autofmt_xdate() 
plt.show() 

enter image description here


编辑: 这听起来像你想斌数据,总结每个仓内的区域。

如果是这样,您可以使用hist2d来做到这一点。如果您将太阳黑子的区域指定为直方图的weights,则每个箱内的区域将进行求和。

下面是一个示例(来自此处的数据:http://solarscience.msfc.nasa.gov/greenwch.shtml,特别是,this file,格式为as described here)。其中大部分是读取数据。请注意,我指定vmin,然后使用im.cmap.set_under('none')在该值下显示任何内容为透明。

这完全有可能是我完全误解了这里的数据。这些单元可能完全不正确(我认为,给出的“原始”区域是在太阳表面积的百万分之一)。

from glob import glob 
import datetime as dt 
import pandas as pd 
import matplotlib.pyplot as plt 
import matplotlib.dates as mdates 

def main(): 
    files = sorted(glob('sunspot_data/*.txt')) 
    df = pd.concat([read_file(name) for name in files]) 

    date = mdates.date2num(df.date) 

    fig, ax = plt.subplots(figsize=(10, 4)) 
    data, xbins, ybins, im = ax.hist2d(date, df.latitude, weights=df.area/1e4, 
             bins=(1000, 50), vmin=1e-6) 
    ax.xaxis_date() 
    im.cmap.set_under('none') 
    cbar = fig.colorbar(im) 

    ax.set(xlabel='Date', ylabel='Solar Latitude', title='Butterfly Plot') 
    cbar.set_label("Percentage of the Sun's surface") 

    fig.tight_layout() 
    plt.show() 

def read_file(filename): 
    """This data happens to be in a rather annoying format...""" 
    def parse_date(year, month, day, time): 
     year, month, day = [int(item) for item in [year, month, day]] 
     time = 24 * float(time) 
     hour = int(time) 
     minute_frac = 60 * (time % 1) 
     minute = int(minute_frac) 
     second = int(60 * (minute_frac % 1)) 
     return dt.datetime(year, month, day, hour, minute, second) 

    cols = dict(year=(0, 4), month=(4, 6), day=(6, 8), time=(8, 12), 
       area=(41, 44), latitude=(63, 68), longitude=(57, 62)) 
    df = pd.read_fwf(filename, colspecs=cols.values(), header=None, 
        names=cols.keys(), date_parser=parse_date, 
        parse_dates={'date':['year', 'month', 'day', 'time']}) 
    return df 

main() 

enter image description here

+0

是的,我尝试使用分散,我发现更容易使用。然而,我有超过100年的数据,并试图绘制所有这些东西给我一个MemoryError。 – user3027287

+0

所以我想我应该使用PColorMesh或PColor来做这件事。当我绘制所有数据时,最终的图片应该是这样的:http://en.wikipedia.org/wiki/File:Sunspot-bfly.gif – user3027287

+0

@ user3027287 - 啊!好的,你只需要网格你的数据。我会在一秒钟之内举一个例子。 –

相关问题