Matplotlib Pcolormesh - 我应该以什么格式提供数据?

时间:2013-11-24 14:11:39

标签: python arrays matplotlib plot

我正在尝试使用matplotlib的pcolormesh函数来绘制一个在2d坐标中显示点的图表,并且点的颜色将由数字定义。

我有三个数组,其中一个有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希望将其数据作为三个2d数组。 在某些例子中,我看到过这样的事情:

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')

编辑:我注意到颜色与数字相对应。现在我只需要将那些大小相同的条形成点。

1 个答案:

答案 0 :(得分: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


编辑: 听起来你想要分析你的数据并总结每个bin中的区域。

如果是这样,您只需使用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