def test_get_figure_from_references(): url, g = test_plot_from_grid() fig = py.get_figure(url) data = fig['data'] trace = data[0] assert (g[0].data == trace['x']) assert (g[1].data == trace['y'])
def test_get_figure_from_references(): url, g = test_plot_from_grid() fig = py.get_figure(url) data = fig['data'] trace = data[0] assert(g[0].data == trace['x']) assert(g[1].data == trace['y'])
def test_get_figure_from_references(self): url, g = self.test_plot_from_grid() fig = py.get_figure(url) data = fig['data'] trace = data[0] assert(tuple(g[0].data) == tuple(trace['x'])) assert(tuple(g[1].data) == tuple(trace['y']))
def handler(event, context): plot_username, plot_number = re.search('plot\.ly\/~(\w+)\/(\d+)', event['plot_url']).groups() assert plot_username is not None assert plot_number is not None bot = SlackBot(slack_channel) py.sign_in(plotly_username, plotly_api_token) fig = py.get_figure(plot_username, plot_number) bot.upload_file(file=py.image.get(fig), filename='plotly-{}-{}.png'.format(plot_username, plot_number))
def add_data(names, scores): """ Updates our plot with lists of names and scores and returns a pair: a link to a PNG image with the scores plot and a list with tournament champions, if any. """ # Add element to our counter link = py.plot([{'x': 1, 'name': 'counter'}], filename=COUNTER_FILENAME, fileopt='append', auto_open=False) # Get in which day of the tournament we are day = len(py.get_figure(link).get_data()) # Compute special days: Monday and Friday of the second week title = 'Scores' fileopt = 'append' weekday = datetime.datetime.now().isoweekday() # Compute special outputs: # If second Friday since tournament started, close tournament # If Monday after tournament ended, reset everything if day > 6 and weekday == 1: # If Monday, overwrite previous plot and reset counter fileopt = 'overwrite' py.plot([{'x': 1, 'name': 'counter'}], filename=COUNTER_FILENAME, fileopt='overwrite', auto_open=False) day = 1 if day > 5 and weekday == 5: # If Friday, finish this competition title = 'Final scores!' # Update scores plot and get it's link link = add_stack(names, scores, day, title, fileopt) # Wait for plot to get updated time.sleep(1) # Compute champion it it's last day of tournament champions = [] if title == 'Final scores!': data = py.get_figure(link).get_data() champions = compute_champions(data) return '%s.png' % link, champions
def url_worker(section, command): username = section['url'].replace("https://plot.ly/~", "").split('/')[0] fid = section['url'].replace("https://plot.ly/~", "").split('/')[1] if 'private' in section and section['private']: time.sleep(1) # thread issues if we're singing in differently... match = [ usr for usr in users.values() if usr['un'].lower() == username.lower() ] if match: user = match[0] py.sign_in(user['un'], user['ak']) try: fig = py.get_figure(username, fid) except: # todo, too broad exception clause pass # print ("couldn't port url over for '{}'." # "".format(section['id'])) fig = None finally: py.sign_in(users[doc_user]['un'], users[doc_user]['ak']) if fig: if 'layout' not in fig: fig['layout'] = dict() if 'margin' not in fig['layout']: fig['layout']['margin'] = dict(t=90, b=65, r=50, l=65) if 'title' not in fig['layout']: fig['layout']['margin']['t'] = 65 fig['layout'].update(autosize=False, width=500, height=500) if 'private' in section and section['private']: try: # todo clean up exception handling new_url = py.plot(fig, filename=section['id'], auto_open=False, world_readable=False) except: new_url = None # print "\t\tcall to py.plot() failed" else: try: # todo clean up exception handling # sign in again here to reduce chance of collision... py.sign_in(users[doc_user]['un'], users[doc_user]['ak']) new_url = py.plot(fig, filename=section['id'], auto_open=False) except: new_url = None # print "\t\tcall to py.plot() failed" if command == 'test' and new_url: section['test-url'] = new_url print "\tnew url for ({}): '{}'".format(section['id'], new_url) elif command == 'publish' and new_url: section['publish-url'] = new_url print "\tnew url for ({}): '{}'".format(section['id'], new_url)
def url_worker(section, command): username = section['url'].replace("https://plot.ly/~", "").split('/')[0] fid = section['url'].replace("https://plot.ly/~", "").split('/')[1] if 'private' in section and section['private']: time.sleep(1) # thread issues if we're singing in differently... match = [usr for usr in users.values() if usr['un'].lower() == username.lower()] if match: user = match[0] py.sign_in(user['un'], user['ak']) try: fig = py.get_figure(username, fid) except: # todo, too broad exception clause pass # print ("couldn't port url over for '{}'." # "".format(section['id'])) fig = None finally: py.sign_in(users[doc_user]['un'], users[doc_user]['ak']) if fig: if 'layout' not in fig: fig['layout'] = dict() if 'margin' not in fig['layout']: fig['layout']['margin'] = dict(t=90, b=65, r=50, l=65) if 'title' not in fig['layout']: fig['layout']['margin']['t'] = 65 fig['layout'].update(autosize=False, width=500, height=500) if 'private' in section and section['private']: try: # todo clean up exception handling new_url = py.plot( fig, filename=section['id'], auto_open=False, world_readable=False) except: new_url = None # print "\t\tcall to py.plot() failed" else: try: # todo clean up exception handling # sign in again here to reduce chance of collision... py.sign_in(users[doc_user]['un'], users[doc_user]['ak']) new_url = py.plot(fig, filename=section['id'], auto_open=False) except: new_url = None # print "\t\tcall to py.plot() failed" if command == 'test' and new_url: section['test-url'] = new_url print "\tnew url for ({}): '{}'".format(section['id'], new_url) elif command == 'publish' and new_url: section['publish-url'] = new_url print "\tnew url for ({}): '{}'".format(section['id'], new_url)
def plot(self, contributor_email, cid): """make all plots for contribution_id""" plot_contrib = self.contrib_coll.find_one( {'contribution_id': cid}, { 'content.data': 1, 'content.plots': 1, '_id': 0, 'collaborators': 1, 'project': 1 } ) if 'data' not in plot_contrib['content']: return None author = Author.parse_author(contributor_email) project = str(author.name).translate(None, '.').replace(' ','_') \ if 'project' not in plot_contrib else plot_contrib['project'] subfld = 'contributed_data.%s.plotly_urls.%d' % (project, cid) data = plot_contrib['content']['data'] df = pd.DataFrame.from_dict(data) url_list = list(self.mat_coll.find( {subfld: {'$exists': True}}, {'_id': 0, subfld: 1} )) urls = [] if len(url_list) > 0: urls = url_list[0]['contributed_data'][project]['plotly_urls'][str(cid)] for nplot,plotopts in enumerate( plot_contrib['content']['plots'].itervalues() ): filename = 'test%d_%d' % (cid,nplot) fig, ax = plt.subplots(1, 1) df.plot(ax=ax, **plotopts) if len(urls) == len(plot_contrib['content']['plots']): pyfig = py.get_figure(urls[nplot]) for ti,line in enumerate(ax.get_lines()): pyfig['data'][ti]['x'] = list(line.get_xdata()) pyfig['data'][ti]['y'] = list(line.get_ydata()) py.plot(pyfig, filename=filename, auto_open=False) else: update = dict( layout=dict( annotations=[dict(text=' ')], showlegend=True, legend=Legend(x=1.05, y=1) ), ) urls.append(py.plot_mpl( fig, filename=filename, auto_open=False, strip_style=True, update=update, resize=True )) return None if len(url_list) > 0 else urls
def image_worker(section, command, folder_path, file_path): url = section["{}-url".format(command)] username = url.replace("https://plot.ly/~", "").split('/')[0] fid = url.replace("https://plot.ly/~", "").split('/')[1] try: fig = py.get_figure(username, fid) except plotly.exceptions.PlotlyError: print ("\tcouldn't get figure to save image for example, '{}'." "\n\tis the plot private?\n\t'{}'" "".format(section['id'], section['url'])) else: if not os.path.exists(folder_path): os.makedirs(folder_path) try: py.image.save_as(fig, file_path) except plotly.exceptions.PlotlyError: print "\t\timage save failed..." section['image'] = False else: section['image'] = True
def image_worker(section, command, folder_path, file_path): url = section["{}-url".format(command)] username = url.replace("https://plot.ly/~", "").split('/')[0] fid = url.replace("https://plot.ly/~", "").split('/')[1] try: fig = py.get_figure(username, fid) except plotly.exceptions.PlotlyError: print( "\tcouldn't get figure to save image for example, '{}'." "\n\tis the plot private?\n\t'{}'" "".format(section['id'], section['url'])) else: if not os.path.exists(folder_path): os.makedirs(folder_path) try: py.image.save_as(fig, file_path) except plotly.exceptions.PlotlyError: print "\t\timage save failed..." section['image'] = False else: section['image'] = True
def plot_memory(task_list, mapping): data = [] for task in task_list: trace = go.Bar(x=[mapping[task['name']]['name']], y=[task['memory']], name=task['name']) data.append(trace) if len(data) == 0: return False layout = go.Layout(barmode='stack') fig = go.Figure(data=data, layout=layout) url = py.plot(fig, filename='stacked-bar', world_readable=True, auto_open=False) print(url) url = url[-1:] print(url) fig = py.get_figure(username, url) py.image.save_as(fig, 'bar.png') return True
def plot_tasks( task_list ): #task_list is a list of dictionaries containing name,start and finish in relative seconds now_seconds = time_functions.get_current_sec() df = [] for task in task_list: task_name = task['name'] start_time = time_functions.get_real_time(now_seconds + task['start']) finish_time = time_functions.get_real_time(now_seconds + task['finish']) df.append(dict(Task=task_name, Start=start_time, Finish=finish_time)) print(df) fig = ff.create_gantt(df, group_tasks=True) url = py.plot(fig, filename='gantt-simple-gantt-chart', world_readable=True, auto_open=False) url = url[-1:] print(url) fig = py.get_figure(username, url) py.image.save_as(fig, 'simple.png')
import plotly.plotly as py py.sign_in('TestBot', 'r1neazxo9w') fig = py.get_figure("PlotBot", 5) plot_url = py.plot(fig, filename="python-replot2")
import plotly.plotly as py import matplotlib.pyplot as plt from plotly.graph_objs import * plt.figure(1) principal = 10000 interestRate = 0.05 years = 20 values = [] for i in range(years + 1): values.append(principal) principal += principal * interestRate plt.plot(range(years + 1), values) plt.title('5% Growth, Compounded Annually') plt.xlabel('Years of Compounding') plt.ylabel('Value of Principal ($)') py.plot_mpl(plt.figure(1)) # convert mpl object to plotly figure = py.get_figure('https://plot.ly/~Rafeh01/36/_5-growth-compounded-annually/') py.image.save_as(figure, 'mpl_to_plotly_image.png') # plt.show() # plot_url = py.plot(fig)
# ## Setting the <code>'textposition'</code> # # The <code>'textposition'</code> parameter determines whether the labels are drawn inside or outside the segments. It can take one of four values: # # - <code>'inside'</code> - all labels are inside the segments # - <code>'outside'</code> - all labels are outside the segments # - <code>'none'</code> - no labels are shown # - <code>'auto'</code> - labels are positioned inside and outside the segment based on their length and the space available inside the segment (we saw this in action in the previous lesson) # # Let's get the chart we made in the last lesson and practise moving the labels: # # In[4]: level = py.get_figure("rmuir", 269) pyo.iplot(level) py.image.save_as( level, r"C:\Users\Rytch\Google Drive\Financial\Passive Income\Online courses\Plotly\Course Content\Lessons\(08) Pie Charts\Notebooks\images\Pie charts (6) - Formatting text\pyo.iplot-0.png" ) # # Firstly let's set the <code>'textposition'</code> to <code>'none'</code> to remove the labels. You'll notice that it's probably a good idea to reinstate the legend. We're going to move on quickly, so there's not much point in doing this. # In[5]: level['data'][0].update({'textposition': 'none'}) pyo.iplot(level)
def get_graph(self, graph, out_file): figure = py.get_figure('shemer77', graph) py.image.save_as(figure, out_file)
# -*- coding: utf-8 -*- """ Created on Mon Apr 10 18:31:17 2017 @author: Brian """ import pandas as pd import plotly.plotly as py import plotly.graph_objs as go import numpy as np py.sign_in('w1057216', 'e6m3sexk0v') data = py.get_figure("https://plot.ly/~dfreder1/69/").get_data() fontDict = dict(family='Times New Roman', size=12, color='black') layout = go.Layout(title='Total Bridges Built in CA Since 1900', titlefont=fontDict, width=600, height=400, xaxis=dict(title='Year', titlefont=fontDict, showticklabels=True, tickfont=fontDict, ticks='outside', range=[1900, 2013], dtick=10, tickwidth=1), yaxis=dict(title='Total Bridges',
#pandas is a data analysis library import pandas as pd from pandas import DataFrame # In[3]: #lets us see the charts in an iPython Notebook pyo.offline.init_notebook_mode() # run at the start of every ipython # ## Getting a chart to modify: # # We'll import the MPs expense claims chart and set the tickvalues individually: # In[6]: expenses = py.get_figure('rmuir', 148) pyo.iplot(expenses) py.image.save_as( expenses, r"C:\Users\Rytch\Google Drive\Financial\Passive Income\Online courses\Plotly\Course Content\Lessons\(03) Chart Presentation 1\Notebooks\images\Chart Presentation (4) - Creating ticklabels\pyo.iplot-0.png" ) # # ## Setting the tickvalues # # In order to set the tickvalues, we need to manipulate three different parameters within the axis object. First of all, we need to set <code>'tickmode'</code> to <code>'array'</code> to tell Plotly to expect a list of ticks. # # Next, we need to set the <code>'tickvals'</code> to be a list of numbers where each tick will be displayed. # # Finally, we specify the <code>'ticktext'</code> to place at each of the <code>'tickvals'</code>:
import pandas as pd from pandas import DataFrame # In[49]: #lets us see the charts in an iPython Notebook pyo.offline.init_notebook_mode() # run at the start of every ipython # ## Getting a chart # We'll use the same chart that we made in the previous lesson: # In[50]: revEmp = py.get_figure("rmuir", 200) pyo.iplot(revEmp) py.image.save_as(revEmp, r"C:\Users\Rytch\Google Drive\Financial\Passive Income\Online courses\Plotly\Course Content\Lessons\(04) Scatterplots\Notebooks\images\Scatterplots (03) - Styling the marker points\pyo.iplot-0.png") # # ### Changing the marker symbols # Let's quickly review how to change the marker symbol on this scatterplot. Through the use of different marker symbols we can allow the reader to discern between two different categories on the same plot. These categories are often distinguished through the use of colour, but using different marker symbols accounts for those times when the colour is not sufficient (black and white printing, colour-blindness etc). # # You can find a list of the available symbols <a href="https://plot.ly/python/reference/#scatter-marker-symbol">here</a>. # # To change the marker symbol we need to change the <code>'symbol'</code> option within the <code>'marker'</code> dictionary. # ````python # trace = {'marker' : {'symbol' : <symbol string or numer>}} # ````
#pandas is a data analysis library import pandas as pd from pandas import DataFrame # In[3]: #lets us see the charts in an iPython Notebook pyo.offline.init_notebook_mode() # run at the start of every ipython # ## Getting a chart to modify: # # We'll use the stacked area chart showing total emissions for 5 countries to practise setting the tickprefix, ticksuffix and number of ticks: # In[46]: C02 = py.get_figure('rmuir', 156) pyo.iplot(C02) py.image.save_as( C02, r"C:\Users\Rytch\Google Drive\Financial\Passive Income\Online courses\Plotly\Course Content\Lessons\(03) Chart Presentation 1\Notebooks\images\Chart presentation (3) - Modifying tickvalues\pyo.iplot-0.png" ) # # ## Modifying the tick values # # We can change how the tick values are displayed by adding a tick suffix or prefix. # Both of these options are contained within in the x- and y-axis objects: # ````python # layout = {'xaxis' : {'ticksuffix' : <string>}} # ````
import plotly.plotly as py from plotly.graph_objs import * py.sign_in("-", "-") data = py.get_figure("https://plot.ly/~AlexHP/68").get_data() distance = [d['y'][0] for d in data] # check out the data for yourself! fig = Figure() fig['data'] += [Histogram(y=distance, name="flyby distance", histnorm='probability')] xaxis = XAxis(title="Probability for Flyby at this Distance") yaxis = YAxis(title="Distance from Earth (Earth Radii)") fig['layout'].update(title="data source: https://plot.ly/~AlexHP/68", xaxis=xaxis, yaxis=yaxis) plot_url = py.plot(fig, filename=">>>filename<<<")
""" Created on Thu Mar 1 14:50:56 2018 @author: caseytaylor """ import plotly import plotly.plotly as py import plotly.graph_objs as go import plotly.tools as tools import itertools # Embed Plotly plot in an IPython notebook: tools.embed('dfreder1', '69') dfreder1 = py.get_figure('dfreder1', '69') # print(dfreder1.to_string()) # get data object years = dfreder1.get_data()[0]['x'] unique_years = list(set(years)) # get number of bridges per year dictionary = {year:years.count(year) for year in unique_years} # delete pair u'(21658': 1 dictionary2 = {i:dictionary[i] for i in dictionary if i!=u'(21658'} unique_years2 = [i for i in unique_years if i!= u'(21658'] cumulative_bridges = []
def analyze(args): """analyze data at any point for a copy of the streaming figure""" # NOTE: make copy online first with suffix _%Y-%m-%d and note figure id fig = py.get_figure(creds['username'], args.fig_id) if args.t: if args.fig_id == 42: label_entries = filter( None, '<br>'.join(fig['data'][2]['text']).split('<br>')) pairs = map(make_tuple, label_entries) grps = set(chain.from_iterable(pairs)) snlgrp_cursor = sma.snlgroups.aggregate([{ '$match': { 'snlgroup_id': { '$in': list(grps) }, 'canonical_snl.about.projects': { '$ne': 'CederDahn Challenge' } } }, { '$project': { 'snlgroup_id': 1, 'canonical_snl.snlgroup_key': 1, '_id': 0 } }], cursor={}) snlgroup_keys = {} for d in snlgrp_cursor: snlgroup_keys[ d['snlgroup_id']] = d['canonical_snl']['snlgroup_key'] print snlgroup_keys[40890] sma2 = SNLMongoAdapter.from_file( os.path.join(os.environ['DB_LOC'], 'materials_db.yaml')) materials_cursor = sma2.database.materials.aggregate([{ '$match': { 'snlgroup_id_final': { '$in': list(grps) }, 'snl_final.about.projects': { '$ne': 'CederDahn Challenge' } } }, { '$project': { 'snlgroup_id_final': 1, '_id': 0, 'task_id': 1, 'final_energy_per_atom': 1, 'band_gap.search_gap.band_gap': 1, 'volume': 1, 'nsites': 1 } }], cursor={}) snlgroup_data = {} for material in materials_cursor: snlgroup_id = material['snlgroup_id_final'] final_energy_per_atom = material['final_energy_per_atom'] band_gap = material['band_gap']['search_gap']['band_gap'] volume_per_atom = material['volume'] / material['nsites'] snlgroup_data[snlgroup_id] = { 'final_energy_per_atom': final_energy_per_atom, 'band_gap': band_gap, 'task_id': material['task_id'], 'volume_per_atom': volume_per_atom } print snlgroup_data[40890] filestem = 'mpworks/check_snl/results/bad_snlgroups_2_' with open(filestem+'in_matdb.csv', 'wb') as f, \ open(filestem+'notin_matdb.csv', 'wb') as g: writer1, writer2 = csv.writer(f), csv.writer(g) header = [ 'category', 'composition', 'snlgroup_id 1', 'sg_num 1', 'task_id 1', 'snlgroup_id 2', 'sg_num 2', 'task_id 2', 'delta_energy', 'delta_bandgap', 'delta_volume_per_atom', 'rms_dist', 'scenario' ] writer1.writerow(header) writer2.writerow(header) for primary_id, secondary_id in pairs: if primary_id not in snlgroup_keys or \ secondary_id not in snlgroup_keys: continue composition, primary_sg_num = snlgroup_keys[ primary_id].split('--') secondary_sg_num = snlgroup_keys[secondary_id].split( '--')[1] category = 'same SGs' if primary_sg_num == secondary_sg_num else 'diff. SGs' if primary_id not in snlgroup_data or secondary_id not in snlgroup_data: delta_energy, delta_bandgap, delta_volume_per_atom = '', '', '' else: delta_energy = "{0:.3g}".format(abs( snlgroup_data[primary_id]['final_energy_per_atom'] - \ snlgroup_data[secondary_id]['final_energy_per_atom'] )) delta_bandgap = "{0:.3g}".format(abs( snlgroup_data[primary_id]['band_gap'] - \ snlgroup_data[secondary_id]['band_gap'] )) delta_volume_per_atom = "{0:.3g}".format(abs( snlgroup_data[primary_id]['volume_per_atom'] - \ snlgroup_data[secondary_id]['volume_per_atom'] )) scenario, rms_dist_str = '', '' if category == 'diff. SGs' and delta_energy and delta_bandgap: scenario = 'different' if ( float(delta_energy) > 0.01 or float(delta_bandgap) > 0.1) else 'similar' snlgrp1_dict = sma.snlgroups.find_one( {"snlgroup_id": primary_id}) snlgrp2_dict = sma.snlgroups.find_one( {"snlgroup_id": secondary_id}) snlgrp1 = SNLGroup.from_dict(snlgrp1_dict) snlgrp2 = SNLGroup.from_dict(snlgrp2_dict) primary_structure = snlgrp1.canonical_structure secondary_structure = snlgrp2.canonical_structure rms_dist = matcher.get_rms_dist( primary_structure, secondary_structure) if rms_dist is not None: rms_dist_str = "({0:.3g},{1:.3g})".format( *rms_dist) print rms_dist_str row = [ category, composition, primary_id, primary_sg_num, snlgroup_data[primary_id]['task_id'] \ if primary_id in snlgroup_data else '', secondary_id, secondary_sg_num, snlgroup_data[secondary_id]['task_id'] \ if secondary_id in snlgroup_data else '', delta_energy, delta_bandgap, delta_volume_per_atom, rms_dist_str, scenario ] if delta_energy and delta_bandgap: writer1.writerow(row) else: writer2.writerow(row) elif args.fig_id == 16: out_fig = Figure() badsnls_trace = Scatter(x=[], y=[], text=[], mode='markers', name='SG Changes') bisectrix = Scatter(x=[0, 230], y=[0, 230], mode='lines', name='bisectrix') print 'pulling bad snls from plotly ...' bad_snls = OrderedDict() for category, text in zip(fig['data'][2]['y'], fig['data'][2]['text']): for snl_id in map(int, text.split('<br>')): bad_snls[snl_id] = category with open('mpworks/check_snl/results/bad_snls.csv', 'wb') as f: print 'pulling bad snls from database ...' mpsnl_cursor = sma.snl.find({ 'snl_id': { '$in': bad_snls.keys() }, 'about.projects': { '$ne': 'CederDahn Challenge' } }) writer = csv.writer(f) writer.writerow([ 'snl_id', 'category', 'snlgroup_key', 'nsites', 'remarks', 'projects', 'authors' ]) print 'writing bad snls to file ...' for mpsnl_dict in mpsnl_cursor: mpsnl = MPStructureNL.from_dict(mpsnl_dict) row = [ mpsnl.snl_id, bad_snls[mpsnl.snl_id], mpsnl.snlgroup_key ] row += _get_snl_extra_info(mpsnl) writer.writerow(row) sg_num = mpsnl.snlgroup_key.split('--')[1] if (bad_snls[mpsnl.snl_id] == 'SG default' and sg_num != '-1') or \ bad_snls[mpsnl.snl_id] == 'SG change': mpsnl.structure.remove_oxidation_states() sf = SpacegroupAnalyzer(mpsnl.structure, symprec=0.1) badsnls_trace['x'].append(mpsnl.sg_num) badsnls_trace['y'].append(sf.get_spacegroup_number()) badsnls_trace['text'].append(mpsnl.snl_id) if bad_snls[mpsnl.snl_id] == 'SG default': print sg_num, sf.get_spacegroup_number() print 'plotting out-fig ...' out_fig['data'] = Data([bisectrix, badsnls_trace]) out_fig['layout'] = Layout( showlegend=False, hovermode='closest', title='Spacegroup Assignment Changes', xaxis=XAxis(showgrid=False, title='old SG number', range=[0, 230]), yaxis=YAxis(showgrid=False, title='new SG number', range=[0, 230]), ) filename = 'spacegroup_changes_' filename += datetime.datetime.now().strftime('%Y-%m-%d') py.plot(out_fig, filename=filename, auto_open=False) elif args.fig_id == 43: # SNLGroupMemberChecker matcher2 = StructureMatcher(ltol=0.2, stol=0.3, angle_tol=5, primitive_cell=False, scale=True, attempt_supercell=True, comparator=ElementComparator()) print 'pulling data from plotly ...' trace = Scatter(x=[], y=[], text=[], mode='markers', name='mismatches') bad_snls = OrderedDict() # snlgroup_id : [ mismatching snl_ids ] for category, text in zip(fig['data'][2]['y'], fig['data'][2]['text']): if category != 'mismatch': continue for entry in text.split('<br>'): fields = entry.split(':') snlgroup_id = int(fields[0].split(',')[0]) print snlgroup_id snlgrp_dict = sma.snlgroups.find_one( {'snlgroup_id': snlgroup_id}) snlgrp = SNLGroup.from_dict(snlgrp_dict) s1 = snlgrp.canonical_structure.get_primitive_structure() bad_snls[snlgroup_id] = [] for i, snl_id in enumerate(fields[1].split(',')): mpsnl_dict = sma.snl.find_one({'snl_id': int(snl_id)}) if 'CederDahn Challenge' in mpsnl_dict['about'][ 'projects']: print 'skip CederDahn: %s' % snl_id continue mpsnl = MPStructureNL.from_dict(mpsnl_dict) s2 = mpsnl.structure.get_primitive_structure() is_match = matcher2.fit(s1, s2) if is_match: continue bad_snls[snlgroup_id].append(snl_id) trace['x'].append(snlgroup_id) trace['y'].append(i + 1) trace['text'].append(snl_id) if len(bad_snls[snlgroup_id]) < 1: bad_snls.pop(snlgroup_id, None) with open('mpworks/check_snl/results/bad_snlgroups.csv', 'wb') as f: print 'pulling bad snlgroups from database ...' snlgroup_cursor = sma.snlgroups.find({ 'snlgroup_id': { '$in': bad_snls.keys() }, }) writer = csv.writer(f) writer.writerow( ['snlgroup_id', 'snlgroup_key', 'mismatching snl_ids']) print 'writing bad snlgroups to file ...' for snlgroup_dict in snlgroup_cursor: snlgroup = SNLGroup.from_dict(snlgroup_dict) row = [ snlgroup.snlgroup_id, snlgroup.canonical_snl.snlgroup_key, ' '.join(bad_snls[snlgroup.snlgroup_id]) ] writer.writerow(row) print 'plotting out-fig ...' out_fig = Figure() out_fig['data'] = Data([trace]) out_fig['layout'] = Layout( showlegend=False, hovermode='closest', title='Member Mismatches of SNLGroup Canonicals', xaxis=XAxis(showgrid=False, title='snlgroup_id', showexponent='none'), yaxis=YAxis(showgrid=False, title='# mismatching SNLs'), ) filename = 'groupmember_mismatches_' filename += datetime.datetime.now().strftime('%Y-%m-%d') py.plot(out_fig, filename=filename, auto_open=False) else: errors = Counter() bad_snls = OrderedDict() bad_snlgroups = OrderedDict() for i, d in enumerate(fig['data']): if not isinstance(d, Scatter): continue if not 'x' in d or not 'y' in d or not 'text' in d: continue start_id = int(d['name'].split(' - ')[0][:-1]) * 1000 marker_colors = d['marker']['color'] if i < 2 * num_snl_streams: # spacegroups errors += Counter(marker_colors) for idx, color in enumerate(marker_colors): snl_id = start_id + d['x'][idx] color_index = category_colors.index(color) category = categories[color_index] bad_snls[snl_id] = category else: # groupmembers for idx, color in enumerate(marker_colors): if color != category_colors[0]: continue snlgroup_id = start_id + d['x'][idx] mismatch_snl_id, canonical_snl_id = d['text'][idx].split( ' != ') bad_snlgroups[snlgroup_id] = int(mismatch_snl_id) print errors fig_data = fig['data'][-1] fig_data['x'] = [ errors[color] for color in fig_data['marker']['color'] ] filename = _get_filename() print filename #py.plot(fig, filename=filename) with open('mpworks/check_snl/results/bad_snls.csv', 'wb') as f: mpsnl_cursor = sma.snl.find({'snl_id': {'$in': bad_snls.keys()}}) writer = csv.writer(f) writer.writerow([ 'snl_id', 'category', 'snlgroup_key', 'nsites', 'remarks', 'projects', 'authors' ]) for mpsnl_dict in mpsnl_cursor: mpsnl = MPStructureNL.from_dict(mpsnl_dict) row = [ mpsnl.snl_id, bad_snls[mpsnl.snl_id], mpsnl.snlgroup_key ] row += _get_snl_extra_info(mpsnl) writer.writerow(row) with open('mpworks/check_snl/results/bad_snlgroups.csv', 'wb') as f: snlgrp_cursor = sma.snlgroups.find( {'snlgroup_id': { '$in': bad_snlgroups.keys() }}) first_mismatch_snls_cursor = sma.snl.find( {'snl_id': { '$in': bad_snlgroups.values() }}) first_mismatch_snl_info = OrderedDict() for mpsnl_dict in first_mismatch_snls_cursor: mpsnl = MPStructureNL.from_dict(mpsnl_dict) first_mismatch_snl_info[mpsnl.snl_id] = _get_snl_extra_info( mpsnl) writer = csv.writer(f) writer.writerow([ 'snlgroup_id', 'snlgroup_key', 'canonical_snl_id', 'first_mismatching_snl_id', 'nsites', 'remarks', 'projects', 'authors' ]) for snlgrp_dict in snlgrp_cursor: snlgrp = SNLGroup.from_dict(snlgrp_dict) first_mismatch_snl_id = bad_snlgroups[snlgrp.snlgroup_id] row = [ snlgrp.snlgroup_id, snlgrp.canonical_snl.snlgroup_key, snlgrp.canonical_snl.snl_id, first_mismatch_snl_id ] row += [ ' & '.join(pair) if pair[0] != pair[1] else pair[0] for pair in zip( _get_snl_extra_info(snlgrp.canonical_snl), first_mismatch_snl_info[int(first_mismatch_snl_id)]) ] writer.writerow(row)
from collections import OrderedDict import dash import dash_core_components as dcc import dash_html_components as html import plotly.plotly as py graphs = OrderedDict([ ['scatter', { 'figure': py.get_figure(), 'label': 'Scatter' ], ['bar', { 'figure': py.get_figure(), 'label': 'Bar' }], ['surface': { 'figure': py.get_figure(), 'label': 'Surface' }], ['multiple-axes': { 'figure': 'label': }], ['error-bars': { 'figure': py.get_figure('https://plot.ly/~chriddyp/674'), 'label': 'Error Bars' }] ]) html.Div([
import plotly import plotly.plotly as py py.sign_in('cheecheelin','br1lrimfku') import plotly.tools as tls from plotly.graph_objs import Data, Layout, Figure from plotly.graph_objs import Scatter from plotly.graph_objs import XAxis, YAxis from plotly.graph_objs import Line, Marker from plotly.graph_objs import Histogram from plotly.graph_objs import XBins, Annotations, Font rawdata = py.get_figure('dfreder1','69') # print(importthis.to_string()) datalist= rawdata.get_data() post1900=[i for i in datalist[0]['x'] if i>=1900 and i<=2010] mydata= map(str, post1900) d= {x:mydata.count(x) for x in mydata} #sorting by date a=[] b=[] for k in sorted(d): a.append(k) b.append(d[k]) for i in range(len(b)): if i!=0: b[i]+=b[i-1] else: b[i]=b[i] # print a,b
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Wed Nov 15 13:06:40 2017 @author: keianarei """ import plotly.plotly as py import plotly.graph_objs as go from plotly.plotly import plot from plotly.graph_objs import Scatter,Data,Layout,Figure my_fig = py.get_figure("dfreder1",69) data = my_fig.get_data() raw_data = data[0] manip_data = {} for year in raw_data['x']: if year in manip_data: manip_data[year] += 1 else: manip_data[year] = 1 manip_data[1858]=manip_data.pop('(21658') fin_data = [] for year in manip_data: if year == 1900: fin_data.append(manip_data[year]) x = fin_data.index(manip_data[year])
idata = Data([ bar ]) layout = Layout( title="{} Length Distribution".format(proper_name if basename != "ALL" else "Overall"), xaxis=XAxis(title="Word length (characters)"), yaxis=YAxis(title="Frequency (number of words)") ) fig = Figure(data=idata, layout=layout) if basename != "ALL": type_map[basename] = bar print("generating plot: {}".format(basename)) plot_url_map[basename] = py.plot(fig, filename='{}-distribution'.format(basename), auto_open=False) fig = py.get_figure(plot_url_map[basename]) py.image.save_as(fig, os.path.join(PLOTS_PATH, "{}.png".format(basename))) layout = Layout( title="Combined Length Distributions (all types)", xaxis=XAxis(title="Word length (characters)"), yaxis=YAxis(title="Frequency (number of words)"), barmode='stacked' ) fig = Figure(data=type_map.values(), layout=layout) print("generating stacked plot") stacked_url = py.plot(fig, filename='combined_word_distributions', auto_open=False) with open(URL_FILE, "w") as f:
def plotFalseMatchvsM(): # Plot probability of a false match as a function of M # Plot three different values of n. n=1000, 10000, 20000? # listofMValues,errorsN1000 = falseMatchvsM(1000) # listofMValues,errorsN10000 = falseMatchvsM(10000) # listofMValues,errorsN20000 = falseMatchvsM(20000) # I get a JSON serialization error if I try to use the above values directly # Go figure! listofMValues = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] errorsN1000 = [1.00140315240769e-5, 0.0473894530292855, 0.501650232353378, 0.899154755459308, 0.991079536716752, 0.999523524650922, 0.999981553264809, 0.999999512691584, 0.999999990860972, 0.999999999845828, 0.999999999997526, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] errorsN10000 = [5.30352419025194e-20, 2.35602184920806e-14, 1.53934243906984e-11, 1.10302867119927e-9, 2.27921915526762e-8, 2.69253218125502e-7, 1.98072351729197e-6, 9.75307801058064e-6, 3.98373263650013e-5, 0.000127080441383853, 0.000362630253250421, 0.000874610762751977, 0.00190208641223641, 0.00378812896412743, 0.00716447200395456, 0.0123542384410630, 0.0201165770143612, 0.0311403472140338, 0.0453789479134073] errorsN20000 = [1.75826328738259e-24, 9.73603540551588e-19, 7.92130752699486e-16, 7.08428692442973e-14, 2.03825914561597e-12, 2.93568736488724e-11, 2.44471331836285e-10, 1.59493239439383e-9, 8.04780894887436e-9, 3.32242567600452e-8, 1.11328056326208e-7, 3.44426535137730e-7, 9.53448550682057e-7, 2.31864104977855e-6, 5.40383164541756e-6, 1.13919680675529e-5, 2.33141516738363e-5, 4.40336400781654e-5, 8.14089056445047e-5] plotlyUser = os.environ['PLOTLY_USER_NAME'] plotlyAPIKey = os.environ['PLOTLY_API_KEY'] py.sign_in(plotlyUser, plotlyAPIKey) trace1 = Scatter( y=errorsN1000, x=listofMValues, line=Line( color='rgb(0, 0, 0)', width=3, shape='spline' ), name="n=1000" ) trace2 = Scatter( y=errorsN10000, x=listofMValues, line=Line( color='rgb(0, 0, 0)', width=3, shape='spline' ), name="n=10000" ) trace3 = Scatter( y=errorsN20000, x=listofMValues, line=Line( color='rgb(0, 0, 0)', width=3, shape='spline' ), name="n=20000" ) data = Data([trace1, trace2, trace3]) layout = Layout( title='', showlegend=False, autosize=False, width=855, height=700, xaxis=XAxis( title='$\\text{Number of combined patterns } (M)$', titlefont=Font( family='', size=16, color='' ), tickfont=Font( family='', size=16, color='' ), exponentformat="none", dtick=2, showline=True, range=[0,20], ), yaxis=YAxis( title='Probability of false positives', type='log', exponentformat='power', autorange=True, titlefont=Font( family='', size=18, color='' ), tickfont=Font( family='', size=12, color='' ), showline=True, ), annotations=Annotations([ Annotation( x=7.7906, y=0.916, xref='x', yref='paper', text='$n = 1000$', showarrow=False, font=Font( family='', size=16, color='' ), align='center', textangle=0, bordercolor='', borderwidth=1, borderpad=1, bgcolor='rgba(0, 0, 0, 0)', opacity=1 ), Annotation( x=7.906, y=0.7148, xref='x', yref='paper', text='$n = 10000$', showarrow=False, font=Font( family='', size=16, color='' ), align='center', textangle=0, bordercolor='', borderwidth=1, borderpad=1, bgcolor='rgba(0, 0, 0, 0)', opacity=1 ), Annotation( x=7.906, y=0.534, xref='x', yref='paper', text='$n = 20000$', showarrow=False, font=Font( family='', size=16, color='' ), align='center', textangle=0, bordercolor='', borderwidth=1, borderpad=1, bgcolor='rgba(0, 0, 0, 0)', opacity=1 ), ]), ) fig = Figure(data=data, layout=layout) plot_url = py.plot(fig) print "url=",plot_url figure = py.get_figure(plot_url) py.image.save_as(figure, 'union_effect_of_n.png', scale=4)
import plotly.plotly as py from plotly.graph_objs import * py.sign_in('TestBot', 'r1neazxo9w') data = py.get_figure("https://plot.ly/~AlexHP/68").get_data() distance = [d['y'][0] for d in data] # check out the data for yourself! fig = Figure() fig['data'] += [ Histogram(y=distance, name="flyby distance", histnorm='probability') ] xaxis = XAxis(title="Probability for Flyby at this Distance") yaxis = YAxis(title="Distance from Earth (Earth Radii)") fig['layout'].update(title="data source: https://plot.ly/~AlexHP/68", xaxis=xaxis, yaxis=yaxis) plot_url = py.plot(fig, filename="python-get-data")
# We can add `py$ggplotly` to the call, which will draw the figure with Plotly's [R API](https://plot.ly/r). Then we can call it in a Notebook. You can similarly call any Plotly graph with the username and graph id pair. # In[9]: tls.embed('RgraphingAPI', '1457') # Note: the data is called from a WDI database; if you make it with Plotly, the data is stored with the plot. I forked the data and shared it: [https://plot.ly/~MattSundquist/1343](https://plot.ly/~MattSundquist/1343). # If you want to use Plotly's default graph look, you can edit the graph with Python. # In[10]: fig = py.get_figure('RgraphingAPI', '1457') fig.strip_style() py.iplot(fig) # Often we come to a visualization with data rather than coming to data with a visualization. In that case, Plotly is useful for quick exploration, with matplotlib or Plotly's API. # In[11]: my_data = py.get_figure('PythonAPI', '455').get_data() # In[12]: get_ipython().magic(u'matplotlib inline') import matplotlib.pyplot as plt
from pandas import DataFrame # In[2]: #lets us see the charts in an iPython Notebook pyo.offline.init_notebook_mode() # run at the start of every ipython # ## Options for changing the colour. # # There are two colours we can change; the <code>'paper_bgcolor'</code> and the <code>'plot_bgcolor'</code>. # # <code>'paper_bgcolor'</code> changes the colour of the area around the plotting area, whilst <code>'plot_bgcolor'</code> changes the colour of the background of the plotting area. Just to keep things interesting, let's test this on a different chart to the one that we've been using. # In[4]: lifeExp = py.get_figure('rmuir', 225) pyo.iplot(lifeExp) py.image.save_as( lifeExp, r"C:\Users\Rytch\Google Drive\Financial\Passive Income\Online courses\Plotly\Course Content\Lessons\(07) Chart Presentation 3\Notebooks\images\Chart presentation (5) - Changing the colour of the plotting area\pyo.iplot-0.png" ) # # Let's change the <code>'paper_bgcolor'</code> first. I'm going to change it to an off-white colour: # In[5]: lifeExp['layout'].update({'paper_bgcolor': '#faebd7'}) pyo.iplot(lifeExp)
def test04(): import plotly.plotly as py fig = py.get_figure('https://plot.ly/~jackp/8715', raw=True) plot(fig)
), Annotation( x=23.67, y=0.90, xref='x', yref='paper', text='$a = 4000, n=16000$', showarrow=True, ax = 0, ay=-30, font=Font( family='', size=16, color='' ), align='left', textangle=0, bordercolor='', borderwidth=1, borderpad=1, bgcolor='rgba(0, 0, 0, 0)', opacity=1 ), ]), ) fig = Figure(data=data, layout=layout) plot_url = py.plot(fig) print "url=",plot_url figure = py.get_figure(plot_url) py.image.save_as(figure, 'images/effect_of_s.png', scale=4)
import dash import dash_core_components as dcc import dash_html_components as html import plotly.plotly as py from collections import OrderedDict graphs = OrderedDict([ ['scatter', { 'figure': py.get_figure(), 'label': 'Scatter' ], ['bar', { 'figure': py.get_figure(), 'label': 'Bar' }], ['surface': { 'figure': py.get_figure(), 'label': 'Surface' }], ['multiple-axes': { 'figure': 'label': }], ['error-bars': { 'figure': py.get_figure('https://plot.ly/~chriddyp/674'), 'label': 'Error Bars' }] ])
from pandas import DataFrame # In[2]: #lets us see the charts in an iPython Notebook pyo.offline.init_notebook_mode() # run at the start of every ipython # ## Getting the charts # We're going to add source annotations to two charts that we've previously produced; the Gapminder plot, and the plot which shows Life Expectancy against cigarette prices. # In[3]: gapMinder = py.get_figure("rmuir", 225) lifeExp = py.get_figure("rmuir", 223) # ### Setting the source for the Gapminder plot # # Let's add the source as an annotation to the Gapminder plot, remembering to set the <code>'xref'</code> and <code>'yref'</code> to <code>'paper'</code> to allow us to position this annotation outside of the plotting area. # # I'm going to position this annotation at the bottom-right of the chart, in italics, and in a small, light grey font. # In[4]: gapMinder['layout'].update({'annotations' : [{'text' : "<i>Source: https://www.gapminder.org/data/</i>", 'xref' : 'paper', 'yref' : 'paper', 'x' : 0,
from pandas import DataFrame # In[2]: #lets us see the charts in an iPython Notebook pyo.offline.init_notebook_mode() # run at the start of every ipython # ## Getting the chart # # We'll load the chart that we made last lesson: # In[3]: ethPie = py.get_figure("rmuir", 263) pyo.iplot(ethPie) py.image.save_as(ethPie, r"C:\Users\Rytch\Google Drive\Financial\Passive Income\Online courses\Plotly\Course Content\Lessons\(08) Pie Charts\Notebooks\images\Pie charts (3) - Styling the pie chart\pyo.iplot-0.png") # # We can change the colour of the individual segments by passing a list of colours into the trace: # In[5]: ethPie['data'][0].update({'marker' : {'colors' : ["rgb(12,192,170)", "rgb(190,252,250)", "rgb(77,194,84)", "rgb(211,238,128)", "rgb(97,167,193)"]}})
def plotSynapsesvsM(): # Plot number of synapses on a segment as a function of M listofMValues = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] synapsesS20 = [20.0000000000000, 39.6000000000000, 58.8080000000000, 77.6318400000000, 96.0792032000000, 114.157619136000, 131.874466753280, 149.236977418214, 166.252237869850, 182.927193112453, 199.268649250204, 215.283276265200, 230.977610739896, 246.358058525098, 261.430897354596, 276.202279407504, 290.678233819354, 304.864669142967, 318.767375760108] synapsesS25 = [25.0000000000000, 49.3750000000000, 73.1406250000000, 96.3121093750000, 118.904306640625, 140.931698974609, 162.408406500244, 183.348196337738, 203.764491429295, 223.670379143562, 243.078619664973, 262.001654173349, 280.451612819015, 298.440322498540, 315.979314436076, 333.079831575174, 349.752835785795, 366.009014891150, 381.858789518871] synapsesS30 = [30.0000000000000, 59.1000000000000, 87.3270000000000, 114.707190000000, 141.265974300000, 167.027995071000, 192.017155218870, 216.256640562304, 239.768941345435, 262.575873105072, 284.698596911920, 306.157639004562, 326.972909834425, 347.163722539392, 366.748810863211, 385.746346537314, 404.173956141195, 422.048737456959, 439.387275333250] plotlyUser = os.environ['PLOTLY_USER_NAME'] plotlyAPIKey = os.environ['PLOTLY_API_KEY'] py.sign_in(plotlyUser, plotlyAPIKey) trace1 = Scatter( y=synapsesS20, x=listofMValues, line=Line( color='rgb(0, 0, 0)', width=3, shape='spline' ), name="s=20" ) trace2 = Scatter( y=synapsesS25, x=listofMValues, line=Line( color='rgb(0, 0, 0)', width=3, shape='spline' ), name="s=25" ) trace3 = Scatter( y=synapsesS30, x=listofMValues, line=Line( color='rgb(0, 0, 0)', width=3, shape='spline' ), name="s=30" ) data = Data([trace1, trace2, trace3]) layout = Layout( title='', showlegend=False, autosize=False, width=855, height=700, xaxis=XAxis( title='$\\text{Number of combined patterns } (M)$', titlefont=Font( family='', size=16, color='' ), tickfont=Font( family='', size=16, color='' ), exponentformat="none", dtick=2, range=[0,20], ), yaxis=YAxis( title='Number of synapses on segment', exponentformat='none', autorange=True, titlefont=Font( family='', size=18, color='' ), tickfont=Font( family='', size=12, color='' ), ), annotations=Annotations([ Annotation( x=18.0, y=0.616, xref='x', yref='paper', text='$s = 20$', showarrow=False, font=Font( family='', size=16, color='' ), align='center', textangle=0, bordercolor='', borderwidth=1, borderpad=1, bgcolor='rgba(0, 0, 0, 0)', opacity=1 ), Annotation( x=18.0, y=0.7552, xref='x', yref='paper', text='$s = 25$', showarrow=False, font=Font( family='', size=16, color='' ), align='center', textangle=0, bordercolor='', borderwidth=1, borderpad=1, bgcolor='rgba(0, 0, 0, 0)', opacity=1 ), Annotation( x=18.0, y=0.876, xref='x', yref='paper', text='$s = 30$', showarrow=False, font=Font( family='', size=16, color='' ), align='center', textangle=0, bordercolor='', borderwidth=1, borderpad=1, bgcolor='rgba(0, 0, 0, 0)', opacity=1 ), ]), ) fig = Figure(data=data, layout=layout) plot_url = py.plot(fig) print "url=",plot_url figure = py.get_figure(plot_url) py.image.save_as(figure, 'union_number_synapses.png', scale=4)
# coding: utf-8 # # Barcharts (1) - What is a barchart? # # In this section we're going to learn how to create barcharts with Plotly. A barchart is used to display categorical data, with the size of the bar representing the quantity (or sometimes proportion) in that particular category. Barcharts make it easy to compare different categories because we can easily assess which bars are longer than others. # # Barcharts can be horizontal or vertical. The example below from Plotly user <a href="https://plot.ly/~elliotk">elliotk</a> shows the reasons people reject an employment offer: # In[7]: import plotly.plotly as py import plotly.offline as pyo pyo.init_notebook_mode() pyo.iplot(py.get_figure("elliotk", 21)) py.image.save_as( py.get_figure("elliotk", 21), r"C:\Users\Rytch\Google Drive\Financial\Passive Income\Online courses\Plotly\Course Content\Lessons\(06) Barcharts\Notebooks\images\Barcharts (01) - What is a barchart\pyo.iplot-0.png" ) # # # ### Stacked bar charts # # We can also use bar charts to show more complex categorical data, by stacking the bars, as in this example from the Higher Education Funding Council for England which shows the proportion of staff by gender and job type: # In[6]: pyo.iplot(py.get_figure("hefceplots", 33)) py.image.save_as(
def analyze(args): """analyze data at any point for a copy of the streaming figure""" # NOTE: make copy online first with suffix _%Y-%m-%d and note figure id fig = py.get_figure(creds['username'], args.fig_id) if args.t: if args.fig_id == 42: label_entries = filter(None, '<br>'.join(fig['data'][2]['text']).split('<br>')) pairs = map(make_tuple, label_entries) grps = set(chain.from_iterable(pairs)) snlgrp_cursor = sma.snlgroups.aggregate([ { '$match': { 'snlgroup_id': { '$in': list(grps) }, 'canonical_snl.about.projects': {'$ne': 'CederDahn Challenge'} } }, { '$project': { 'snlgroup_id': 1, 'canonical_snl.snlgroup_key': 1, '_id': 0 } } ], cursor={}) snlgroup_keys = {} for d in snlgrp_cursor: snlgroup_keys[d['snlgroup_id']] = d['canonical_snl']['snlgroup_key'] print snlgroup_keys[40890] sma2 = SNLMongoAdapter.from_file( os.path.join(os.environ['DB_LOC'], 'materials_db.yaml') ) materials_cursor = sma2.database.materials.aggregate([ { '$match': { 'snlgroup_id_final': { '$in': list(grps) }, 'snl_final.about.projects': {'$ne': 'CederDahn Challenge'} } }, { '$project': { 'snlgroup_id_final': 1, '_id': 0, 'task_id': 1, 'final_energy_per_atom': 1, 'band_gap.search_gap.band_gap': 1, 'volume': 1, 'nsites': 1 }} ], cursor={}) snlgroup_data = {} for material in materials_cursor: snlgroup_id = material['snlgroup_id_final'] final_energy_per_atom = material['final_energy_per_atom'] band_gap = material['band_gap']['search_gap']['band_gap'] volume_per_atom = material['volume'] / material['nsites'] snlgroup_data[snlgroup_id] = { 'final_energy_per_atom': final_energy_per_atom, 'band_gap': band_gap, 'task_id': material['task_id'], 'volume_per_atom': volume_per_atom } print snlgroup_data[40890] filestem = 'mpworks/check_snl/results/bad_snlgroups_2_' with open(filestem+'in_matdb.csv', 'wb') as f, \ open(filestem+'notin_matdb.csv', 'wb') as g: writer1, writer2 = csv.writer(f), csv.writer(g) header = [ 'category', 'composition', 'snlgroup_id 1', 'sg_num 1', 'task_id 1', 'snlgroup_id 2', 'sg_num 2', 'task_id 2', 'delta_energy', 'delta_bandgap', 'delta_volume_per_atom', 'rms_dist', 'scenario' ] writer1.writerow(header) writer2.writerow(header) for primary_id, secondary_id in pairs: if primary_id not in snlgroup_keys or \ secondary_id not in snlgroup_keys: continue composition, primary_sg_num = snlgroup_keys[primary_id].split('--') secondary_sg_num = snlgroup_keys[secondary_id].split('--')[1] category = 'same SGs' if primary_sg_num == secondary_sg_num else 'diff. SGs' if primary_id not in snlgroup_data or secondary_id not in snlgroup_data: delta_energy, delta_bandgap, delta_volume_per_atom = '', '', '' else: delta_energy = "{0:.3g}".format(abs( snlgroup_data[primary_id]['final_energy_per_atom'] - \ snlgroup_data[secondary_id]['final_energy_per_atom'] )) delta_bandgap = "{0:.3g}".format(abs( snlgroup_data[primary_id]['band_gap'] - \ snlgroup_data[secondary_id]['band_gap'] )) delta_volume_per_atom = "{0:.3g}".format(abs( snlgroup_data[primary_id]['volume_per_atom'] - \ snlgroup_data[secondary_id]['volume_per_atom'] )) scenario, rms_dist_str = '', '' if category == 'diff. SGs' and delta_energy and delta_bandgap: scenario = 'different' if ( float(delta_energy) > 0.01 or float(delta_bandgap) > 0.1 ) else 'similar' snlgrp1_dict = sma.snlgroups.find_one({ "snlgroup_id": primary_id }) snlgrp2_dict = sma.snlgroups.find_one({ "snlgroup_id": secondary_id }) snlgrp1 = SNLGroup.from_dict(snlgrp1_dict) snlgrp2 = SNLGroup.from_dict(snlgrp2_dict) primary_structure = snlgrp1.canonical_structure secondary_structure = snlgrp2.canonical_structure rms_dist = matcher.get_rms_dist(primary_structure, secondary_structure) if rms_dist is not None: rms_dist_str = "({0:.3g},{1:.3g})".format(*rms_dist) print rms_dist_str row = [ category, composition, primary_id, primary_sg_num, snlgroup_data[primary_id]['task_id'] \ if primary_id in snlgroup_data else '', secondary_id, secondary_sg_num, snlgroup_data[secondary_id]['task_id'] \ if secondary_id in snlgroup_data else '', delta_energy, delta_bandgap, delta_volume_per_atom, rms_dist_str, scenario ] if delta_energy and delta_bandgap: writer1.writerow(row) else: writer2.writerow(row) elif args.fig_id == 16: out_fig = Figure() badsnls_trace = Scatter(x=[], y=[], text=[], mode='markers', name='SG Changes') bisectrix = Scatter(x=[0,230], y=[0,230], mode='lines', name='bisectrix') print 'pulling bad snls from plotly ...' bad_snls = OrderedDict() for category, text in zip(fig['data'][2]['y'], fig['data'][2]['text']): for snl_id in map(int, text.split('<br>')): bad_snls[snl_id] = category with open('mpworks/check_snl/results/bad_snls.csv', 'wb') as f: print 'pulling bad snls from database ...' mpsnl_cursor = sma.snl.find({ 'snl_id': { '$in': bad_snls.keys() }, 'about.projects': {'$ne': 'CederDahn Challenge'} }) writer = csv.writer(f) writer.writerow([ 'snl_id', 'category', 'snlgroup_key', 'nsites', 'remarks', 'projects', 'authors' ]) print 'writing bad snls to file ...' for mpsnl_dict in mpsnl_cursor: mpsnl = MPStructureNL.from_dict(mpsnl_dict) row = [ mpsnl.snl_id, bad_snls[mpsnl.snl_id], mpsnl.snlgroup_key ] row += _get_snl_extra_info(mpsnl) writer.writerow(row) sg_num = mpsnl.snlgroup_key.split('--')[1] if (bad_snls[mpsnl.snl_id] == 'SG default' and sg_num != '-1') or \ bad_snls[mpsnl.snl_id] == 'SG change': mpsnl.structure.remove_oxidation_states() sf = SpacegroupAnalyzer(mpsnl.structure, symprec=0.1) badsnls_trace['x'].append(mpsnl.sg_num) badsnls_trace['y'].append(sf.get_spacegroup_number()) badsnls_trace['text'].append(mpsnl.snl_id) if bad_snls[mpsnl.snl_id] == 'SG default': print sg_num, sf.get_spacegroup_number() print 'plotting out-fig ...' out_fig['data'] = Data([bisectrix, badsnls_trace]) out_fig['layout'] = Layout( showlegend=False, hovermode='closest', title='Spacegroup Assignment Changes', xaxis=XAxis(showgrid=False, title='old SG number', range=[0,230]), yaxis=YAxis(showgrid=False, title='new SG number', range=[0,230]), ) filename = 'spacegroup_changes_' filename += datetime.datetime.now().strftime('%Y-%m-%d') py.plot(out_fig, filename=filename, auto_open=False) elif args.fig_id == 43: # SNLGroupMemberChecker matcher2 = StructureMatcher( ltol=0.2, stol=0.3, angle_tol=5, primitive_cell=False, scale=True, attempt_supercell=True, comparator=ElementComparator() ) print 'pulling data from plotly ...' trace = Scatter(x=[], y=[], text=[], mode='markers', name='mismatches') bad_snls = OrderedDict() # snlgroup_id : [ mismatching snl_ids ] for category, text in zip(fig['data'][2]['y'], fig['data'][2]['text']): if category != 'mismatch': continue for entry in text.split('<br>'): fields = entry.split(':') snlgroup_id = int(fields[0].split(',')[0]) print snlgroup_id snlgrp_dict = sma.snlgroups.find_one({ 'snlgroup_id': snlgroup_id }) snlgrp = SNLGroup.from_dict(snlgrp_dict) s1 = snlgrp.canonical_structure.get_primitive_structure() bad_snls[snlgroup_id] = [] for i, snl_id in enumerate(fields[1].split(',')): mpsnl_dict = sma.snl.find_one({ 'snl_id': int(snl_id) }) if 'CederDahn Challenge' in mpsnl_dict['about']['projects']: print 'skip CederDahn: %s' % snl_id continue mpsnl = MPStructureNL.from_dict(mpsnl_dict) s2 = mpsnl.structure.get_primitive_structure() is_match = matcher2.fit(s1, s2) if is_match: continue bad_snls[snlgroup_id].append(snl_id) trace['x'].append(snlgroup_id) trace['y'].append(i+1) trace['text'].append(snl_id) if len(bad_snls[snlgroup_id]) < 1: bad_snls.pop(snlgroup_id, None) with open('mpworks/check_snl/results/bad_snlgroups.csv', 'wb') as f: print 'pulling bad snlgroups from database ...' snlgroup_cursor = sma.snlgroups.find({ 'snlgroup_id': { '$in': bad_snls.keys() }, }) writer = csv.writer(f) writer.writerow(['snlgroup_id', 'snlgroup_key', 'mismatching snl_ids']) print 'writing bad snlgroups to file ...' for snlgroup_dict in snlgroup_cursor: snlgroup = SNLGroup.from_dict(snlgroup_dict) row = [ snlgroup.snlgroup_id, snlgroup.canonical_snl.snlgroup_key, ' '.join(bad_snls[snlgroup.snlgroup_id]) ] writer.writerow(row) print 'plotting out-fig ...' out_fig = Figure() out_fig['data'] = Data([trace]) out_fig['layout'] = Layout( showlegend=False, hovermode='closest', title='Member Mismatches of SNLGroup Canonicals', xaxis=XAxis(showgrid=False, title='snlgroup_id', showexponent='none'), yaxis=YAxis(showgrid=False, title='# mismatching SNLs'), ) filename = 'groupmember_mismatches_' filename += datetime.datetime.now().strftime('%Y-%m-%d') py.plot(out_fig, filename=filename, auto_open=False) else: errors = Counter() bad_snls = OrderedDict() bad_snlgroups = OrderedDict() for i,d in enumerate(fig['data']): if not isinstance(d, Scatter): continue if not 'x' in d or not 'y' in d or not 'text' in d: continue start_id = int(d['name'].split(' - ')[0][:-1])*1000 marker_colors = d['marker']['color'] if i < 2*num_snl_streams: # spacegroups errors += Counter(marker_colors) for idx,color in enumerate(marker_colors): snl_id = start_id + d['x'][idx] color_index = category_colors.index(color) category = categories[color_index] bad_snls[snl_id] = category else: # groupmembers for idx,color in enumerate(marker_colors): if color != category_colors[0]: continue snlgroup_id = start_id + d['x'][idx] mismatch_snl_id, canonical_snl_id = d['text'][idx].split(' != ') bad_snlgroups[snlgroup_id] = int(mismatch_snl_id) print errors fig_data = fig['data'][-1] fig_data['x'] = [ errors[color] for color in fig_data['marker']['color'] ] filename = _get_filename() print filename #py.plot(fig, filename=filename) with open('mpworks/check_snl/results/bad_snls.csv', 'wb') as f: mpsnl_cursor = sma.snl.find({ 'snl_id': { '$in': bad_snls.keys() } }) writer = csv.writer(f) writer.writerow([ 'snl_id', 'category', 'snlgroup_key', 'nsites', 'remarks', 'projects', 'authors' ]) for mpsnl_dict in mpsnl_cursor: mpsnl = MPStructureNL.from_dict(mpsnl_dict) row = [ mpsnl.snl_id, bad_snls[mpsnl.snl_id], mpsnl.snlgroup_key ] row += _get_snl_extra_info(mpsnl) writer.writerow(row) with open('mpworks/check_snl/results/bad_snlgroups.csv', 'wb') as f: snlgrp_cursor = sma.snlgroups.find({ 'snlgroup_id': { '$in': bad_snlgroups.keys() } }) first_mismatch_snls_cursor = sma.snl.find({ 'snl_id': { '$in': bad_snlgroups.values() } }) first_mismatch_snl_info = OrderedDict() for mpsnl_dict in first_mismatch_snls_cursor: mpsnl = MPStructureNL.from_dict(mpsnl_dict) first_mismatch_snl_info[mpsnl.snl_id] = _get_snl_extra_info(mpsnl) writer = csv.writer(f) writer.writerow([ 'snlgroup_id', 'snlgroup_key', 'canonical_snl_id', 'first_mismatching_snl_id', 'nsites', 'remarks', 'projects', 'authors' ]) for snlgrp_dict in snlgrp_cursor: snlgrp = SNLGroup.from_dict(snlgrp_dict) first_mismatch_snl_id = bad_snlgroups[snlgrp.snlgroup_id] row = [ snlgrp.snlgroup_id, snlgrp.canonical_snl.snlgroup_key, snlgrp.canonical_snl.snl_id, first_mismatch_snl_id ] row += [ ' & '.join(pair) if pair[0] != pair[1] else pair[0] for pair in zip( _get_snl_extra_info(snlgrp.canonical_snl), first_mismatch_snl_info[int(first_mismatch_snl_id)] ) ] writer.writerow(row)
from plotly.tools import FigureFactory as FF # In[3]: #lets us see the charts in an iPython Notebook pyo.offline.init_notebook_mode() # run at the start of every ipython # ## Changing the font: # Let's get the table we worked on in the last lesson: # In[4]: table = py.get_figure("rmuir", 313) pyo.iplot(table) py.image.save_as(table, r"C:\Users\Rytch\Google Drive\Financial\Passive Income\Online courses\Plotly\Course Content\Lessons\(09) Tables\Notebooks\images\Tables (5) - Styling the text in a table\pyo.iplot-0.png") # # As we learnt previously, the text in the tables is stored as annotations in the layout section of the figure. # # We can therefore loop through the list of annotations and apply the styling options as necessary. We'll probably want to style the header/index annotations differently to the those which show data, however there is no direct way to identify which annotation is a header/index and which belongs in a row. # # We can see that the header and index items are shown in bold in the table. To find out which items are in the header/index, let's loop through the annotations and look for <code>'<b>'</code>: # In[5]: for ann in table['layout']['annotations']:
#!/usr/bin/python import plotly.plotly as py import plotly.tools as tls import plotly.graph_objs as go from IPython.display import Image import json data = py.get_figure('dfreder1', '69').get_data() datadict = data[0] dates = datadict['x'] def f(x): return x >=1900 and x <= 2010 def createDict(s): d = {} #return a dictionary for word in s: if word in d: d[word] = d[word] + 1 else: d[word] = 1 return d parse_dates = sorted(filter(f, dates)) #dictionary keys should be sorted freq_dict = createDict(parse_dates) #create a cumulative list sumvals = 0
from pandas import DataFrame # In[3]: #lets us see the charts in an iPython Notebook pyo.offline.init_notebook_mode() # run at the start of every ipython # ## Styling the legend border # # Putting a border around your legend is a great way to visually separate it from your chart. In Plotly we can control the thickness and colour of the legend border. # # Let's grab our stacked bar chart showing meteorite landings per continent and the percentage of meteorites under 101 grams in mass; we'll use this to practise on in this section. # In[6]: stacked = py.get_figure("rmuir", 251) pyo.iplot(stacked) py.image.save_as( stacked, r"C:\Users\Rytch\Google Drive\Financial\Passive Income\Online courses\Plotly\Course Content\Lessons\(07) Chart Presentation 3\Notebooks\images\Chart Presentation (1) - Styling the legend\pyo.iplot-0.png" ) # # The legend styling options are contained within the layout. We're going to use the <code>bordercolor</code> and <code>borderwidth</code> keys to style the legend's border. # # Let's set the borderwidth to 5 to see how that affects the legend: # In[7]: stacked['layout'].update({'legend': {'borderwidth': 5}})
filename=graph_unique_name, fileopt='extend', auto_open=False) break except PlotlyError, ex: #usually first try will give an error if i > 1: L.l.info('Error extending graph {} in pass {}, err={}'.format( graph_unique_name, i, ex)) #first time failed, so second time we try an extend, but trace definition will change trace_list[0] = trace_ref_extend if i > 1: trace_list.append(trace_empty) if not graph_url is None: try: figure = py.get_figure(graph_url) for serie in figure['data']: remote_type = serie['type'] if 'name' in serie: remote_name = serie['name'] else: L.l.warning('Unable to find name field in graph, skipping') remote_name = 'N/A' #remote_x=serie['x'] #remote_y=serie['y'] if 'text' in serie: remote_id_text = serie['text'] else: #FIXME: plotly api changed, fix this! #Log.logger.warning('Could not find serie [{}] field in graph [{}]'.format(remote_name,graph_unique_name)) remote_id_text = remote_name
import plotly.plotly as py py.sign_in('TestBot', 'r1neazxo9w') fig = py.get_figure("https://plot.ly/~PlotBot/5") fig['layout']['title'] = "Never forget that title!" plot_url = py.plot(fig, filename="python-change_plot")
# ## Retrieving a chart from your plotly account # # Before we can begin to polish our charts, we must first retrieve them. # # We can use the <code>plotly.get_figure()</code> function to get the Figure object which relates to your chart. # # We can retrieve the chart in two ways: # - pass the plotly url of the chart (you can get this by clicking 'Share' on the chart and getting the shareable link # - pass the file owner and the unique file id separately # # Here's how we get it the first way: # In[3]: stocks = py.get_figure("https://plot.ly/~rmuir/162/stock-closing-prices-for-apple-in-2012/") # And the second: # In[4]: stocks = py.get_figure('rmuir', 162) stocks # We can now pass this object to the <code>po.iplot()</code> function to confirm that it's the correct chart: # In[5]: pyo.iplot(stocks)
y=df['"{}{}"'.format(continent,', y')], text=df['"{}{}"'.format(continent,', text')], marker=Marker(size=df['"{}{}"'.format(continent,', size')], sizemode='area', sizeref=131868,), mode='markers', name=continent) for continent in ['Africa', 'Americas', 'Asia', 'Europe', 'Oceania'] ], 'layout': Layout(xaxis=XAxis(title='Life Expectancy'), yaxis=YAxis(title='GDP per Capita', type='log')) }, show_link=False) import cufflinks as cf iplot(cf.datagen.lines().iplot(asFigure=True, kind='scatter',xTitle='Dates',yTitle='Returns',title='Returns')) import plotly.plotly as py fig = py.get_figure('https://plot.ly/~jackp/8715', raw=True) iplot(fig) import plotly.offline as offline import plotly.graph_objs as go offline.init_notebook_mode() offline.iplot({'data': [{'y': [4, 2, 3, 4]}], 'layout': {'title': 'Test Plot', 'font': dict(size=16)}}, image='png') import plotly.offline as offline import plotly.graph_objs as go
# In[6]: sub # The domain for a normal (x/y anchored trace) is set by linking the x- and y-axes in the trace to the x- and y-axes in the layout. This is done by the <code>make_subplots()</code> function when we specify <code>row = 1</code> and <code>col = 1</code>. # # We don't have this luxury when adding pie charts to a subplots object. Instead we must specify the domain of the chart directly, and append the trace to the data portion of the subplots object using the Python <code>list.append()</code> function. # ## Setting the domain for pie traces # # Let's try this out. I'm going to load a pie chart that we've created in this section and append the trace to the data object in the figure: # In[7]: eth = py.get_figure("rmuir", 267) pyo.iplot(eth) py.image.save_as(eth, r"C:\Users\Rytch\Google Drive\Financial\Passive Income\Online courses\Plotly\Course Content\Lessons\(08) Pie Charts\Notebooks\images\Pie charts (7) - Using pie charts in a sub-plots object\pyo.iplot-1.png") # # Let's now take the data from the <code>eth</code> pie chart and append it to the data in the <code>sub</code> subplots object. In order for this trace to be accepted, we must specify a domain in the form: # ````python # 'domain' : {'x' : [min, max], # 'y' : [min, max]} # ```` # I'm going to place this trace in the top-right cell of the subplots object, in cell x2/y2. This means that we must specify the domain as: # ````python # 'domain' : {'x' : [0.55, 1.0], # 'y' : [0.575, 1.0]}
size=16, color='' ), exponentformat="none", showline=True, range=[0,30], ), yaxis=YAxis( title='Probability of false positives', type='log', exponentformat='power', autorange=True, titlefont=Font( family='', size=18, color='' ), tickfont=Font( family='', size=12, color='' ), showline=True, ), ) fig = Figure(data=data, layout=layout) plot_url = py.plot(fig) print "url=",plot_url figure = py.get_figure(plot_url) py.image.save_as(figure, 'figure.png')
mat = sma2.database.materials.find_one( {'snlgroup_id_final': snlgroup_id}, {'_id': 0, 'task_id': 1} ) if mat is not None: return mat['task_id'] return 'not found' def _get_mp_link(mp_id): if mp_id == 'not found': return mp_id url = 'link:$$https://materialsproject.org/materials/' url += mp_id url += '$$[%s]' % mp_id return url fig = py.get_figure('tschaume',11) df = DataFrame.from_dict(fig['data'][1]).filter(['x','y','text']) grouped_x = df.groupby('x') print '|===============================' print '| old SG | close to bisectrix | far from bisectrix' for n,g in grouped_x: if g.shape[0] < 2: continue # at least two entries at same old SG grouped_y = g.groupby('y') if len(grouped_y.groups) < 2: continue # at least two different entries g['diff'] = g['x'] - g['y'] gs = g.sort('diff') # first entry: closest to bisectrix, last entry: farthest first, last = gs.iloc[0], gs.iloc[-1] ratios = [ float(abs(first['diff']))/float(first['x']), float(abs(last['diff']))/float(last['x']) ]