def term_sheet_to_json(self, window=30, windows=[30, 60, 90, 120], quantiles=[0.25, 0.75], bins=100, normed=True, bench='SPY', open=False): ext = '.json' cones_fig, cones_plt = self.cones(windows=windows, quantiles=quantiles) rolling_quantiles_fig, rolling_quantiles_plt = self.rolling_quantiles( window=window, quantiles=quantiles) rolling_extremes_fig, rolling_extremes_plt = self.rolling_extremes( window=window) rolling_descriptives_fig, rolling_descriptives_plt = self.rolling_descriptives( window=window) histogram_fig, histogram_plt = self.histogram(window=window, bins=bins, normed=normed) mpld3.save_json(cones_fig, self.define_fig("cones", ext)) mpld3.save_json(rolling_quantiles_fig, self.define_fig("rolling_quantiles", ext)) mpld3.save_json(rolling_extremes_fig, self.define_fig("rolling_extremes", ext)) mpld3.save_json(rolling_descriptives_fig, self.define_fig("rolling_desc", ext)) mpld3.save_json(histogram_fig, self.define_fig("histogram", ext)) pyplt.close(cones_fig) pyplt.close(rolling_quantiles_fig) pyplt.close(rolling_extremes_fig) pyplt.close(rolling_descriptives_fig) pyplt.close(histogram_fig)
def sin_cos_d3_save(): """ Web上でy=sin(x), y=cos(x)の図の表示するためのデータをJSONで保存 :return: """ base = os.path.dirname(os.path.abspath(__file__)) path = os.path.normpath(os.path.join(base, 'result/sin_cos.json')) plt = sin_cos_plt() mpld3.save_json(plt.gcf(), open(path, "w"))
def figlist_to_json(rootname, figlist, postfixes=None): from matplotlib.backends.backend_agg import FigureCanvasAgg from itertools import izip, count from mpld3 import save_json if postfixes is None: postfixes = ("fig%02d" % i for i in count(1)) for postfix, fig in izip(postfixes, figlist): FigureCanvasAgg(fig) save_json(fig, "%s_%s.json" % (rootname, postfix))
def to_html(self, title, subfolder, index_filename, template_name): subfolder = subfolder.replace(" ","_") if subfolder != '': try: os.makedirs(os.path.join(self.local_folder, subfolder)) except: pass subfolder += '/' #use mpl3d to create a json file for every matplotlib figure #these json files will be copied to S3, to be loaded by a web browser and visualized by the MPLD3 library for idx, (key,val) in enumerate(self.items()): if val['datatype']=='MPLD3': json_filename = subfolder + key.replace(" ","_") + '.json' mpld3.save_json(val['data'], os.path.join(self.local_folder, json_filename)) #val['data'] is expected to be a matplotlib figure; save the json file to the local folder, it will be copied to S3 val['filename'] = json_filename #add the json's filename to the dictionary, it will be use dby the jinja2 template (in the javascript to load json from S3) elif val['datatype']=='MPLPNG': png_filename = key.replace(" ","_") + '.png' val['data'].savefig(os.path.join(self.local_folder, subfolder+png_filename)) #val['data'] is expected to be a matplotlib figure #del val['data'] #gc.collect() val['filename'] = png_filename #add the png's filename to the dictionary, it will be used by the jinja2 template to create a IMG tag with this file as source) else: pass #use Jinja2 to instantiate a web page that will load these json files and display the plots with the mpl3d javascript front-end env = Environment( loader=FileSystemLoader('./covid/templates'), #autoescape=select_autoescape(['html', 'xml']) ) template = env.get_template(template_name) page = template.render(title=title, items=self) file = open(os.path.join(self.local_folder,subfolder+index_filename),'w') file.write(page) file.close()
import mpld3 import matplotlib import matplotlib.pyplot as plt labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' sizes = [15, 30, 45, 10] explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs') fig, ax = plt.subplots() ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90) ax.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. fig_var = [x[0] for x in locals().items() if isinstance(x[1], matplotlib.pyplot.Figure)][0] print(fig_var) globals()[fig_var] = matplotlib.pyplot.Figure mpld3.save_json(fig_var, fileobj='json_fig.json')
#!/usr/bin/python import matplotlib.pyplot as plt import numpy as np import mpld3 N = 20 fig, ax = plt.subplots() x = np.random.rand(N) y = np.random.rand(N) ax.plot(x, y) mpld3.save_html(fig,"figout.html") mpld3.save_json(fig,"figout.json") html = mpld3.fig_to_html(fig,"figout.json") print html #https://stackoverflow.com/questions/22981359/display-multiple-mpld3-exports-on-a-single-html-page
# In[13]: ## Plot Extents # Plot pan-Arctic sea ice extent f = plt.figure(figsize=(10, 5)) ax1 = plt.subplot(1, 1, 1) # Observations da_81_30_avg.plot(ax=ax1, label='NSIDC NRT\n (Maslanik et al. 1999)') ds_ext.sel(datetime=da_81_30_avg.time).Extent.plot(label='NSIDC V3', color='m') ax1.set_ylabel('Millions of square km') plt.legend(loc='lower right') #bbox_to_anchor=(1.03, 1.05)) # Save to png and json (for website) f_name = os.path.join(fig_dir, 'panArcticSIC_Forecast_30days') plt.title('Last 30 days') f.savefig(f_name + '.png', bbox_inches='tight', dpi=200) mpld3.save_json(f, f_name + '.json') mpld3.save_html(f, f_name + '.html') ## Plot pan-Arctic sea ice extent f = plt.figure(figsize=(10, 5)) ax1 = plt.subplot(1, 1, 1) # Observations da_81_3m_avg.plot(ax=ax1, label='NSIDC NRT\n (Maslanik et al. 1999)') ds_ext.sel(datetime=da_81_3m_avg.time).Extent.plot(label='NSIDC V3', color='m') plt.title('Last 3 months') ax1.set_ylabel('Millions of square km') plt.legend(loc='lower right') f.savefig(os.path.join(fig_dir, 'panArcticSIC_Forecast_3months.png'), bbox_inches='tight', dpi=200) # Plot Recent maps
def PCA2d(sleppa, toflur): # prófa fyrst að taka bara costoflivingindex og veðrið í viku 30 # töflurnar geta verið prices, costoflivingindex, qualityoflifeindex, weathereurope og weatherchanceseurope pca = PCA(n_components=2) fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE')) if (len(toflur) == 1): if "WeatherEurope" in toflur or "WeatherChancesEurope" in toflur: query = cur.execute("SELECT * FROM " + toflur[0] + " where week = 20") cols = [column[0] for column in query.description] results = pd.DataFrame.from_records(data=query.fetchall(), columns=cols) cities = results['city'].tolist() dropColumns = ['week', 'city'] if toflur[0] == "WeatherEurope": dropColumns.append('cloud_cover') results.drop(columns=dropColumns, inplace=True) results = results.apply(pd.to_numeric) resultsScaled = preprocessing.StandardScaler().fit_transform( results) principalComponentsScaled = pca.fit_transform(resultsScaled) principalDfScaled = pd.DataFrame( data=principalComponentsScaled, columns=['principal component 1', 'principal component 2']) N = len(cities) col = np.random.rand(N) mynd = ax.scatter(principalDfScaled['principal component 1'], principalDfScaled['principal component 2'], c=col, alpha=0.5) ax.set_title("2-dimensional PCA of weather in Europe in late June") if "CostOfLivingIndex" in toflur or "QualityOfLifeIndex" in toflur: query2 = cur.execute("SELECT * from " + toflur[0]) cols = [column[0] for column in query2.description] results2 = pd.DataFrame.from_records(data=query2.fetchall(), columns=cols) cities2 = results2['City'].tolist() results2.drop(columns=['City', 'Rank'], inplace=True) results2 = results2.apply(pd.to_numeric) results2Scaled = preprocessing.StandardScaler().fit_transform( results2) principalComponentsScaled2 = pca.fit_transform(results2Scaled) principalDfScaled2 = pd.DataFrame( data=principalComponentsScaled2, columns=['principal component 1', 'principal component 2']) col = np.random.rand(len(cities2)) mynd = ax.scatter(principalDfScaled2['principal component 1'], principalDfScaled2['principal component 2'], c=col, alpha=0.5) if ("CostOfLivingIndex" in toflur): ax.set_title("2-dimensional PCA of cost of living indeces") else: ax.set_title("2-dimensional PCA of quality of life indeces") if "Prices" in toflur: query3 = cur.execute("SELECT * from " + toflur[0]) cols = [column[0] for column in query2.description] print(cols) results3 = pd.DataFrame.from_records(data=query3.fetchall(), columns=cols) cities3 = results3['City'].tolist() irrelevantAttributes = [ "City", "Monthly Pass", "Volkswagen Golf", "Toyota Corolla", "Basic Utilities", "Internet", "Fitness Club", "Tennis Court Rent", "Preschool Month", "Primary School Year", "Rent 1 Bedroom Center" "Rent 1 Bedroom Outside Center" "Rent 3 Bedrooms Center", "Rent 3 Bedrooms Outside Center", "Price m2 Center", "Price m2 Outside Center", "Average Monthly Salary After Tax", "Mortgage Interest Rate" ] results3.drop(columns=irrelevantAttributes, inplace=True) results3 = results3.apply(pd.to_numeric) results3Scaled = preprocessing.StandardScaler().fit_transform( results3) principalComponentsScaled3 = pca.fit_transform(results3Scaled) principalDfScaled3 = pd.DataFrame( data=principalComponentsScaled3, columns=['principal component 1', 'principal component 2']) col = np.random.rand(len(cities3)) mynd = ax.scatter(principalDfScaled3['principal component 1'], principalDfScaled3['principal component 2'], c=col, alpha=0.5) ax.set_title("2-dimensional PCA of prices of goods") else: # sameina toflulisti = [] for tafla in toflur: if tafla == "WeatherEurope" or tafla == "WeatherChancesEurope": query = cur.execute("SELECT * FROM " + tafla + " where week = 20") cols = [column[0] for column in query.description] results = pd.DataFrame.from_records(data=query.fetchall(), columns=cols) cities = results['city'].tolist() toflulisti.append(results) if tafla == "CostOfLivingIndex" or tafla == "QualityOfLifeIndex": query2 = cur.execute("SELECT * from " + tafla) cols = [column[0] for column in query2.description] results2 = pd.DataFrame.from_records(data=query2.fetchall(), columns=cols) cities = results2['City'].tolist() results2.drop(sleppa[1], axis=1) results2.drop("Rank", axis=1, inplace=True) results2.rename(index=str, columns={"City": "city"}, inplace=True) toflulisti.append(results2) if tafla == "Prices": query3 = cur.execute("SELECT * from " + toflur[0]) cols = [column[0] for column in query2.description] results3 = pd.DataFrame.from_records(data=query3.fetchall(), columns=cols) cities3 = results3['City'].tolist() irrelevantAttributes = [ "City", "Monthly Pass", "Volkswagen Golf", "Toyota Corolla", "Basic Utilities", "Internet", "Fitness Club", "Tennis Court Rent", "Preschool Month", "Primary School Year", "Rent 1 Bedroom Center" "Rent 1 Bedroom Outside Center" "Rent 3 Bedrooms Center", "Rent 3 Bedrooms Outside Center", "Price m2 Center", "Price m2 Outside Center", "Average Monthly Salary After Tax", "Mortgage Interest Rate" ] results3.drop(columns=irrelevantAttributes, inplace=True) toflulisti.append(results3) saman = reduce((lambda results, results2: pd.merge(results, results2, how='inner', left_on=['city'], right_on=['city'])), toflulisti) cities = list(saman["city"]) samandrop = ['city', 'week', 'cloud_cover'] saman.drop(columns=samandrop, inplace=True) samanScaled = preprocessing.StandardScaler().fit_transform(saman) principalComponentsScaledSaman = pca.fit_transform(samanScaled) principalDfScaledSaman = pd.DataFrame( data=principalComponentsScaledSaman, columns=['principal component 1', 'principal component 2']) col = np.random.rand(len(saman)) mynd = ax.scatter(principalDfScaledSaman['principal component 1'], principalDfScaledSaman['principal component 2'], c=col, alpha=0.5) # frekar en að hafa results, results2 o.s.frv. þá get ég haft lista/dict results["WeatherEurope"], results["CostOfLivingIndex"] # Prices og QualityOfLifeIndex er keimlíkt CostOfLivingIndex # WeatherChancesEurope er keimlíkt WeatherEurope # nema það þarf að droppa öðrum dálkum ax.set_xlabel('Principal Component 1', fontsize=15) ax.set_ylabel('Principal Component 2', fontsize=15) if (len(toflur) > 1): title = "2-dimensional PCA of " + toflur[0] i = 1 for tafla in toflur[1:]: if i != (len(toflur) - 1): title = title + ", " + tafla + " " else: title = title + " and " + tafla i = i + 1 ax.set_title(title) # vantar að staðfesta að röðin sé eins? tooltip = mpld3.plugins.PointLabelTooltip(mynd, labels=cities) mpld3.plugins.connect(fig, tooltip) mpld3.save_json(fig, "PCA.json")
def one_bar_per_cmd(args): matplotlib.rcParams['axes.labelsize'] = args.fontsize matplotlib.rcParams['axes.titlesize'] = args.fontsize + 2 matplotlib.rcParams['xtick.labelsize'] = args.fontsize matplotlib.rcParams['ytick.labelsize'] = args.fontsize matplotlib.rcParams['legend.fontsize'] = args.fontsize matplotlib.rcParams['font.family'] = 'serif' matplotlib.rcParams['font.serif'] = ['Computer Modern Roman'] matplotlib.rcParams['text.usetex'] = True matplotlib.rcParams['figure.max_open_warning'] = 400 matplotlib.rcParams['image.cmap'] = "gray" matplotlib.rcParams['image.interpolation'] = "none" matplotlib.rcParams['figure.autolayout'] = True pylab.clf() # Essential! Otherwise the plots will be f**d up! pylab.figure(1) pylab.gca().yaxis.grid(True) # Makes it easier to compare bars pylab.gca().xaxis.grid(False) pylab.gca().set_axisbelow(True) plt.style.use(args.pyplot_style) cmd_list = json.load(args.results)['cmd_list'] cmd_list = util.filter_cmd_list(cmd_list, args.labels_to_include, args.labels_to_exclude) (cmd_list, label_map) = util.translate_dict(cmd_list, args.label_map) # For a bar-chart, we need labels, values, and errors labels = [util.texsafe(cmd['label']) for cmd in cmd_list] values = [] std = [] for cmd in cmd_list: res = util.extract_succeed_results(cmd, args.parse_regex, args.py_type) if len(res) == 0: res = [0] values.append(util.mean(res)) std.append(util.standard_deviation(res)) # Let's write the bar-chart using matplotlib margin = 0.05 width = (1. - 1. * margin) / len(cmd_list) ind = np.arange(len(cmd_list)) # the x locations for each bar fig, ax = plt.subplots() ax.bar(ind * width, values, width, log=args.ylog, yerr=std) if args.ymin is not None: plt.ylim(ymin=float(args.ymin)) if args.ymax is not None: plt.ylim(ymax=float(args.ymax)) if args.title is not None: plt.title(util.texsafe(args.title)) # Add some text for labels, title and axes ticks ax.set_xticks(ind * width + width / 2.) ax.set_xticklabels(labels, rotation=args.xticklabel_rotation) if args.ylabel is not None: ax.set_ylabel(args.ylabel) # Now make some labels value_labels(ax, ax.patches) if args.output is not None: fname = args.output.name fformat = fname.split('.')[-1] print("Writing file '%s' using format '%s'." % (fname, fformat)) if args.mpld3: import mpld3 if fformat == "html": mpld3.save_html(fig, args.output) elif fformat == "json": mpld3.save_json(fig, args.output) else: raise ValueError( "--mpld3: The output must be either `html` or `json`") else: pylab.savefig(fname, format=fformat) else: if args.mpld3: import mpld3 mpld3.show() else: pylab.show()
def makePlot(self, noTightLayout=False, figHeight='100%', figWidth='100%', centeredStyle=None): current_figure = plt.gcf() if self.usesMPLD3(): plugins.connect(current_figure, plugins.MousePosition(fontsize=14)) if self.outputType == PlotSaveTYPE.HTML_STRING: if centeredStyle == None: centeredStyle = self.centeredStyle outString = mpld3.fig_to_html(current_figure, template_type='notebook', d3_url=self.d3js, mpld3_url=self.mpld3js, figHeight=figHeight, figWidth=figWidth, styles=centeredStyle) self.createdPlots.append(outString) plt.close(current_figure) return if self.outputType == PlotSaveTYPE.D3: mpld3.show(current_figure) if self.save_to_file: exactFilename = self.save_file + ".%02d." + PlotSaveTYPE.getFileExtension( self.outputType) exactFilename = exactFilename % self.saved_plot self.saved_plot += 1 if self.outputType == PlotSaveTYPE.HTML: mpld3.save_html(current_figure, exactFilename, template_type='simple', d3_url=self.d3js, mpld3_url=self.mpld3js) elif self.outputType == PlotSaveTYPE.JSON: mpld3.save_json(current_figure, exactFilename, d3_url=self.d3js, mpld3_url=self.mpld3js) elif self.outputType == PlotSaveTYPE.PNG: plt.savefig(exactFilename, transparent=self.transparent_bg, bbox_inches='tight') self.createdPlots.append(exactFilename) else: # if self.outputType == PlotSaveTYPE.MPL if not noTightLayout: legends = current_figure.legends makeTightLayout = True for lgd in legends + [ ax.legend_ for ax in current_figure.axes ]: if lgd == None: continue lgd.set_draggable(True) makeTightLayout = makeTightLayout and lgd._bbox_to_anchor == None if makeTightLayout: plt.tight_layout() plt.show() plt.close(current_figure)
def line_per_cmd(args): import matplotlib if args.output is not None: matplotlib.use('Agg') # We need this to make matplotlib work on systems with no DISPLAY import matplotlib.pyplot as plt import pylab matplotlib.rcParams.update({'figure.subplot.right': args.plot_size}) matplotlib.rcParams.update({'figure.subplot.bottom': args.plot_size}) plt.style.use(args.pyplot_style) # First we create `means` which map a command label and date to a pair of mean and standard deviation # e.g. means['Bean']['2017-05-02 13:29:34.87'] = (0.1289, 0.0006) (means, cmd_labels, meta_keys) = util.means_series_map(args) # Let's write a plot line for each command label fig, ax = plt.subplots() lines = [] for cmd_label in cmd_labels: x, y, err = ([], [], []) for i in range(len(meta_keys)): (mean, std) = means[cmd_label].get(meta_keys[i], (0, 0)) x.append(i) y.append(mean) err.append(std) lines.append(ax.errorbar(x, y, fmt='-o', yerr=err)) if args.ymin is not None: plt.ylim(ymin=float(args.ymin)) if args.ymax is not None: plt.ylim(ymax=float(args.ymax)) if args.title is not None: plt.title(util.texsafe(args.title)) # Add some text for labels, title and axes ticks ax.set_xticks(range(len(meta_keys))) ax.set_xticklabels(meta_keys, rotation=args.xticklabel_rotation) if not args.no_legend and not args.mpld3: # mpld3 comes with its own legends ax.legend(lines, cmd_labels, loc='best', fancybox=True, shadow=True) if args.mpld3: import mpld3 from mpld3 import plugins interactive_legend = plugins.InteractiveLegendPlugin(lines, cmd_labels) plugins.connect(fig, interactive_legend) if args.output is not None: fname = args.output.name fformat = fname.split('.')[-1] print ("Writing file '%s' using format '%s'." % (fname, fformat)) if args.mpld3: if fformat == "html": mpld3.save_html(fig, args.output) elif fformat == "json": mpld3.save_json(fig, args.output) else: raise ValueError("--mpld3: The output must be either `html` or `json`") else: pylab.savefig(args.output, format=fformat) else: if args.mpld3: mpld3.show() else: pylab.show()
fg = folium.FeatureGroup('my map') min = 200 max = 300 m = folium.Map(location=[12.9752848, 77.5070119], zoom_start=10) # MON = list(data["Mon"]) # TUE = list(data["Tue"]) x=[' ', ''] y=[5,9] plt.xlabel('MON________________________________________________TUE ') plt.ylabel('Sales->') plt.title('Data') # plt.bar(x,y) plt.bar(x,y) plt.savefig("teettt.png") vis3= mpld3.save_json(plt.figure(), "teettttttt.html") n = "teettttttt.html" html_image = "<!DOCTYPE html><html><body><h2>HTML Image</h2><img src='C:/Users/admin/Desktop/teettt.jpg' alt=\"Girl in a jacket\" width=\"500\" height=\"600\"></body></html>" bar = "http://127.0.0.1:8898/" text_file = open("test_image.html", "w") n = text_file.write(html_image) text_file.close() cluster = folium.plugins.MarkerCluster().add_to(m)
def to_json(self): str_buffer = StringIO.StringIO() mpld3.save_json(self.content, str_buffer) return str_buffer.getvalue()