每天并排绘制色彩图图表

时间:2016-04-23 08:41:41

标签: python matlab matplotlib

我有以下2列数据:

Time            PRESSURE 
2/24/2016 13:00 1011.937618
2/24/2016 14:00 1011.721583
2/24/2016 15:00 1011.348064
2/24/2016 16:00 1011.30785
2/24/2016 17:00 1011.3198
2/24/2016 18:00 1011.403372
2/24/2016 19:00 1011.485108
2/24/2016 20:00 1011.270083
2/24/2016 21:00 1010.936331
2/24/2016 22:00 1010.920958
2/24/2016 23:00 1010.816478
2/25/2016 00:00 1010.899142
2/25/2016 01:00 1010.209392
2/25/2016 02:00 1009.700625
2/25/2016 03:00 1009.457683
2/25/2016 04:00 1009.268081
2/25/2016 05:00 1009.718639
2/25/2016 06:00 1010.745444
2/25/2016 07:00 1011.062028
2/25/2016 08:00 1011.168117
2/25/2016 09:00 1010.771281
2/25/2016 10:00 1010.138053
2/25/2016 11:00 1009.509119
2/25/2016 12:00 1008.703811
2/25/2016 13:00 1008.021547
2/25/2016 14:00 1007.774825
  .....................

我想创建一个X轴为天(2 / 25,2 / 26,......,3/25)和Y轴时间(00:00,01:00,......, 23:00)。 在每一天,我想要每小时压力的强度图。 因此,结果图表应该每天都有一个强度条并排。我已经尝试使用matlab创建一天的变量(2列因为我试图插值以获得更高的分辨率):

data =
1010.89914200000    1010.89914200000
1010.20939200000    1010.20939200000
1009.70062500000    1009.70062500000
1009.45768300000    1009.45768300000
1009.26808100000    1009.26808100000
1009.71863900000    1009.71863900000
1010.74544400000    1010.74544400000
1011.06202800000    1011.06202800000
1011.16811700000    1011.16811700000
1010.77128100000    1010.77128100000
1010.13805300000    1010.13805300000
1009.50911900000    1009.50911900000
1008.70381100000    1008.70381100000
1008.02154700000    1008.02154700000
1007.77482500000    1007.77482500000
1007.69477800000    1007.69477800000
1007.77405000000    1007.77405000000
1008.10153900000    1008.10153900000
1008.42932800000    1008.42932800000
1008.61755800000    1008.61755800000
1008.56708100000    1008.56708100000
1008.29791700000    1008.29791700000
1008.21371700000    1008.21371700000
1007.63143900000    1007.63143900000

并绘制如下:

%// Define integer grid of coordinates for the above data
[X,Y] = meshgrid(1:size(data,2), 1:size(data,1));

%// Define a finer grid of points
[X2,Y2] = meshgrid(1:0.01:size(data,2), 1:0.01:size(data,1));

%// Interpolate the data and show the output
outData = interp2(X, Y, data, X2, Y2, 'linear');
imagesc(outData);

%// Cosmetic changes for the axes
set(gca, 'XTick', linspace(1,size(X2,2),size(X,2))); 
set(gca, 'YTick', linspace(1,size(X2,1),size(X,1)));
set(gca, 'XTickLabel', 1:size(X,2));
set(gca, 'YTickLabel', 1:size(X,1));

%// Add colour bar
colorbar;

并得到以下内容:

enter image description here

但是,我正在寻找一种方法,在剩下的时间里做到这一点并将它并排绘制! (希望现在问题更好)

谢谢!!!

1 个答案:

答案 0 :(得分:3)

这是制作"日历情节的python方法"利用numpypandas,尤其是matplotlib

1)生成数据

import numpy as np
import pandas as pd

# arbitrary hourly time series data (from 11/9/2014 to 1/17/15)
time = pd.date_range(start='20141109', end='20150117', freq='H')[:-1]

# sinusoidal "pressure" data with minor noise added
x = np.linspace(-2*np.pi, 2*np.pi, num=time.size)
pressure = np.sin(x) + 100
noise = np.random.normal(0, 0.1, pressure.size)
noisy_pressure = pressure + noise

2)创建对应于月份的int s数组。这些将在以后用于迭代以标记每个图

# pick out months from time array using filtering with np.diff
months = time.month[:-1][np.diff(time.month) != 0]
months = np.append(months, time.month[-1])
# months = array([11, 12, 1]) corresponding to Nov, Dec, Jan

3)使用np.meshgrid

为绘图设置x和y轴
hours = np.unique(time.hour) # array([0,1,2,...,21,22,23])
X, hours_2d = np.meshgrid([0, 1], hours)

4)使用matplotlib.gridspec绘制日历以设置轴,使用plt.pcolormesh绘制数据的颜色图

import matplotlib.pyplot as plt
from matplotlib import gridspec
from calendar import month_name

cols = 7 # corresponding to days of the week
rows = 5 # int(np.ceil(31/7)), 31=max(days in a month)
num_days = rows * cols

for i, month in enumerate(months):
    gs = gridspec.GridSpec(rows, cols)
    fig = plt.figure()
    plt.suptitle(month_name[month], size='x-large')

    for day in range(num_days):
        # filter pressure on a daily basis according to month and day
        daily_pressure = noisy_pressure[(time.month==month) & (time.day==day+1)]
        # need to tile array in order to plot it with plt.pcolormesh
        daily_pressure_2d = np.tile(daily_pressure, (2, 1)).T

        if daily_pressure_2d.size > 0:
            ax = fig.add_subplot(gs[day])
            cmesh = ax.pcolormesh(X, hours_2d, daily_pressure_2d,
                                  vmin=noisy_pressure.min(),
                                  vmax=noisy_pressure.max())
            # remove x and y ticklabels and x tick marks
            ax.set_xticklabels([]); ax.set_xticks([])
            ax.set_yticklabels([])
            ax.set_xlabel('{month} {day}'.format(
                month=month_name[month][:3], day=day+1))
            ax.set_ylim((hours.min(), hours.max()))
        else:
            # basically create an empty plot for days without pressure data
            ax = fig.add_subplot(gs[day])
            ax.axis('off')

    plt.tight_layout() # for nicer formatting
    fig.subplots_adjust(top=0.9) # create room for suptitle
    fig.subplots_adjust(right=0.85) # create room for colorbar
    # create colorbar with customized location
    cbar_ax = fig.add_axes([0.87, 0.05, 0.03, 0.85])
    fig.colorbar(cmesh, cax=cbar_ax)

5)Admire输出 - 在日期范围内为每个月创建一个类似的图

enter image description here