Python 并排绘制每天的彩色地图图
我有以下两列数据:Python 并排绘制每天的彩色地图图,python,matlab,matplotlib,Python,Matlab,Matplotlib,我有以下两列数据: 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.
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为一天创建一个变量(两列,因为我试图插值以获得更高的分辨率):
并绘制如下:
%// 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;
并得到以下结果:
但我正在寻找一种方法,在接下来的日子里做到这一点,并并排策划!
(希望现在的问题更好)
谢谢你 这里有一种
python
方法来制作“日历图”,它利用numpy
、pandas
,尤其是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
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) 佩服输出——为日期范围内的每个月创建一个类似的绘图
链接和绘图。这是2013年12月或2002年12月(至少在美国和英国),因为它从周日开始。。。在开始处添加空格,使其更像一个真实的日历页。。当然,我已经投了赞成票……虽然我喜欢你的建议@GBOFI,但经过一段时间的摆弄之后,我认为这可能会带来更多的麻烦。您可以使用
datetime.datetime(年、月、日).weekday()
获取偏移量,但它似乎不像将偏移量添加到gs[day]
中那样简单。由于主要目的只是为了数据可视化,而不是与日历的实际对应关系,我将把它留给读者作为练习:)
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)