def render(nx, ny, my_data): a = [""] b = [""] for i in my_data.keys(): if i.isupper(): a.append(my_data[i]) elif i != 'bank_player_tow' and i != 'bank_player_one': b.append(my_data[i]) a.append(my_data['bank_player_tow']) a = a[::-1] b.append(my_data['bank_player_one']) data = [a, b] tb = pl.table(cellText=data, loc=(0, 0), cellLoc='center') tc = tb.properties()['child_artists'] for cell in tc: cell.set_height(1 / ny) cell.set_width(1 / nx) ax = pl.gca() ax.set_xticks([]) ax.set_yticks([]) pl.show()
def fit_plot(self, data, topn=0, bins=20): """ Create a plot. """ from matplotlib import pylab as pl distros = self.get_topn(topn) xx = numpy.linspace(data.min(), data.max(), 300) table = [] nparms = max(len(x.parms) for x in distros) tcolours = [] for dd in distros: patch = pl.plot(xx, [dd.pdf(p) for p in xx], label='%10.2f%% %s' % (100.0*dd.rss/dd.dss, dd.name)) row = ['', dd.name, '%10.2f%%' % (100.0*dd.rss/dd.dss,)] + ['%0.2f' % x for x in dd.parms] while len(row) < 3 + nparms: row.append('') table.append(row) tcolours.append([patch[0].get_markerfacecolor()] + ['w'] * (2+nparms)) # add a historgram with the data pl.hist(data, bins=bins, normed=True) tab = pl.table(cellText=table, cellColours=tcolours, colLabels=['', 'Distribution', 'Res. SS/Data SS'] + ['P%d' % (x + 1,) for x in range(nparms)], bbox=(0.0, 1.0, 1.0, 0.3)) #loc='top')) #pl.legend(loc=0) tab.auto_set_font_size(False) tab.set_fontsize(10.)
def compareAnimals(animals, precision): ''' :param animals:动物列表 :param (int) precision: 精度 :return : 表格,包含任意两个动物之间的欧式距离 ''' columnLabels = [] for a in animals: columnLabels.append(a.getName()) rowLabels = columnLabels[:] tableVals = [] #循环计算任意两个动物间的欧氏距离 for a1 in animals: row = [] for a2 in animals: if a1 == a2: row.append('--') else: distance = a1.distance(a2) row.append(str(round(distance, precision))) tableVals.append(row) #生成表格 table = plt.table(rowLabels=rowLabels, colLabels=columnLabels, cellText=tableVals, cellLoc='center', loc='center', colWidths=[0.2] * len(animals)) table.scale(1, 2.5) plt.axis('off') plt.savefig('chapter19_1.png', dpi=100) plt.show()
def fit_plot(self, data, topn=0, bins=20): """ Create a plot. """ from matplotlib import pylab as pl distros = self.get_topn(topn) xx = numpy.linspace(data.min(), data.max(), 300) table = [] nparms = max(len(x.parms) for x in distros) tcolours = [] for dd in distros: patch = pl.plot(xx, [dd.pdf(p) for p in xx], label='%10.2f%% %s' % (100.0 * dd.rss / dd.dss, dd.name)) row = ['', dd.name, '%10.2f%%' % (100.0 * dd.rss / dd.dss, ) ] + ['%0.2f' % x for x in dd.parms] while len(row) < 3 + nparms: row.append('') table.append(row) tcolours.append([patch[0].get_markerfacecolor()] + ['w'] * (2 + nparms)) # add a historgram with the data pl.hist(data, bins=bins, normed=True) tab = pl.table(cellText=table, cellColours=tcolours, colLabels=['', 'Distribution', 'Res. SS/Data SS'] + ['P%d' % (x + 1, ) for x in range(nparms)], bbox=(0.0, 1.0, 1.0, 0.3)) #loc='top')) #pl.legend(loc=0) tab.auto_set_font_size(False) tab.set_fontsize(10.)
def compareAnimals(animals, precision): ''' :param animals:动物列表 :param (int) precision: 精度 :return : 表格,包含任意两个动物之间的欧式距离 ''' columnLabels = [] for a in animals: columnLabels.append(a.getName()) rowLabels = columnLabels[:] tableVals = [] #循环计算任意两个动物间的欧氏距离 for a1 in animals: row =[] for a2 in animals: if a1 == a2: row.append('--') else: distance = a1.distance(a2) row.append(str(round(distance, precision))) tableVals.append(row) #生成表格 table = plt.table(rowLabels=rowLabels, colLabels=columnLabels, cellText=tableVals, cellLoc='center', loc='center', colWidths=[0.2]*len(animals)) table.scale(1, 2.5) plt.axis('off') plt.savefig('chapter19_1.png', dpi=100) plt.show()
def export_plot_as_jpeg(self): print('export_plot_as_jpeg()') import matplotlib.pylab as plt ES = self.export_settings P = self.plot_n_fit L = self.x_slicer.settings['stop'] - self.x_slicer.settings['start'] plt.figure() ax = plt.subplot(111) y_lim = [None, None] x_lim = [None, None] for label, (x, y) in self.gather_plot_data_for_export().items(): ax.semilogy(x, y, label=label) if len(y) == L: y_lim = [0.9 * y[-1], 1.05 * y[0]] x_lim = [0.99 * x[0], x[-1] * 1.1] # Apply limits if ES['auto_y_lim']: ax.set_ylim(y_lim) else: ax.set_ylim(ES['y_lim_min'], ES['y_lim_max']) if ES['auto_x_lim']: ax.set_xlim(x_lim) else: ax.set_xlim(ES['x_lim_min'], ES['x_lim_max']) plt.legend(loc=1) # Put the fit results somewhere if ES['include_fit_results']: tab = plt.table( cellText=P.get_result_table(), colWidths=[0.15, 0.1, 0.04], loc='lower left', colLoc=['right', 'right', 'left'], ) tab.auto_set_font_size(True) for cell in tab.get_celld().values(): cell.set_linewidth(0) if ES['plot_title'] != '': plt.title(ES['plot_title']) plt.xlabel('time ({})'.format(self.settings['time_unit'])) plt.ylabel('intensity (a.u.)') plt.tight_layout() fname = self.databrowser.settings['data_filename'] fig_name = fname.replace('.h5', '_{:0.0f}.jpg'.format(time.time())) plt.savefig(fig_name, dpi=300) plt.close() self.databrowser.ui.statusbar.showMessage('exported new data to ' + fig_name)
def plot(table,x): ny = len(table) nx = len(table[0]) pl.figure("Section "+ str(x+1)) tb = pl.table(cellText=table, loc=(0,0), cellLoc='center') ax = pl.gca() ax.set_xticks([]) ax.set_yticks([]) pl.show()
def fDisplay(matrix): pl.figure() tb = pl.table(cellText=matrix, loc=(0, 0), cellLoc='center') tc = tb.properties()['child_artists'] for cell in tc: cell.set_height(1 / ny) cell.set_width(1 / nx) ax = pl.gca() ax.set_xticks([]) ax.set_yticks([]) plt.show() return ()
def plot(table, secname): ny = len(table) nx = len(table[0]) pl.figure("Section " + secname) tb = pl.table(cellText=table, loc=(0, 0), cellLoc='center') tc = tb.properties()['child_artists'] for cell in tc: cell.set_height(1 / ny) cell.set_width(1 / nx) ax = pl.gca() ax.set_xticks([]) ax.set_yticks([]) pl.show()
def main(): plt.figure() ax = plt.gca() y = np.random.randn(9) col_label = ['col1', 'col2', 'col3'] row_label = ['row1', 'row2', 'row3'] table_value = [[11, 12, 13], [21, 22, 23], [31, 32, 33]] row_color = ['red', 'gold', 'green'] my_table = plt.table(cellText=table_value, colWidths=[0.1] * 3, rowLabels=row_label, colLabels=col_label, rowColours=row_color, loc='upper right') plt.plot(y) plt.show()
def draw_matrix(image): import matplotlib.pylab as pl h, w = image.shape nx = w ny = h data = np.asarray(image, dtype=int) pl.figure(figsize=(5, 5)) tb = pl.table(cellText=data, loc=(0, 0), cellLoc='center') tc = tb.properties()['child_artists'] for cell in tc: cell.set_height(1 / ny) cell.set_width(1 / nx) ax = pl.gca() ax.set_xticks([]) ax.set_yticks([]) plt.show()
def mytable(mypolyfit): import numpy as np import matplotlib.pyplot as plt plt.figure(figsize=(18, 4)) data = list(mypolyfit.values.astype(str)) columns = tuple(mypolyfit.columns.astype(str)) rows = np.arange(1, len(mypolyfit) + 1).astype(str) #values = np.arange(len(mypolyfit)) #value_increment = 1 # Get some pastel shades for the colors ccolors = plt.cm.BuPu(np.linspace(0, 0.5, len(columns))) rcolors = plt.cm.BuPu(np.linspace(0, 0.5, len(rows))) #n_rows = len(rows) # index = np.arange(len(rows)) + 1 #bar_width = 0.4 # Plot bars and create text labels for the table cell_text = data # Reverse colors and text labels to display the last value at the top. rcolors = rcolors[::-1] ccolors = ccolors[::-1] # Add a table at the bottom of the axes the_table = plt.table( cellText=cell_text, #colWidths=[1/15] * 15, rowLabels=rows, colColours=ccolors, rowColours=rcolors, colLabels=columns, loc='center') the_table.scale(1, 1) the_table.auto_set_font_size(False) the_table.set_fontsize(10) plt.title('Polynomial degree', fontsize=20) plt.axis('off') plt.tight_layout() plt.show()
def display_numpy_array_as_table(input_array): # This function displays a 1d or 2d numpy array (matrix). if input_array.ndim == 1: num_of_columns, = input_array.shape temp_matrix = input_array.reshape((1, num_of_columns)) elif input_array.ndim > 2: print( "Input matrix dimension is greater than 2. Can not display as table" ) return else: temp_matrix = input_array number_of_rows, num_of_columns = temp_matrix.shape fig = plt.figure() tb = plt.table(cellText=np.round(temp_matrix, 2), loc=(0, 0), cellLoc='center') for cell in tb.properties()['child_artists']: cell.set_height(1 / number_of_rows) cell.set_width(1 / num_of_columns) ax = fig.gca() ax.set_xticks([]) ax.set_yticks([]) plt.show()
def Sobol(): N = 1000 an_m = 'sobol' smp_m = 'saltelli' sa_results = pickle.load( open("SA_results_" + str(N) + "_" + an_m + "_" + smp_m, "rb")) names = [ 'r1_k1', 'r19_k1', 'r11_k1', 'r6_k1', 'r23_k1', 'r12_k1', 'r24_k1', 'r8_k1', 'r17_k1', 'r10_k1', 'r26_k1', 'r15_k1', 'r21_k1', 'r20_k1', 'r7_k1', 'r29_k1', 'r25_k1', 'r9_k1', 'r18_k1', 'r27_k1', 'r16_k1', 'r22_k1', 'r4_k1', 'r14_k1', 'r13_k1', 'r28_k1', 'r2_k1', 'r5_k1', 'r3_k1', 'default_size', 'c1_size', 'cell_size', '_c1_iron_in_Plasma_0' ] data_s1 = [] data_st = [] data_s2 = [] banned = ['default_size', 'c1_size', 'cell_size', '_c1_iron_in_Plasma_0'] for out in sa_results.keys(): s1_row = [] st_row = [] s2_row = [] for i, n in enumerate(names): if n not in banned: s1_row.append(sa_results[out]['S1'][i]) st_row.append(sa_results[out]['ST'][i]) s2_row.append(sa_results[out]['S2'][i, :29]) data_s1.append(s1_row) data_st.append(st_row) data_s2.append(s2_row) columns = [x.split('_k1')[0] for x in names if x not in banned] rows = [k.split('_')[2] for k in sa_results.keys()] n_rows = len(data_s1) index = np.arange(len(columns)) bar_width = 0.8 for data in [data_s1, data_st]: colors = pl.cm.tab20(np.linspace(0, 1, n_rows)) y_offset = np.zeros(len(columns)) pl.figure(figsize=(30, 8)) cell_text = [] for row in range(n_rows): pl.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row]) y_offset = y_offset + data[row] cell_text.append(['%.3f' % x for x in data[row]]) # Reverse colors and text labels to display the last value at the top. colors = colors[::-1] cell_text.reverse() the_table = pl.table(cellText=cell_text, rowLabels=rows, rowColours=colors, colLabels=columns, loc='bottom') the_table.auto_set_font_size(False) the_table.set_fontsize(9) pl.subplots_adjust(bottom=0.2) pl.xticks([]) pl.margins(x=0) if data == data_s1: pl.savefig('Sobol_S1_results.png', bbox_inches="tight", dpi=300) elif data == data_st: pl.savefig('Sobol_ST_results.png', bbox_inches="tight", dpi=300) pl.clf() for i in range(n_rows): row_1 = data_s1[i] row_T = data_st[i] for n in range(len(row_1)): if row_T[n] > row_1[n]: # possible higher order interactions interactions = list( np.nonzero(np.nan_to_num(data_s2[i][n]) > 0.05)[0]) if interactions: print(rows[i], columns[n], row_T[n], row_1[n], list(np.array(columns)[interactions]), list(np.array(data_s2[i][n])[interactions]))
def Morris(): N = 1000 an_m = 'morris' smp_m = 'morris' sa_results = pickle.load( open("SA_results_" + str(N) + "_" + an_m + "_" + smp_m, "rb")) data_mu = [] data_mu_star = [] data_sigma = [] banned = ['default_size', 'c1_size', 'cell_size', '_c1_iron_in_Plasma_0'] for out in sa_results.keys(): mu_row = [] mu_star_row = [] sigma_row = [] for i, n in enumerate(sa_results[out]['names']): if n not in banned: mu_row.append(sa_results[out]['mu'][i]) mu_star_row.append(sa_results[out]['mu_star'][i]) sigma_row.append(sa_results[out]['sigma'][i]) data_mu.append(mu_row) data_mu_star.append(mu_star_row) data_sigma.append(sigma_row) columns = [ x.split('_k1')[0] for x in sa_results['iron_in_Plasma_c1']['names'] if x not in banned ] rows = [k.split('_')[2] for k in sa_results.keys()] n_rows = len(data_mu) index = np.arange(len(columns)) bar_width = 0.8 for data in [data_mu_star, data_sigma]: colors = pl.cm.tab20(np.linspace(0, 1, n_rows)) y_offset = np.zeros(len(columns)) pl.figure(figsize=(30, 8)) cell_text = [] for row in range(n_rows): pl.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row]) y_offset = y_offset + data[row] cell_text.append(['%.3f' % x for x in data[row]]) # Reverse colors and text labels to display the last value at the top. colors = colors[::-1] cell_text.reverse() the_table = pl.table(cellText=cell_text, rowLabels=rows, rowColours=colors, colLabels=columns, loc='bottom') the_table.auto_set_font_size(False) the_table.set_fontsize(9) pl.subplots_adjust(bottom=0.2) pl.xticks([]) pl.margins(x=0) if data == data_mu_star: pl.savefig('Morris_mu_star_results.png', bbox_inches="tight", dpi=300) elif data == data_sigma: pl.savefig('Morris_sigma_results.png', bbox_inches="tight", dpi=300) pl.clf()
#!-*-coding:utf8-*- import matplotlib.pylab as plt import numpy as np plt.figure(0) ax = plt.gca() y = np.random.randn(9) col_labels = ['a', 'b', 'c'] row_labels = ['1', '2', '3'] table_vals = [[11, 12, 13], [21, 22, 23], [31, 32, 33]] row_colors = ['r', 'g', 'b'] plt.table(cellText=table_vals,colWidths=[0.1]*3, \ rowLabels=row_labels,colLabels=col_labels, \ rowColours=row_colors, \ colColours=row_colors,\ loc='upper right') plt.plot(y) plt.figure(1) # 1 1 1 # 2 3 3 # 2 3 3 #将Figure划分为3*3的网格生成子区,起始子区0行0列 axes1 = plt.subplot2grid((3, 3), (0, 0), colspan=3) #跨越3列则第一个子区占据了网格第0行 axes2 = plt.subplot2grid((3, 3), (1, 0), rowspan=2) axes3 = plt.subplot2grid((3, 3), (1, 1), rowspan=2, colspan=2) plt.show()
print("cont:", cost_val, "train accuracy:", train_a, "test accuracy:", a, "step:", step) accuracy_test_list.append(a) if train_a > 0.95: break saver = tf.train.Saver() saver.save(sess, './model/ssum_predict') print(y_test_data) success = 0 fail = 0 for data in y_test_data: if data[0] == 0.0: fail += 1 if data[0] == 1.0: success += 1 print("success:", success, "fail:", fail) gat, = plt.plot(step_list, accuracy_test_list, 'ro-') bat, = plt.plot(step_list, accuracy_train_list, 'bs-') plt.legend([gat, bat], ['test data', 'train data'], loc=2) plt.xlabel('number of step') plt.ylabel('accuracy') cell_text = [] cell_text.append(["asd"]) plt.table(cellText=cell_text, loc='top') plt.show()
# -*- coding: utf-8 -*- import matplotlib.pylab as plt import numpy as np plt.figure(1, figsize=(8, 6), dpi=300) axes = plt.gca() y = np.random.randn(9) col_labels = ['col1', 'col2', 'col3'] row_labels = ['row1', 'row2', 'row3'] table_vals = [[11, 12, 13], [21, 22, 23], [28, 29, 30]] row_colors = ['red', 'gold', 'green'] the_table = plt.table(cellText=table_vals, colWidths=[0.1] * 3, rowLabels=row_labels, colLabels=col_labels, rowColours=row_colors, loc='upper right') plt.text(12, 3.4, 'Table Title', size=8) plt.plot(y) plt.show()
ax1.plot(radi, s_fixed_time, label='$t=%0.1f s$' % t) ax1.legend(title='Theis Solution', loc='upper right', fancybox=True, shadow=True) ax2.legend(title='Theis Solution', loc='lower right', fancybox=True, shadow=True) cellText = table_values ax = plt.subplot(111, frame_on=False) ax.xaxis.set_visible(False) ax.yaxis.set_visible(False) the_table = plt.table(cellText=table_values, colWidths=[.1, .3, .3, .3], colLabels=col_labels, cellLoc='center', colLoc='center', loc='best') properties = the_table.properties() cells = properties['child_artists'] for cell in cells: cell.set_height(.045) the_table.auto_set_font_size(False) the_table.set_fontsize(12) plt.show()
import matplotlib.pylab as plt import numpy as np plt.figure() axes = plt.gca() y = np.random.randn(9) col_labels = ["col1", "col2", "col3"] row_labels = ["row1", "row2", "row3"] table_vals = [[11, 12, 13], [21, 22, 23], [28, 29, 30]] row_colors = ["red", "gold", "green"] the_table = plt.table( cellText=table_vals, colWidths=[0.1] * 3, rowLabels=row_labels, colLabels=col_labels, rowColours=row_colors, loc="upper right", ) plt.text(12, 3.4, "Table Title", size=8) plt.plot(y) plt.show()
def showMatr(matrix): table(cellText=matrix, loc='center', cellLoc='center', bbox=[0.2, 0.2, 0.5, 0.5]) show()