2013年03月25日 星期一 17:05
def example_data():
data = {
'column names':
['OC1', 'OC2', 'OC3'],
'data1':
[[1.00, 0.00, 1.00, 1.00, 0.00, 1.06, 1.01, 1.00, 1.00],
[0.07, 0.95, 0.04, 0.05, 0.00, 0.02, 0.01, 0.00, 0.00],
[0.01, 0.02, 0.85, 0.19, 0.05, 0.10, 0.00, 0.00, 0.00],
[0.02, 0.01, 0.07, 0.01, 0.21, 0.12, 0.98, 0.00, 0.00],
[0.01, 0.01, 0.02, 0.71, 0.74, 0.70, 0.00, 0.00, 0.00]],
}
return data
以上定义的'data1'中数据比较少,可以直接写在代码中, 请问如何把它换成保存在txt文件中比较大的数据:
2013年03月25日 星期一 17:09
我直接导入数据并且替换, 如下:
def example_data():
d1 = np.loadtxt('d1.txt')
d2 = np.loadtxt('d2.txt'),
d3 ...;
d4....
data = {
'column names':
['OC1', 'OC2', 'OC3'],
'data1':
[d1, d2, d3, d4],
}
return data
结果调用的时候出现格式错误:
raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension
2013年03月30日 星期六 17:16
请把代码和数据贴全。
2013年04月04日 星期四 16:12
"""
Example of creating a radar chart (a.k.a. a spider or star chart) [1]_.
Although this example allows a frame of either 'circle' or 'polygon', polygon
frames don't have proper gridlines (the lines are circles instead of polygons).
It's possible to get a polygon grid by setting GRIDLINE_INTERPOLATION_STEPS in
matplotlib.axis to the desired number of vertices, but the orientation of the
polygon is not aligned with the radial axes.
.. [1] http://en.wikipedia.org/wiki/Radar_chart
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.spines import Spine
from matplotlib.projections.polar import PolarAxes
from matplotlib.projections import register_projection
def radar_factory(num_vars, frame='circle'):
"""Create a radar chart with `num_vars` axes.
This function creates a RadarAxes projection and registers it.
Parameters
----------
num_vars : int
Number of variables for radar chart.
frame : {'circle' | 'polygon'}
Shape of frame surrounding axes.
"""
# calculate evenly-spaced axis angles
theta = 2*np.pi * np.linspace(0, 1-1./num_vars, num_vars)
# rotate theta such that the first axis is at the top
theta += np.pi/2
def draw_poly_patch(self):
verts = unit_poly_verts(theta)
return plt.Polygon(verts, closed=True, edgecolor='k')
def draw_circle_patch(self):
# unit circle centered on (0.5, 0.5)
return plt.Circle((0.5, 0.5), 0.5)
patch_dict = {'polygon': draw_poly_patch, 'circle': draw_circle_patch}
if frame not in patch_dict:
raise ValueError('unknown value for `frame`: %s' % frame)
class RadarAxes(PolarAxes):
name = 'radar'
# use 1 line segment to connect specified points
RESOLUTION = 1
# define draw_frame method
draw_patch = patch_dict[frame]
def fill(self, *args, **kwargs):
"""Override fill so that line is closed by default"""
closed = kwargs.pop('closed', True)
return super(RadarAxes, self).fill(closed=closed, *args, **kwargs)
def plot(self, *args, **kwargs):
"""Override plot so that line is closed by default"""
lines = super(RadarAxes, self).plot(*args, **kwargs)
for line in lines:
self._close_line(line)
def _close_line(self, line):
x, y = line.get_data()
# FIXME: markers at x[0], y[0] get doubled-up
if x[0] != x[-1]:
x = np.concatenate((x, [x[0]]))
y = np.concatenate((y, [y[0]]))
line.set_data(x, y)
def set_varlabels(self, labels):
self.set_thetagrids(theta * 180/np.pi, labels)
def _gen_axes_patch(self):
return self.draw_patch()
def _gen_axes_spines(self):
if frame == 'circle':
return PolarAxes._gen_axes_spines(self)
# The following is a hack to get the spines (i.e. the axes frame)
# to draw correctly for a polygon frame.
# spine_type must be 'left', 'right', 'top', 'bottom', or `circle`.
spine_type = 'circle'
verts = unit_poly_verts(theta)
# close off polygon by repeating first vertex
verts.append(verts[0])
path = Path(verts)
spine = Spine(self, spine_type, path)
spine.set_transform(self.transAxes)
return {'polar': spine}
register_projection(RadarAxes)
return theta
def unit_poly_verts(theta):
"""Return vertices of polygon for subplot axes.
This polygon is circumscribed by a unit circle centered at (0.5, 0.5)
"""
x0, y0, r = [0.5] * 3
verts = [(r*np.cos(t) + x0, r*np.sin(t) + y0) for t in theta]
return verts
def example_data():
filepath = 'C:\\Users\\Jingsong\\Desktop\\My papers\\PARADE2011 paper\\Windrose plot'
DATA = np.loadtxt(filepath + '\\' + 'All data.txt',dtype = np.object)
#Julianday CO NOx O3 HCHO Vaisala(WD) Vaisala(WS)
data = np.delete(DATA,0,axis=0)
k1 = len(data)
utc = data[:,0]
co = data[:,1]
nox = data[:,2]
o3 = data[:,3]
hcho = data[:,4]
wd = data[:,5]
ws = data[:,6]
UTC, WD, WS, CO, NOx, O3 = [], [], [], [], [],[]
f1, f2, f3, f4 = 30, 8, 30, 100
for j in range(k1):
if ws[j] != '--' and co[j] != '--' and o3[j] != '--' and nox[j] != '--':
CO.append(float(co[j]))
O3.append(float(o3[j]))
NOx.append(float(nox[j]))
UTC.append(float(utc[j]))
WD.append(float(wd[j]))
WS.append(float(ws[j]))
else :
pass
data = {
'column names':
# ['W','WSW','SW','SSW','S','SSE','SE','ESE','E', 'ENE', 'NE', 'NNE','N', 'NNW', 'NW', 'WNW'],
['N','N-N-W','N-W','W-N-W','W','W-S-W','S-W','S-S-W','S','S-S-E','S-E','E-S-E','E','E-N-E','N-E','N-N-E'
],
'Mean (ppb)': # CO, NOx, O3
[
[102.24627786,106.83650667,99.92441288,96.53708956,90.24190571,90.71115166,97.72157486,
104.4235838,113.2356681,114.58009237,114.84519222,112.92359114,109.30011623,107.12493302,103.95084444,
101.40956103],
[1.80433923*f1,2.64418*f1,3.03536707*f1,2.50796932*f1,1.73060199*f1,1.58757234*f1,2.17394835*f1,2.60896209*f1,3.15702655*f1,
3.58586803*f1,3.48038276*f1,2.8917116*f1,2.38614875*f1,2.40764455*f1,2.6135483*f1,2.39924822*f1],
[41.03782609, 44.49,41.73207447, 40.31592105, 37.31870728, 36.05327068,
37.84087008, 43.77360938, 54.12770742, 54.6471243, 55.18656, 55.96642317,
52.49690426, 43.06090741, 35.09577, 35.81180734 ],
],
'Median (ppb)':
[
[96.22278,96.22278,94.77351,92.15701,84.86605,85.090805,92.95792,101.62886,
111.48659,113.70175,114.727555,111.69604,106.81137,103.645705,100.900165,100.195495],
[1.76005*f3,2.64418*f3,2.52785*f3,1.93465*f3,1.375345*f3,1.29202*f3,1.7407*f3,2.07194*f3,2.38818*f3,2.635375*f3,2.39237*f3,
1.97503*f3,1.82236*f3,1.82236*f3,1.80325*f3,1.96315*f3],
[40.27, 44.49, 40.51, 38.21, 35.19, 34.545, 34.82, 38.08, 56.46, 55.58,
55.575, 56.74, 53.635, 46.095, 40.005, 37.16],
],
'Maximum (ppb)':
[[131.33685,131.33685,164.48452,164.48452,164.0069,170.14479,170.14479,163.73189,
161.9768,161.9768,179.63048,179.63048,155.65133,155.65133,148.27387,125.06671],
[3.52831*f2,3.52831*f2,8.87297*f2,8.87297*f2,8.50828*f2,6.07696*f2,12.98087*f2,12.98087*f2,15.03495*f2,17.31412*f2,19.37536*f2,
19.37536*f2,15.69124*f2,8.4555*f2,7.29886*f2,7.29886*f2],
[50.49, 48.71, 74.04, 74.04, 78.58, 78.58, 78.55, 80.27, 87.24, 88.73, 88.73, 73.00,
72.89, 70.57, 51.43, 52.05 ],
],
'Minimum (ppb)':
[
[90.54955,92.94989,77.3248,77.3248,74.055,72.75713,72.75713,74.15136,75.91537,77.13579,
78.21124,85.17453,89.88097,87.87074,87.87074,87.36955],
[0.59927*f4, .76005*f4, 0.49778*f4, 0.36868*f4, 0.14775*f4, 0.14775*f4, 0.17049*f4, 0.17049*f4, 0.20257*f4,
0.32186*f4, 0.34364*f4, 0.34364*f4, 0.50515*f4, 0.65795*f4, 0.48625*f4, 0.43119*f4 ],
[34.08, 40.27, 23.49, 22.13, 14.65, 13.0, 13.0, 14.57, 14.75, 14.17,
14.17, 26.71, 9.467, 9.467, 9.317, 9.317 ],
],
}
return data
if __name__ == '__main__':
N = 16
theta = radar_factory(N, frame='polygon')
data = example_data()
spoke_labels = data.pop('column names')
plt.figure(4).clf()
fig = plt.figure(4,figsize=(9, 9))
fig.subplots_adjust(wspace=0.25, hspace=0.20, top=0.85, bottom=0.05)
colors = ['b', 'r', 'g', 'm', 'y']
# Plot the four cases from the example data on separate axes
for n, title in enumerate(data.keys()):
ax = fig.add_subplot(2, 2, n+1, projection='radar')
# plt.rgrids([0.2, 0.4, 0.6, 0.8])
plt.rgrids(angle=11.25)
ax.set_title(title, weight='bold', size='medium', position=(0.5, 1.1),
horizontalalignment='center', verticalalignment='center')
for d, color in zip(data[title], colors):
# ax.plot(theta, d, color=color)
ax.plot(theta, d, 'o-')
ax.fill(theta, d, facecolor=color, alpha=0.05)
ax.set_varlabels(spoke_labels)
# add legend relative to top-left plot
plt.subplot(2, 2, 1)
labels = ('CO', 'NOx', 'O3')
legend = plt.legend(labels, loc=(1.1, -0.2), labelspacing=0.2)
plt.setp(legend.get_texts(), fontsize='12')
# plt.figtext(0.5, 0.965, 'windrose plot',
# ha='center', color='black', weight='bold', size='large')
plt.show()
代码如上,请问如何把
data = { 'Mean (ppbv)': # CO, NOx, O3} 中定义的3组数据替换成 np.loadtxt导入的数据:
即:
CO.append(float(co[j]))
O3.append(float(o3[j]))
NOx.append(float(nox[j]))
Zeuux © 2024
京ICP备05028076号