def plot_x_y_yhat(x, y, y_hat, xsz, ysz, binz=False):
"""Plot x, y and y_hat side by side."""
plt.close("all")
f = plt.figure(figsize=(15, 10.8), dpi=300)
gs = gridspec.GridSpec(1, 3)
if binz:
y_hat = (y_hat > 0.5) * 1.
ims = [x, y, y_hat]
tils = [
"x:" + str(xsz) + "x" + str(xsz),
"y:" + str(ysz) + "x" + str(ysz),
"yhat:" + str(ysz) + "x" + str(ysz)]
for n, ti in zip([0, 1, 2], tils):
f.add_subplot(gs[n])
if n == 0:
plt.imshow(ims[n], cmap=cm.Greys_r)
else:
plt.imshow(ims[n], cmap=cm.Greys_r)
plt.title(ti)
return f
python类close()的实例源码
def plot_x_x_yhat(x, x_hat):
"""Plot x, y and y_hat side by side."""
plt.close("all")
f = plt.figure() # figsize=(15, 10.8), dpi=300
gs = gridspec.GridSpec(1, 2)
ims = [x, x_hat]
tils = [
"xin:" + str(x.shape[0]) + "x" + str(x.shape[1]),
"xout:" + str(x.shape[1]) + "x" + str(x_hat.shape[1])]
for n, ti in zip([0, 1], tils):
f.add_subplot(gs[n])
plt.imshow(ims[n], cmap=cm.Greys_r)
plt.title(ti)
ax = f.gca()
ax.set_axis_off()
return f
def save_images(self, X, imgfile, density=False):
ax = plt.axes()
x = X[:, 0]
y = X[:, 1]
if density:
xy = np.vstack([x,y])
z = scipy.stats.gaussian_kde(xy)(xy)
ax.scatter(x, y, c=z, marker='o', edgecolor='')
else:
ax.scatter(x, y, marker='o', c=range(x.shape[0]),
cmap=plt.cm.coolwarm)
if self.collection is not None:
self.collection.set_transform(ax.transData)
ax.add_collection(self.collection)
ax.text(x[0], y[0], str('start'), transform=ax.transAxes)
ax.axis([-0.2, 1.2, -0.2, 1.2])
fig = plt.gcf()
plt.savefig(imgfile)
plt.close()
def plot_volcano(logFC,p_val,sample_name,saveName,logFC_thresh):
fig=pl.figure()
## To plot and save
pl.scatter(logFC[(p_val>0.05)|(abs(logFC)<logFC_thresh)],-np.log10(p_val[(p_val>0.05)|(abs(logFC)<logFC_thresh)]),color='blue',alpha=0.5);
pl.scatter(logFC[(p_val<0.05)&(abs(logFC)>logFC_thresh)],-np.log10(p_val[(p_val<0.05)&(abs(logFC)>logFC_thresh)]),color='red');
pl.hlines(-np.log10(0.05),min(logFC),max(logFC))
pl.vlines(-logFC_thresh,min(-np.log10(p_val)),max(-np.log10(p_val)))
pl.vlines(logFC_thresh,min(-np.log10(p_val)),max(-np.log10(p_val)))
pl.xlim(-3,3)
pl.xlabel('Log Fold Change')
pl.ylabel('-log10(p-value)')
pl.savefig(saveName)
pl.close(fig)
# def plot_histograms(df_peaks,pntr_list):
#
# for pntr in pntr_list:
# colName =pntr[2]+'_Intragenic_position'
# pl.hist(df_peaks[colName])
# pl.xlabel(colName)
# pl.ylabel()
# pl.show()
def plot_1d(dataset, nbins, data):
with sns.axes_style('white'):
plt.rc('font', weight='bold')
plt.rc('grid', lw=2)
plt.rc('lines', lw=3)
plt.figure(1)
plt.hist(data, bins=np.arange(nbins+1), color='blue')
plt.ylabel('Count', weight='bold', fontsize=24)
xticks = list(plt.gca().get_xticks())
while (nbins-1) / float(xticks[-1]) < 1.1:
xticks = xticks[:-1]
while xticks[0] < 0:
xticks = xticks[1:]
xticks.append(nbins-1)
xticks = list(sorted(xticks))
plt.gca().set_xticks(xticks)
plt.xlim([int(np.ceil(-0.05*nbins)),int(np.ceil(nbins*1.05))])
plt.legend(loc='upper right')
plt.savefig('plots/marginals-{0}.pdf'.format(dataset.replace('_','-')), bbox_inches='tight')
plt.clf()
plt.close()
def plot_2d(dataset, nbins, data, extra=None):
with sns.axes_style('white'):
plt.rc('font', weight='bold')
plt.rc('grid', lw=2)
plt.rc('lines', lw=2)
rows, cols = nbins
im = np.zeros(nbins)
for i in xrange(rows):
for j in xrange(cols):
im[i,j] = ((data[:,0] == i) & (data[:,1] == j)).sum()
plt.imshow(im, cmap='gray_r', interpolation='none')
if extra is not None:
dataset += extra
plt.savefig('plots/marginals-{0}.pdf'.format(dataset.replace('_','-')), bbox_inches='tight')
plt.clf()
plt.close()
def plot_1d(dataset, nbins):
data = np.loadtxt('experiments/uci/data/splits/{0}_all.csv'.format(dataset), skiprows=1, delimiter=',')[:,-1]
with sns.axes_style('white'):
plt.rc('font', weight='bold')
plt.rc('grid', lw=2)
plt.rc('lines', lw=3)
plt.figure(1)
plt.hist(data, bins=np.arange(nbins+1), color='blue')
plt.ylabel('Count', weight='bold', fontsize=24)
xticks = list(plt.gca().get_xticks())
while (nbins-1) / float(xticks[-1]) < 1.1:
xticks = xticks[:-1]
while xticks[0] < 0:
xticks = xticks[1:]
xticks.append(nbins-1)
xticks = list(sorted(xticks))
plt.gca().set_xticks(xticks)
plt.xlim([int(np.ceil(-0.05*nbins)),int(np.ceil(nbins*1.05))])
plt.legend(loc='upper right')
plt.savefig('plots/marginals-{0}.pdf'.format(dataset.replace('_','-')), bbox_inches='tight')
plt.clf()
plt.close()
def plot_2d(dataset, nbins, data=None, extra=None):
if data is None:
data = np.loadtxt('experiments/uci/data/splits/{0}_all.csv'.format(dataset), skiprows=1, delimiter=',')[:,-2:]
with sns.axes_style('white'):
plt.rc('font', weight='bold')
plt.rc('grid', lw=2)
plt.rc('lines', lw=2)
rows, cols = nbins
im = np.zeros(nbins)
for i in xrange(rows):
for j in xrange(cols):
im[i,j] = ((data[:,0] == i) & (data[:,1] == j)).sum()
plt.imshow(im, cmap='gray_r', interpolation='none')
if extra is not None:
dataset += extra
plt.savefig('plots/marginals-{0}.pdf'.format(dataset.replace('_','-')), bbox_inches='tight')
plt.clf()
plt.close()
def plotValueFunction(self, valueFunction, prefix):
'''3d plot of a value function.'''
fig, ax = plt.subplots(subplot_kw = dict(projection = '3d'))
X, Y = np.meshgrid(np.arange(self.numCols), np.arange(self.numRows))
Z = valueFunction.reshape(self.numRows, self.numCols)
for i in xrange(len(X)):
for j in xrange(len(X[i])/2):
tmp = X[i][j]
X[i][j] = X[i][len(X[i]) - j - 1]
X[i][len(X[i]) - j - 1] = tmp
my_col = cm.jet(np.random.rand(Z.shape[0],Z.shape[1]))
ax.plot_surface(X, Y, Z, rstride = 1, cstride = 1,
cmap = plt.get_cmap('jet'))
plt.gca().view_init(elev=30, azim=30)
plt.savefig(self.outputPath + prefix + 'value_function.png')
plt.close()
def test_email_message():
subject = 'test subject'
body = 'test body'
recipient = ['recipient.email.address']
sender = 'sender.email.address'
attachment = 'file.txt'
f = open('file.txt','w')
f.write('test attachment')
f.close()
msg = pecos.io._create_email_message(subject, body, recipient, sender,
attachment)
assert_true(subject in msg.as_string())
assert_true(body in msg.as_string())
assert_true(recipient[0] in msg.as_string())
assert_true(sender in msg.as_string())
assert_true(attachment in msg.as_string())
def test_plot_timeseries1():
filename = abspath(join(testdir, 'plot_timeseries1.png'))
if isfile(filename):
os.remove(filename)
periods = 5
index = pd.date_range('1/1/2016', periods=periods, freq='H')
data = np.array([[1,2,3], [4,5,6], [7,8,9], [10,11,12], [13,14,15]])
df = pd.DataFrame(data=data, index=index, columns=['A', 'B', 'C'])
plt.figure()
pecos.graphics.plot_timeseries(df,yaxis_min=0, yaxis_max=20)
plt.savefig(filename, format='png')
plt.close()
assert_true(isfile(filename))
def test_plot_timeseries2():
filename = abspath(join(testdir, 'plot_timeseries2.png'))
if isfile(filename):
os.remove(filename)
periods = 5
index = pd.date_range('1/1/2016', periods=periods, freq='H')
data = np.array([[1,2,3], [4,5,6], [7,8,9], [10,11,12], [13,14,15]])
df = pd.DataFrame(data=data, index=index, columns=['A', 'B', 'C'])
tfilter = pd.Series(data = (df.index < index[3]), index = df.index)
plt.figure()
pecos.graphics.plot_timeseries(df,tfilter, yaxis_min=0, yaxis_max=20)
plt.savefig(filename, format='png')
plt.close()
assert_true(isfile(filename))
def test_plot_heatmap1():
filename = abspath(join(testdir, 'plot_heatmap1.png'))
if isfile(filename):
os.remove(filename)
periods = 5
index = pd.date_range('1/1/2016', periods=periods, freq='D')
data = np.random.rand(periods, 4)
df = pd.DataFrame(data=data, index=index, columns=['A', 'B', 'C', 'D'])
plt.figure()
pecos.graphics.plot_heatmap(df)
plt.savefig(filename, format='png', bbox_inches='tight', pad_inches = 0)
plt.close()
assert_true(isfile(filename))
def test_plot_doy_heatmap1():
filename = abspath(join(testdir, 'plot_doy_heatmap1.png'))
if isfile(filename):
os.remove(filename)
periods = 5*24 # 5 days
index = pd.date_range('3/1/2016', periods=periods, freq='H')
data = np.random.rand(periods)
df = pd.DataFrame(data=data, index=index, columns=['A'])
plt.figure()
pecos.graphics.plot_doy_heatmap(df['A'])
plt.savefig(filename, format='png')
plt.close()
assert_true(isfile(filename))
def plot_classes(y, cord, names, test_error, message=""):
plt.close("all")
cord = np.array(cord)
colors = ('b', 'g', 'r', 'c', 'm', 'y', 'k')
un = np.unique(y)
fig, ax = plt.subplots()
for u, col in zip(un, colors):
ind = np.argwhere(y == u)
x = cord[ind, :]
x = x.reshape(x.shape[0], cord.shape[1])
ax.scatter(x[:, 0], x[:, 1], label="class:" + str(u),
color=col)
plt.legend(loc='upper right', fancybox=True, shadow=True, prop={'size': 8})
fig.suptitle(
"Output prediction. Test error:" + str(test_error*100) + "%. " +
message, fontsize=8)
return fig
def plot_penalty_vl(debug, tag, fold_exp):
plt.close("all")
vl = np.array(debug["penalty"])
fig = plt.figure(figsize=(15, 10.8), dpi=300)
names = debug["names"]
for i in range(vl.shape[1]):
if vl.shape[1] > 1:
plt.plot(vl[:, i], label="layer_"+str(names[i]))
else:
plt.plot(vl[:], label="layer_"+str(names[i]))
plt.xlabel("mini-batchs")
plt.ylabel("value of penlaty")
plt.title(
"Penalty value over layers:" + "_".join([str(k) for k in names]) +
". tag:" + tag)
plt.legend(loc='upper right', fancybox=True, shadow=True, prop={'size': 8})
plt.grid(True)
fig.savefig(fold_exp+"/penalty.png", bbox_inches='tight')
plt.close('all')
del fig
def test_plot_fragility_curve1():
from scipy.stats import lognorm
filename = abspath(join(testdir, 'plot_fragility_curve1.png'))
if isfile(filename):
os.remove(filename)
FC = wntr.scenario.FragilityCurve()
FC.add_state('Minor', 1, {'Default': lognorm(0.5,scale=0.3)})
FC.add_state('Major', 2, {'Default': lognorm(0.5,scale=0.7)})
plt.figure()
wntr.graphics.plot_fragility_curve(FC)
plt.savefig(filename, format='png')
plt.close()
assert_true(isfile(filename))
def plot_true_and_augmented_data(sample,noised_sample,label,n_examples):
output_dir = os.path.split(FLAGS.output)[0]
# Save augmented data
plt.clf()
fig, ax = plt.subplots(3,1)
for t in range(noised_sample.shape[1]):
ax[t].plot(noised_sample[:,t])
ax[t].set_xlabel('time (samples)')
ax[t].set_ylabel('amplitude')
ax[0].set_title('window {:03d}, cluster_id: {}'.format(n_examples,label))
plt.savefig(os.path.join(output_dir, "augmented_data",
'augmented_{:03d}.pdf'.format(n_examples)))
plt.close()
# Save true data
plt.clf()
fig, ax = plt.subplots(3,1)
for t in range(sample.shape[1]):
ax[t].plot(sample[:,t])
ax[t].set_xlabel('time (samples)')
ax[t].set_ylabel('amplitude')
ax[0].set_title('window {:03d}, cluster_id: {}'.format(n_examples,label))
plt.savefig(os.path.join(output_dir, "true_data",
'true__{:03d}.pdf'.format(n_examples)))
plt.close()
def create_simpleDS9region(outputfile,ralist,declist,color='red',circlesize=0.5,textlist=None,clobber=False):
"""
Generate a basic DS9 region file with circles around a list of coordinates
--- INPUT ---
outputfile Path and name of file to store reigion file to
ralist List of R.A. to position circles at
declist List of Dec. to position circles at
color Color of circles
size Size of circles (radius in arcsec)
text Text string for each circle
clobber Overwrite existing files?
"""
if not clobber:
if os.path.isfile(outputfile):
sys.exit('File already exists and clobber = False --> ABORTING')
fout = open(outputfile,'w')
fout.write("# Region file format: DS9 version 4.1 \nfk5\n")
for rr, ra in enumerate(ralist):
string = 'circle('+str(ra)+','+str(declist[rr])+','+str(circlesize)+'") # color='+color+' width=3 '
if textlist is not None:
string = string+' font="times 10 bold roman" text={'+textlist[rr]+'}'
fout.write(string+' \n')
fout.close()
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
def close(self):
"""
Close the plot.
"""
if self._figure:
pylab.close(self._figure)
self._figure = None
self._plot = None
self._canvas = None
def releaseObject(self):
"""
Releases the plot and cleans up resources.
"""
self.stop()
super(PlotBase,self).releaseObject()
self.close()
def save(self, out_path):
'''Saves a figure for the monitor
Args:
out_path: str
'''
plt.clf()
np.set_printoptions(precision=4)
font = {
'size': 7
}
matplotlib.rc('font', **font)
y = 2
x = ((len(self.d) - 1) // y) + 1
fig, axes = plt.subplots(y, x)
fig.set_size_inches(20, 8)
for j, (k, v) in enumerate(self.d.iteritems()):
ax = axes[j // x, j % x]
ax.plot(v, label=k)
if k in self.d_valid.keys():
ax.plot(self.d_valid[k], label=k + '(valid)')
ax.set_title(k)
ax.legend()
plt.tight_layout()
plt.savefig(out_path, facecolor=(1, 1, 1))
plt.close()
def plot_multi_format(plot_funcs, plot_kwargs=None,
usetex=False, outdir='plots',
setting_funcs=['single', 'span', 'slides', 'thumbnails']):
"""
Outputs plots formatted 4 ways: Publication ready (narrow and wide),
PowerPoint ready, and png thumbnails.
input
-----
plot_funcs : List of functions that return a mpl figure and a filename (or list of figures and filenames)
"""
setting_dict = {'single': mpl_single_column,
'span': mpl_span_columns,
'slides': mpl_slides,
'thumbnails': mpl_thumbnails}
if not os.path.exists(outdir):
os.makedirs(outdir)
# For python 3.4
# os.makedirs(outdir, exist_ok=True)
if plot_kwargs is None:
plot_kwargs=[{}]*len(plot_funcs)
for key in setting_funcs:
setting_dict[key](usetex=usetex)
for plot_func,pkwargs in zip(plot_funcs,plot_kwargs):
figs, names = plot_func(**pkwargs)
for fig,name in zip(figs,names):
fig.savefig(os.path.join(outdir, key+'_'+name))
plt.close('all')
Top_Trending.py 文件源码
项目:Trending-Places-in-OpenStreetMap
作者: geometalab
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def plot_graphs(df, trending_daily, day_from, day_to, limit, country_code, folder_out=None):
days = pd.DatetimeIndex(start=day_from, end=day_to, freq='D')
for day in days:
fig = plt.figure()
ax = fig.add_subplot(111)
plt.rc('lines', linewidth=2)
data = trending_daily.get_group(str(day.date()))
places, clusters = top_trending(data, limit)
for cluster in clusters:
places.add(max_from_cluster(cluster, data))
ax.set_prop_cycle(plt.cycler('color', ['r', 'b', 'yellow'] + [plt.cm.Accent(i) for i in np.linspace(0, 1, limit-3)]
) + plt.cycler('linestyle', ['-', '-', '-', '-', '-', '--', '--', '--', '--', '--']))
frame = export(places, clusters, data)
frame.sort_values('trending_rank', ascending=False, inplace=True)
for i in range(len(frame)):
item = frame.index[i]
lat, lon, country = item
result_items = ReverseGeoCode().get_address_attributes(lat, lon, 10, 'city', 'country_code')
if 'city' not in result_items.keys():
mark = "%s (%s)" % (manipulate_display_name(result_items['display_name']),
result_items['country_code'].upper() if 'country_code' in result_items.keys() else country)
else:
if check_eng(result_items['city']):
mark = "%s (%s)" % (result_items['city'], result_items['country_code'].upper())
else:
mark = "%.2f %.2f (%s)" % (lat, lon, result_items['country_code'].upper())
gp = df.loc[item].plot(ax=ax, x='date', y='count', label=mark)
ax.tick_params(axis='both', which='major', labelsize=10)
ax.set_yscale("log", nonposy='clip')
plt.xlabel('Date', fontsize='small', verticalalignment='baseline', horizontalalignment='right')
plt.ylabel('Total number of views (log)', fontsize='small', verticalalignment='center', horizontalalignment='center', labelpad=6)
gp.legend(loc='best', fontsize='xx-small', ncol=2)
gp.set_title('Top 10 OSM trending places on ' + str(day.date()), {'fontsize': 'large', 'verticalalignment': 'bottom'})
plt.tight_layout()
db = TrendingDb()
db.update_table_img(plt, str(day.date()), region=country_code)
plt.close()
def plotBasisFunctions(self, eigenvalues, eigenvectors):
'''3d plot of the basis function. Right now I am plotting eigenvectors,
so each coordinate of the eigenvector correspond to the value to be
plotted for the correspondent state.'''
for i in xrange(len(eigenvalues)):
fig, ax = plt.subplots(subplot_kw = dict(projection = '3d'))
X, Y = np.meshgrid(np.arange(self.numRows), np.arange(self.numCols))
Z = eigenvectors[:,i].reshape(self.numCols, self.numRows)
for ii in xrange(len(X)):
for j in xrange(len(X[ii])/2):
tmp = X[ii][j]
X[ii][j] = X[ii][len(X[ii]) - j - 1]
X[ii][len(X[ii]) - j - 1] = tmp
my_col = cm.jet(np.random.rand(Z.shape[0],Z.shape[1]))
ax.plot_surface(X, Y, Z, rstride = 1, cstride = 1,
cmap = plt.get_cmap('jet'))
plt.gca().view_init(elev=30, azim=30)
plt.savefig(self.outputPath + str(i) + '_eig' + '.png')
plt.close()
plt.plot(eigenvalues, 'o')
plt.savefig(self.outputPath + 'eigenvalues.png')
def test_write_dashboard2(): # with text, graphics (encoded and linked), tables, and links
filename1 = abspath(join(testdir, 'test_write_dashboard2_linked_graphics.html.html'))
filename2 = abspath(join(testdir, 'test_write_dashboard2_encoded_graphics.html.html'))
graphics_filename = abspath(join(testdir, 'dashboard_graphic.png'))
if isfile(filename1):
os.remove(filename1)
if isfile(filename2):
os.remove(filename2)
if isfile(graphics_filename):
os.remove(graphics_filename)
plt.figure()
plt.plot([1, 2, 3],[1, 2, 3])
plt.savefig(graphics_filename, format='png')
plt.close()
column_names = ['loc1', 'loc2']
row_names = ['sys1', 'sys2']
content = {}
content[('sys1', 'loc1')] = {'text': 'sys1-loc1 text',
'graphics': [graphics_filename],
'link': {'Google': 'https://www.google.com', 'Pecos': 'http://pecos.readthedocs.io'} }
content[('sys1', 'loc2')] = {'text': 'sys1-loc2 text',
'table': pd.DataFrame({'sys1': [1,2,3]}).to_html()}
content[('sys2', 'loc1')] = {'text': 'sys2-loc1 text',
'graphics': [graphics_filename],
'link': {'Google': 'https://www.google.com', 'Pecos': 'http://pecos.readthedocs.io'} }
content[('sys2', 'loc2')] = {'text': 'sys2-loc2 text',
'table': pd.DataFrame({'sys2': [2,4,6]}).to_html()}
pecos.io.write_dashboard(filename1, column_names, row_names, content, encode=False)
assert_true(isfile(filename1))
pecos.io.write_dashboard(filename2, column_names, row_names, content, encode=True)
assert_true(isfile(filename2))
def test_plot_scatter1():
filename = abspath(join(testdir, 'plot_scatter1.png'))
if isfile(filename):
os.remove(filename)
x = pd.DataFrame({'x1' : pd.Series([1., 2., 3.], index=['a', 'b', 'c'])})
y = pd.DataFrame({'y1' : pd.Series([1., 2., 3.], index=['a', 'b', 'c'])})
plt.figure()
pecos.graphics.plot_scatter(x,y,xaxis_min=0.5, xaxis_max=6.5, yaxis_min=0.5, yaxis_max=3.5)
plt.savefig(filename, format='png')
plt.close()
assert_true(isfile(filename))
def test_plot_scatter2():
filename = abspath(join(testdir, 'plot_scatter2.png'))
if isfile(filename):
os.remove(filename)
x = pd.DataFrame({'x1' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
'x2' : pd.Series([4., 5., 6.], index=['a', 'b', 'c'])})
y = pd.DataFrame({'y1' : pd.Series([1., 2., 3.], index=['a', 'b', 'c'])})
plt.figure()
pecos.graphics.plot_scatter(x,y,xaxis_min=0.5, xaxis_max=6.5, yaxis_min=0.5, yaxis_max=3.5)
plt.savefig(filename, format='png')
plt.close()
assert_true(isfile(filename))
def test_plot_heatmap2():
filename = abspath(join(testdir, 'plot_heatmap2.png'))
if isfile(filename):
os.remove(filename)
data = np.array([[1,2],[3,4]])
plt.figure()
pecos.graphics.plot_heatmap(data, cmap='jet', show_axis=True)
plt.savefig(filename, format='png')
plt.close()
assert_true(isfile(filename))
def animate(y, ndim, cmap) :
plt.ion()
if ndim == 5:
plt.figure()
plt.show()
for i in range(y.shape[1]) :
print "Showing batch", i
plt.close('all')
for j in range(y.shape[0]) :
plt.imshow(y[j,i], interpolation='none', cmap=cmap)
plt.pause(0.1)
time.sleep(1)
else:
for i in range(y.shape[1]) :
print "Showing batch", i
plt.close('all')
for j in range(y.shape[0]) :
plt.figure(0)
plt.imshow(y[j,i], interpolation='none', cmap=cmap)
plt.figure(1)
plt.imshow(x[j,i], interpolation='none', cmap=cmap)
plt.pause(0.2)
time.sleep(1)