def web_plotting_plots(): # static start = dt.datetime(2010, 1, 1) end = dt.datetime(2013, 1, 27) data = DataReader("MSFT", 'google', start, end) data = data.reset_index() fig, ax = plt.subplots() data.plot(x='Date', y='Close', grid=True, ax=ax) plt.savefig(PATH + 'MSFT.png', dpi=300) # interactive - may not work in cloud 9 warnings.simplefilter('ignore') py.sign_in('Python-Demo-Account', 'gwt101uhh0') # to interactive D3.js plot py.iplot_mpl(fig) # direct approach with Cufflinks data.set_index('Date')['Close'].iplot(world_readable=True)
def plot_df(d, ylabel = '', xlabel = '', title = '', interactive = False, rotate = False): fig = plt.figure(figsize=(10, 4), dpi=80) for c in d.columns: plt.plot(d.index, d[c], label = c) if rotate: plt.xticks(d.index, d.index, rotation='vertical') plt.ylabel(ylabel) plt.xlabel(xlabel) plt.title(title) if interactive: plt.legend() return py.iplot_mpl(fig) else: plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
def plot_df(d, ylabel='', xlabel='', title='', interactive=False, rotate=False): fig = plt.figure(figsize=(10, 4), dpi=80) for c in d.columns: plt.plot(d.index, d[c], label=c) if rotate: plt.xticks(d.index, d.index, rotation='vertical') plt.ylabel(ylabel) plt.xlabel(xlabel) plt.title(title) if interactive: plt.legend() return py.iplot_mpl(fig) else: plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
import plotly.plotly as py import plotly.tools as tls from plotly.graph_objs import * import numpy as np import matplotlib.pyplot as plt import os from astropy.io import ascii py.sign_in('Cat_Phish', 'c4i0ux16og') def plot_mpl_fig(): rootdir = 'C:\Users\Cat\Documents\Research_Halos\HaloDetail' for subdir, dirs, files in os.walk(rootdir): head,tail = os.path.split(subdir) haloname = tail for file in files: if file.endswith('.list'): oldvalues = ascii.read(os.path.join(subdir, file), format = 'commented_header') #Get full path and access file whparent = np.where(max(oldvalues['mvir(10)'])) parent_spin_val = oldvalues[whparent]['Spin(26)'] #Find parent spin parameter elif file.endswith('_Lambda.txt'): newvalues = ascii.read(os.path.join(subdir, file), format = 'commented_header') #Get full path and access file whalo = np.where(newvalues['Halo_Name'] == haloname) #Locate halo orb_ang_mom = newvalues[whalo]['orb_lambda_subhalo'] plt.loglog(parent_spin_val, orb_ang_mom, alpha=0.8,label=haloname) print "%s done. On to the next." %haloname plot_mpl_fig() mpl_fig1 = plt.gcf() py_fig1 = tls.mpl_to_plotly(mpl_fig1, verbose=True) #print(py_fig1.to_string()) py.iplot_mpl(mpl_fig1, strip_style=True, filename='Comparing Host Spin Parameter to Normalized(from subhalos) orbital Angular Momentum of Satellites') py_fig1_ss = tls.mpl_to_plotly(mpl_fig1, strip_style=True) py.plot(py_fig1_ss, filename='Comparing Host Spin Parameter to Normalized(from subhalos) orbital Angular Momentum of Satellites')
def plot_plotly(self, plotly_filename=None, mpl_type=False, xlim=None, ylim=None, title=None, figsize=None, xlabel=None, ylabel=None, fontsize=None, show_legend=True, grid=False): """ Plot data using plotly library in IPython :param plotly_filename: name for resulting plot file on server (use unique name, else the same plot will be showen) :type plotly_filename: None or str :param bool mpl_type: use or not plotly converter from matplotlib (experimental parameter) :param xlim: x-axis range :param ylim: y-axis range :type xlim: None or tuple(x_min, x_max) :type ylim: None or tuple(y_min, y_max) :param title: title :type title: None or str :param figsize: figure size :type figsize: None or tuple(weight, height) :param xlabel: x-axis name :type xlabel: None or str :param ylabel: y-axis name :type ylabel: None or str :param fontsize: font size :type fontsize: None or int :param bool show_legend: show or not labels for plots :param bool grid: show grid or not """ import plotly.plotly as py from plotly import graph_objs from ipykernel import connect plotly_filename = self.plotly_filename if plotly_filename is None else plotly_filename try: connection_file_path = connect.find_connection_file() connection_file = os.path.basename(connection_file_path) if '-' in connection_file: kernel_id = connection_file.split('-', 1)[1].split('.')[0] else: kernel_id = connection_file.split('.')[0] except Exception as e: kernel_id = "no_kernel" PLOTLY_API_USER, PLOTLY_API_KEY, PLOTLY_USER = self._plotly_config() save_name = '{user}_{id}:{name}'.format(user=PLOTLY_USER, id=kernel_id, name=plotly_filename) py.sign_in(PLOTLY_API_USER, PLOTLY_API_KEY) if mpl_type: self.plot(new_plot=True, xlim=xlim, ylim=ylim, title=title, figsize=figsize, xlabel=xlabel, ylabel=ylabel, fontsize=fontsize, grid=grid) mpl_fig = plt.gcf() update = dict( layout=dict( showlegend=show_legend ), data=[dict(name=leg) for leg in mpl_fig.legends] ) return py.iplot_mpl(mpl_fig, width=self.figsize[0] * 60, update=update, height=self.figsize[1] * 60, filename=save_name, fileopt='overwrite') xlabel = self.xlabel if xlabel is None else xlabel ylabel = self.ylabel if ylabel is None else ylabel title = self.title if title is None else title figsize = self.figsize if figsize is None else figsize fontsize = self.fontsize if fontsize is None else fontsize layout = graph_objs.Layout(yaxis={'title': ylabel, 'ticks': ''}, xaxis={'title': xlabel, 'ticks': ''}, showlegend=show_legend, title=title, font=graph_objs.Font(family='Courier New, monospace', size=fontsize), width=figsize[0] * self.PLOTLY_RESIZE, height=figsize[1] * self.PLOTLY_RESIZE ) fig = self._plot_plotly(layout) return py.iplot(fig, width=figsize[0] * self.PLOTLY_RESIZE, height=figsize[1] * self.PLOTLY_RESIZE, filename=save_name)
#To Plot Spin Parameters ############################################################################# def plot_mpl_fig(): rootdir = '/Users/catherinefielder/Documents/Research_Halos/HaloDetail' highmasses = [] pops = [] for subdir, dirs, files in os.walk(rootdir): head,tail = os.path.split(subdir) haloname = tail for file in files: if file.endswith('.list'): oldvalues = ascii.read(os.path.join(subdir, file), format = 'commented_header') #Get full path and access file whparent = np.where(max(oldvalues['mvir(10)'])) highmass=max(oldvalues['mvir(10)']) highmasses = np.append(highmasses, highmass) elif file.endswith('_from_CMF.txt'): newvalues = ascii.read(os.path.join(subdir, file), format = 'commented_header') #Get full path and access file whalo = np.where(newvalues['Halo_Name'] == haloname) #Locate halo pop = newvalues[whalo]['interpolated_y'] pops = np.append(pops, pop) plt.loglog(highmass, pop, alpha=0.8,label=haloname) print "%s done. On to the next." %haloname spearman = scipy.stats.spearmanr(highmasses, pops) print spearman plot_mpl_fig() mpl_fig1 = plt.gcf() py_fig1 = tls.mpl_to_plotly(mpl_fig1, verbose=True) py.iplot_mpl(mpl_fig1, strip_style=True, filename='Interpolated Population vs. Halo Mass') py_fig1_ss = tls.mpl_to_plotly(mpl_fig1, strip_style=True) py.plot(py_fig1_ss, filename='Interpolated Population vs. Halo Mass')
y_pos += 0.25 elif column == "Business": y_pos -= 0.75 elif column == "Math and Statistics": y_pos += 0.75 elif column == "Architecture": y_pos -= 0.75 elif column == "Computer Science": y_pos += 0.75 elif column == "Engineering": y_pos -= 0.25 #again, making sure size of labels is readable plt.text(2011.5, y_pos, column, fontsize=14, color=tableau20[rank]) #titles plt.text(1995, 93, "Percentage of Bachelor's degree conferred to women in the U.S.A" ", by major (1970-2012)", fontsize=17, ha="center") #and of course, citations! plt.text(1966, -8, "Data source: nces.ed.gov/programs/digest/2013menu_tables.asp" "\nAuthor: Randy Olson (randalolson.com / @randal_olson)" "\nNote: Some majors are missing because the historical data " "is not available for them", fontsize=10) #plt.savefig("percent-bachelors-degrees-women.png", bbox_inches="tight") #see if we can put it on plotly dataviz1 = plt.gcf() ply.iplot_mpl(dataviz1,resize=False,filename='dataviz1',width=960,height=1120)
py.sign_in('Cat_Phish', 'c4i0ux16og') ############################################################################# #To Plot Angular Momentum Versus Satellite Abundance ############################################################################# def plot_mpl_fig(): rootdir = '/Users/Cat/Documents/Research_Halos/HaloDetail' for subdir, dirs, files in os.walk(rootdir): head,tail = os.path.split(subdir) haloname = tail for file in files: if file.endswith('.list'): values1 = ascii.read(os.path.join(subdir, file), format = 'commented_header') #Get full path and access file whparent = np.where(max(values1['mvir(10)'])) parent_spin = values1[whparent]['Spin(26)'] elif file.endswith('_subhalos'): values2 = ascii.read(os.path.join(subdir, file), format = 'commented_header') #Get full path and access file index = np.arange(len(values2['mvir(10)'])) rindex = len(index)-index pop = len(values2['mvir(10)']) print pop plt.loglog(parent_spin, pop, alpha=0.8,label=haloname) print "%s done. On to the next." %haloname plot_mpl_fig() mpl_fig1 = plt.gcf() py_fig1 = tls.mpl_to_plotly(mpl_fig1, verbose=True) py.iplot_mpl(mpl_fig1, strip_style=True, filename='Abundance vs. Host Spin Parameter') py_fig1_ss = tls.mpl_to_plotly(mpl_fig1, strip_style=True) py.plot(py_fig1_ss, filename='Abundance vs. Host Spin Parameter')
y_pos += 0.75 elif column == "Engineering": y_pos -= 0.25 #again, making sure size of labels is readable plt.text(2011.5, y_pos, column, fontsize=14, color=tableau20[rank]) #titles plt.text(1995, 93, "Percentage of Bachelor's degree conferred to women in the U.S.A" ", by major (1970-2012)", fontsize=17, ha="center") #and of course, citations! plt.text(1966, -8, "Data source: nces.ed.gov/programs/digest/2013menu_tables.asp" "\nAuthor: Randy Olson (randalolson.com / @randal_olson)" "\nNote: Some majors are missing because the historical data " "is not available for them", fontsize=10) #plt.savefig("percent-bachelors-degrees-women.png", bbox_inches="tight") #see if we can put it on plotly dataviz1 = plt.gcf() ply.iplot_mpl(dataviz1, resize=False, filename='dataviz1', width=960, height=1120)
import os import scipy from scipy import stats from astropy.io import ascii py.sign_in('Cat_Phish', 'c4i0ux16og') def plot_mpl_fig(): rootdir = '/Users/catherinefielder/Documents/Research_Halos/HaloDetail' rmaxs = [] vmaxs = [] for subdir, dirs, files in os.walk(rootdir): head,tail = os.path.split(subdir) haloname = tail for file in files: if file.endswith('.list'): oldvalues = ascii.read(os.path.join(subdir, file), format = 'commented_header') #Get full path and access file whparent = np.where(max(oldvalues['mvir(10)'])) rmax = oldvalues[whparent]['rs(12)']*2.16 vmax = oldvalues[whparent]['vmax(16)'] rmaxs = np.append(rmaxs, rmax) vmaxs = np.append(vmaxs, vmax) plt.loglog(rmax, vmax, alpha=0.8,label=haloname) print "%s done. On to the next." %haloname spearman = scipy.stats.spearmanr(rmaxs, vmaxs) print spearman plot_mpl_fig() mpl_fig1 = plt.gcf() py_fig1 = tls.mpl_to_plotly(mpl_fig1, verbose=True) #print(py_fig1.to_string()) py.iplot_mpl(mpl_fig1, strip_style=True, filename='Rmax-Vmax Host') py_fig1_ss = tls.mpl_to_plotly(mpl_fig1, strip_style=True) py.plot(py_fig1_ss, filename='Rmax-Vmax Host')
#To Plot Spin Parameters ############################################################################# def plot_mpl_fig(): rootdir = '/Users/catherinefielder/Documents/Research_Halos/HaloDetail' parent_spin_vals = [] avg_spin_vals = [] for subdir, dirs, files in os.walk(rootdir): head,tail = os.path.split(subdir) haloname = tail for file in files: if file.endswith('.list'): oldvalues = ascii.read(os.path.join(subdir, file), format = 'commented_header') #Get full path and access file whparent = np.where(max(oldvalues['mvir(10)'])) parent_spin_val = oldvalues[whparent]['Spin(26)'] #Find parent spin parameter parent_spin_vals = np.append(parent_spin_vals, parent_spin_val) elif file.endswith('_subhalos'): newvalues = ascii.read(os.path.join(subdir, file), format = 'commented_header') #Get full path and access file child_spin_vals = newvalues['Spin(26)'] avg_spin_val = (np.mean(child_spin_vals)) avg_spin_vals = np.append(avg_spin_vals, avg_spin_val) plt.loglog(avg_spin_val, parent_spin_val, alpha=0.8,label=haloname) print "%s done. On to the next." %haloname spearman = scipy.stats.spearmanr(avg_spin_vals, parent_spin_vals) print spearman plot_mpl_fig() mpl_fig1 = plt.gcf() py_fig1 = tls.mpl_to_plotly(mpl_fig1, verbose=True) py.iplot_mpl(mpl_fig1, strip_style=True, filename='Spin Parameter Plot') py_fig1_ss = tls.mpl_to_plotly(mpl_fig1, strip_style=True) py.plot(py_fig1_ss, filename='Spin Parameter Plot')
from astropy.io import ascii py.sign_in('Cat_Phish', 'c4i0ux16og') def plot_mpl_fig(): rootdir = '/Users/catherinefielder/Documents/Research_Halos/HaloDetail' orb_ang_moms = [] host_masses = [] for subdir, dirs, files in os.walk(rootdir): head,tail = os.path.split(subdir) haloname = tail for file in files: if file.endswith('.list'): oldvalues = ascii.read(os.path.join(subdir, file), format = 'commented_header') #Get full path and access file whparent = np.where(max(oldvalues['mvir(10)'])) host_mass = oldvalues[whparent]['mvir(10)'] host_masses = np.append(host_masses, host_mass) elif file.endswith('_Lambda.txt'): newvalues = ascii.read(os.path.join(subdir, file), format = 'commented_header') #Get full path and access file whalo = np.where(newvalues['Halo_Name'] == haloname) #Locate halo orb_ang_mom = newvalues[whalo]['orb_lambda_host'] orb_ang_moms = np.append(orb_ang_moms, orb_ang_mom) plt.loglog(host_mass, orb_ang_mom, alpha=0.8,label=haloname) print "%s done. On to the next." %haloname spearman = scipy.stats.spearmanr(host_masses, orb_ang_moms) print spearman plot_mpl_fig() mpl_fig1 = plt.gcf() py_fig1 = tls.mpl_to_plotly(mpl_fig1, verbose=True) #print(py_fig1.to_string()) py.iplot_mpl(mpl_fig1, strip_style=True, filename='Comparing Host Mass to Normalized(from host) orbital Angular Momentum of Satellites') py_fig1_ss = tls.mpl_to_plotly(mpl_fig1, strip_style=True) py.plot(py_fig1_ss, filename='Comparing Host Mass to Normalized(from host) orbital Angular Momentum of Satellites')
## NUMBER OF ASSETS n_assets = 4 ## NUMBER OF OBSERVATIONS n_obs = 1000 return_vec = np.random.randn(n_assets, n_obs) # In[5]: fig = plt.figure() plt.plot(return_vec.T, alpha=.4); plt.xlabel('time') plt.ylabel('returns') py.iplot_mpl(fig, filename='s6_damped_oscillation') # These return series can be used to create a wide range of portfolios, which all # have different returns and risks (standard deviation). We can produce a wide range # of random weight vectors and plot those portfolios. As we want all our capital to be invested, this vector will have to some to one. # In[6]: def rand_weights(n): ''' Produces n random weights that sum to 1 ''' k = np.random.rand(n) return k / sum(k) print rand_weights(n_assets) print rand_weights(n_assets)
## NUMBER OF ASSETS n_assets = 4 ## NUMBER OF OBSERVATIONS n_obs = 1000 return_vec = np.random.randn(n_assets, n_obs) # In[5]: fig = plt.figure() plt.plot(return_vec.T, alpha=.4) plt.xlabel('time') plt.ylabel('returns') py.iplot_mpl(fig, filename='s6_damped_oscillation') # These return series can be used to create a wide range of portfolios, which all # have different returns and risks (standard deviation). We can produce a wide range # of random weight vectors and plot those portfolios. As we want all our capital to be invested, this vector will have to some to one. # In[6]: def rand_weights(n): ''' Produces n random weights that sum to 1 ''' k = np.random.rand(n) return k / sum(k) print rand_weights(n_assets)
def simple_plotly(fig): try: return py.iplot_mpl(fig) except Exception: pass
highvel = [] for subdir, dirs, files in os.walk(rootdir): head,tail = os.path.split(subdir) haloname = tail for file in files: if file.endswith('_subhalos'): oldvalues = ascii.read(os.path.join(subdir, file), format = 'commented_header') #Get full path and access file vmax = oldvalues['vmax(16)'] mass = oldvalues['mvir(10)'] vmaxs = np.append(vmaxs, vmax) masses = np.append(masses, mass) whhi = np.where(mass>=1.0e8) whmed = np.where((mass>=1.0e6)&(mass<1.0e8)) whlo = np.where(mass<1.0e6) lowmass = np.append(lowmass, oldvalues[whlo]['mvir(10)']) medmass = np.append(medmass, oldvalues[whmed]['mvir(10)']) highmass = np.append(highmass, oldvalues[whhi]['mvir(10)']) lowvel = np.append(lowvel, oldvalues[whlo]['vmax(16)']) medvel = np.append(medvel, oldvalues[whmed]['vmax(16)']) highvel = np.append(highvel, oldvalues[whhi]['vmax(16)']) print "%s done. On to the next." %haloname plt.loglog(highmass, highvel, 'o', medmass, medvel, 'bs', lowmass, lowvel, 'g*', alpha=0.8) #spearman = scipy.stats.spearmanr(rmaxs, vmaxs) #print spearman plot_mpl_fig() mpl_fig1 = plt.gcf() py_fig1 = tls.mpl_to_plotly(mpl_fig1, verbose=True) #print(py_fig1.to_string()) py.iplot_mpl(mpl_fig1, strip_style=True, filename='Vmax-Mass Subhaloes') py_fig1_ss = tls.mpl_to_plotly(mpl_fig1, strip_style=True) py.plot(py_fig1_ss, filename='Vmax-Mass Subhaloes')
def plotly_date_frequency_plot( df=None, array=None, series=None, title=None, filename="plotly_date_frequency_plot_test", weekend_bool=True, weekend_height=20, xlabel=None, ylabel=None, vlines=None, hlines=None, xlim=None, ylim=None, **kwargs ): """plotly_date_frequency_plot uses plotly to interactively plot a bar chart showing a date series or df[array] frequency Requires df, array str or series To write a permanent, linkable plot, change filename Optional weekend indicators with adjustable height .. todo: Add hue parameter for stacked bar plot (see Jason's implementation) Add resample argument for weekly, monthly views """ if series is None: series = df[array] else: series = pd.Series(series) day_vcs = series.value_counts().sort_index() day_tuples = zip(day_vcs.keys(), day_vcs.values) if weekend_bool: # create list of weekend tuples to indicate weekends start, end = utils.date.extract_date(min(series)), utils.date.extract_date(max(series)) running_day = start weekend_days = [] while running_day <= end: if running_day.weekday() in set([5, 6]): weekend_days.append(running_day) running_day += dt.timedelta(days=1) if len(weekend_days) % 2 == 1: weekend_days = weekend_days[:-1] weekend_tuples = [] for i in range(len(weekend_days) / 2): weekend_tuples.append((weekend_days[i * 2], weekend_days[i * 2 + 1])) # plotly plot fig = plt.figure(figsize=(8, 6)) ax = fig.add_subplot(1, 1, 1) plt.bar(utils.date.extract_date([day[0] for day in day_tuples]), [count[1] for count in day_tuples], **kwargs) if weekend_bool: for i in weekend_tuples: plt.plot([i[0], i[1], i[0], i[1]], [0.1, weekend_height, weekend_height, 0.1], alpha=0.6, color="grey") if vlines is not None: if ylim is None: ylim = plt.ylim() if not isinstance(vlines, (list, pd.core.series.Series, np.ndarray)): vlines = [vlines] for vl in vlines: plt.plot([vl, vl], [ylim[0], ylim[1]]) if hlines is not None: if xlim is None: xlim = plt.xlim() if not isinstance(hlines, (list, pd.core.series.Series, np.ndarray)): hlines = [hlines] for hl in hlines: plt.plot([xlim[0], xlim[1]], [hl, hl]) if title is not None: plt.title(title, size=20) if xlabel is not None: plt.xlabel(xlabel, size=20) if ylabel is not None: plt.ylabel(ylabel, size=20) if xlim is not None: plt.xlim(xlim) if ylim is not None: plt.ylim(ylim) return py.iplot_mpl(fig, filename=filename, fileopt="overwrite")
left="off", right="off", labelleft="on", labelsize=16) ax.set_ylim(-5, 60) ax.set_xlim(-5, 100) ax.set_ylabel('% HH in Poverty', fontsize=fontsize) ax.set_xlabel('% Population', fontsize=fontsize) # Matplotlib code is very long... But sometimes you have existing matplotlib code, right? The good news is, plotly can eat it! # In[25]: py.iplot_mpl(mpl_fig, filename='baltimore-poverty') # So, at the moment, matplotlib legends do not fully convert to plotly legends (please refer to our [user guide](https://plot.ly/python/matplotlib-to-plotly-tutorial/#Careful,-matplotlib-is-not-perfect-%28yet%29)). Let's tweak this now. # In[26]: import plotly.tools as tls # In[27]: # Convert mpl fig object to plotly fig object, resize to plotly's default. py_fig = tls.mpl_to_plotly(mpl_fig, resize=True) # In[28]: # Give each trace a name to appear in legend.
data_file = urllib2.urlopen(data_url) df = pd.read_csv(data_file, sep=',') df = df.drop(df.index[0]) # drop first row (US totals) df = df[df['murder'] < 11] # drop out-of-range rows mpl_fig_bubble = plt.figure() # (!) set new mpl figure object ax = mpl_fig_bubble.add_subplot(111) # add axis plt.axis([0, 11, 200, 1280]) plt.xlabel('Murders per 100,000 population') plt.ylabel('Burglaries per 100,000 population') scatter = ax.scatter( df['murder'], df['burglary'], c=df['larceny_theft'], # using some color scale s=np.sqrt(df['population']), linewidths=2, edgecolor='w', alpha=0.6) for i_X, X in df.iterrows(): plt.text( X['murder'], X['burglary'], X['state'][0:8], # only the first 8 letters size=8, horizontalalignment='center') py.iplot_mpl(mpl_fig_bubble, filename='s6_bubble-chart')
# Now, we can use picks to select magnetometer data and plot it. The matplotlib graph can be converted into an interactive one using Plotly with just one line of code: # In[11]: picks = mne.pick_types(raw.info, meg='mag', exclude=[]) data, times = raw[picks[:10], start:stop] import matplotlib.pyplot as plt import plotly.plotly as py plt.plot(times, data.T) plt.xlabel('time (s)') plt.ylabel('MEG data (T)') update = dict(layout=dict(showlegend=True), data=[dict(name=raw.info['ch_names'][p]) for p in picks[:10]]) py.iplot_mpl(plt.gcf(), update=update) # But, we can also use MNE-Python's interactive data browser to get a better visualization: # In[12]: raw.plot(); # Let us do the same using Plotly. First, we import the required classes # In[13]: from plotly import tools from plotly.graph_objs import Layout, YAxis, Scatter, Annotation, Annotations, Data, Figure, Marker, Font
from astropy.io import ascii py.sign_in('Cat_Phish', 'c4i0ux16og') def plot_mpl_fig(): rootdir = '/Users/catherinefielder/Documents/Research_Halos/HaloDetail' orb_ang_moms = [] cs = [] for subdir, dirs, files in os.walk(rootdir): head,tail = os.path.split(subdir) haloname = tail for file in files: if file.endswith('.list'): oldvalues = ascii.read(os.path.join(subdir, file), format = 'commented_header') #Get full path and access file whparent = np.where(max(oldvalues['mvir(10)'])) c = oldvalues[whparent]['rvir(11)']/oldvalues[whparent]['rs(12)'] cs = np.append(cs, c) elif file.endswith('_Lambda.txt'): newvalues = ascii.read(os.path.join(subdir, file), format = 'commented_header') #Get full path and access file whalo = np.where(newvalues['Halo_Name'] == haloname) #Locate halo orb_ang_mom = newvalues[whalo]['orb_lambda_host'] orb_ang_moms = np.append(orb_ang_moms, orb_ang_mom) plt.loglog(orb_ang_mom, c, alpha=0.8,label=haloname) print "%s done. On to the next." %haloname spearman = scipy.stats.spearmanr(orb_ang_moms, cs) print spearman plot_mpl_fig() mpl_fig1 = plt.gcf() py_fig1 = tls.mpl_to_plotly(mpl_fig1, verbose=True) #print(py_fig1.to_string()) py.iplot_mpl(mpl_fig1, strip_style=True, filename='Concentration vs. Normalized(from host) Orbital Angular Momentum of Satellites') py_fig1_ss = tls.mpl_to_plotly(mpl_fig1, strip_style=True) py.plot(py_fig1_ss, filename='Concentration vs. Normalized(from host) Orbital Angular Momentum of Satellites')
import os import scipy from scipy import stats from astropy.io import ascii py.sign_in('Cat_Phish', 'c4i0ux16og') def plot_mpl_fig(): rootdir = '/Users/catherinefielder/Documents/Research_Halos/HaloDetail' parent_spin_vals = [] masses = [] for subdir, dirs, files in os.walk(rootdir): head,tail = os.path.split(subdir) haloname = tail for file in files: if file.endswith('.list'): oldvalues = ascii.read(os.path.join(subdir, file), format = 'commented_header') #Get full path and access file whparent = np.where(max(oldvalues['mvir(10)'])) parent_spin_val = oldvalues[whparent]['Spin(26)'] #Find parent spin parameter mass = max(oldvalues['mvir(10)']) parent_spin_vals = np.append(parent_spin_vals, parent_spin_val) masses = np.append(masses, mass) plt.loglog(mass, parent_spin_val, alpha=0.8,label=haloname) print "%s done. On to the next." %haloname spearman = scipy.stats.spearmanr(masses, parent_spin_vals) print spearman plot_mpl_fig() mpl_fig1 = plt.gcf() py_fig1 = tls.mpl_to_plotly(mpl_fig1, verbose=True) #print(py_fig1.to_string()) py.iplot_mpl(mpl_fig1, strip_style=True, filename='Spin parameter vs. Mass') py_fig1_ss = tls.mpl_to_plotly(mpl_fig1, strip_style=True) py.plot(py_fig1_ss, filename='Spin Parameter vs. Mass')
index25 = np.arange(len(nvfrac25)) rindex25 = len(index25)-index25 nrindex25 = np.delete(rindex25, whlimit25) rindex25s = np.append(rindex25s, nrindex25) vfrac25s = np.append(vfrac25s, nvfrac25) plt.loglog(vfrac25, rindex25, alpha=0.8, label=haloname) if c < twentyfifth: vfracx = sorted(values['vmax(16)']) whlimitx = np.where(vfracx<=0.13) nvfracx = np.delete(vfracx, whlimitx) indexx = np.arange(len(vfracx)) rindexx = len(indexx)-indexx nrindexx = np.delete(rindexx, whlimitx) rindexxs = np.append(rindexxs, nrindexx) vfracxs = np.append(vfracxs, nvfracx) plt.loglog(vfracx, rindexx, alpha=0.8, label=haloname) print "%s done. On to the next." %haloname #plt.loglog(sorted(vfracxs,reverse=True), sorted(rindexxs), label= haloname) #plt.loglog(sorted(vfrac25s,reverse=True), sorted(rindex25s), label=haloname) #plt.loglog(sorted(vfrac50s,reverse=True), sorted(rindex50s), label=haloname) #plt.loglog(sorted(vfrac75s,reverse=True), sorted(rindex75s), label=haloname) #plt.xlim(xmin=0.12, xmax=0.4) #print stats.ks_2samp(vfrac75s,vfracxs) plot_mpl_fig() mpl_fig1 = plt.gcf() py_fig1 = tls.mpl_to_plotly(mpl_fig1, verbose=True) #print(py_fig1.to_string()) py.iplot_mpl(mpl_fig1, strip_style=True, filename='Total Mean CMF') py_fig1_ss = tls.mpl_to_plotly(mpl_fig1, strip_style=True) py.plot(py_fig1_ss, filename='Total_Mean_CVF-style2')
x = np.linspace(0,40,1000) for i in range(4): sigma = np.sqrt(variance[i]) y = mlab.normpdf(x,mean[i],sigma) plt.plot(x,y, label=r'$v_{}$'.format(i+1)) plt.xlabel("X") plt.ylabel("P(X)") # To re-create the graph in Plotly and use Plotly's defaults, call `iplot` and add `strip_style`. # In[6]: py.iplot_mpl(fig1, strip_style = True) # It's shareable at a URL, contains the data as part of the plot, and can be edited collaboratively from any API or our web app. Head over to [Plotly's API](https://plot.ly/api) to see more, and check out our [user guide](https://plot.ly/python/user-guide/) to see how it all works. # # Plotly also jointly preserves the data in a graph, the graph, and the graph description (in this case JSON). That's valuable. [One study](http://www.smithsonianmag.com/science-nature/the-vast-majority-of-raw-data-from-old-scientific-studies-may-now-be-missing-180948067/?no-ist) in *current biology* found that over 90 percent of data from papers published over the past 20 years was not available. So sharing data is good for science and reproducibility, useful for your projects, and great for collaboration. ### II. ggplot2 plots in Plotly # Let's take a real-world look storing data and graphs together. Suppose you see a graph on the [World Bank website](http://blogs.worldbank.org/opendata/accessing-world-bank-data-apis-python-r-ruby-stata). The graph uses [ggplot2](http://ggplot2.org), a remarkable plotting library for R. # In[7]: from IPython.display import Image Image(url = 'http://i.imgur.com/PkRRmHq.png')
hostvalues = ascii.read(os.path.join(subdir, file), format = 'commented_header') highmass=max(hostvalues['mvir(10)']) hi = np.where(highmass>=1.0e+12) #Index of host with highmass low = np.where(highmass<1.0e+12) hi_id = hostvalues[hi]['id(1)'] #Id of the highmass host low_id = hostvalues[low]['id(1)'] whhi = np.where(hostvalues['pid(5)']==hi_id) #Indices of subhalos corresponding to the highmass host whlow = np.where(hostvalues['pid(5)']==low_id) hmass = hostvalues[whhi]['mvir(10)'] print len(hmass) lmass = hostvalues[whlow]['mvir(10)'] print len(lmass) hvel = hostvalues[whhi]['vmax(16)'] lvel = hostvalues[whlow]['vmax(16)'] hmasses = np.append(hmasses, hmass) lmasses = np.append(lmasses, lmass) hvels = np.append(hvels, hvel) lvels = np.append(lvels, lvel) print "%s done. On to the next." %haloname plt.hist(hmasses) plt.hist(lmasses) #spearman = scipy.stats.spearmanr(rmaxs, vmaxs) #print spearman plot_mpl_fig() mpl_fig1 = plt.gcf() py_fig1 = tls.mpl_to_plotly(mpl_fig1, verbose=True) #print(py_fig1.to_string()) py.iplot_mpl(mpl_fig1, strip_style=True, filename='Mass Distribution of Subhaloes') py_fig1_ss = tls.mpl_to_plotly(mpl_fig1, strip_style=True) py.plot(py_fig1_ss, filename='Mass Distriburion of Subhaloes')
def get_image(request): #get the name and date of the stock below stock = request.GET.get("name") stockNames = stock.split(",") stockNames.sort() numStocksToCompare = len(stock.split(',')) d = request.GET.get('date') begDate = dt.strptime(d, '%Y-%m-%d') d = d.replace('-', "") stock_price_url = 'https://www.quandl.com/api/v3/datatables/WIKI/PRICES.json?ticker=+stock+&date.gte=dt&qopts.columns=date,close&api_key=-2H8WyYB8b7FaCshLLTN' date = [] price = [] stock_price_url = stock_price_url.replace("+stock+", stock) stock_price_url = stock_price_url.replace("dt", d) source_code = urllib.request.urlopen(stock_price_url).read().decode() json_root = json.loads(source_code) json_datatable = json_root["datatable"] json_data = json_datatable["data"] datesFinal = [] pricesFinal = [] #if there are more than one stocks to compare if numStocksToCompare > 1: for day in json_data: #populate lists with all the data date.append(dt.strptime(day[0], '%Y-%m-%d')) price.append(day[1]) dataLength = len(date) #day will be the current day in the stock information that we received day = date[0] dayPrice = price[0] i = 1 #get the stock information for each stock for x in range(0, numStocksToCompare): datesFinal.append(day) #store the first date and price pricesFinal.append(dayPrice) day = date[i] while ( (abs((day - datesFinal[-1]).days) < 6) ): #if the range between the last date and newest date is larger than 5, we are at a new stock datesFinal.append(day) dayPrice = price[i] pricesFinal.append(dayPrice) i += 1 #if we've reached the end of the data if (i == dataLength): break day = date[i] d = datesFinal p = pricesFinal plt.plot_date(d, p, '-', label=stockNames[x].upper()) #plot the graph #if we've plotted all the stocks, break out of the loop if (stockNames[x] == stockNames[len(stockNames) - 1]): break del datesFinal[:] del pricesFinal[:] dayPrice = price[i] #store the first price of the next stock else: #store the price and date information for each day for day in json_data: date.append(day[0]) price.append(day[1]) plt.plot_date(date, price, '-', label=stock.upper()) #make sure the legend appears when the plotly graph is made update = dict(layout=dict(showlegend=True)) plt.xlabel('Date') plt.ylabel('Price') plt.title(stock.upper() + " Stock") #finally, we create plotly figure mpl_fig = plt.gcf() py_fig = tls.mpl_to_plotly(mpl_fig, verbose=True) py.iplot_mpl(mpl_fig, update=update, filename='graph') return render(request, 'home/graph.html')
import os import scipy from scipy import stats from astropy.io import ascii py.sign_in('Cat_Phish', 'c4i0ux16og') def plot_mpl_fig(): rootdir = '/Users/catherinefielder/Documents/Research_Halos/HaloDetail' cs = [] masses = [] for subdir, dirs, files in os.walk(rootdir): head,tail = os.path.split(subdir) haloname = tail for file in files: if file.endswith('.list'): oldvalues = ascii.read(os.path.join(subdir, file), format = 'commented_header') #Get full path and access file whparent = np.where(max(oldvalues['mvir(10)'])) mass = max(oldvalues['mvir(10)']) #Find parent spin parameter c = oldvalues[whparent]['rvir(11)']/oldvalues[whparent]['rs(12)'] masses = np.append(masses, mass) cs = np.append(cs, c) plt.loglog(mass, c, alpha=0.8,label=haloname) print "%s done. On to the next." %haloname spearman = scipy.stats.spearmanr(masses, cs) print spearman plot_mpl_fig() mpl_fig1 = plt.gcf() py_fig1 = tls.mpl_to_plotly(mpl_fig1, verbose=True) #print(py_fig1.to_string()) py.iplot_mpl(mpl_fig1, strip_style=True, filename='Concentration vs. Mass') py_fig1_ss = tls.mpl_to_plotly(mpl_fig1, strip_style=True) py.plot(py_fig1_ss, filename='Concentration vs. Mass')
ranked_df_body = unstacked_total.apply(lambda x: x[1:], axis=1) ranked_df_body = ranked_df_body.apply(lambda x: ss.rankdata(x, method='min'), axis=1) print len(ranked_df_body) ranked_df_body['date'] = unstacked_total['date'] ranked_unstacked_total = ranked_df_body my_total_ranked_line = pd.melt(ranked_unstacked_total, id_vars=['date']) print ggplot(aes(x='date', y='value', colour="name"), data=my_total_ranked_line) + \ scale_x_date(breaks=date_breaks('6 months'), labels='%b %Y') + \ geom_line(size=3) + \ ggtitle('Ranking over Time') ### # pt.3 ### import plotly.plotly as py import plotly.tools as tls from plotly.graph_objs import * from credentials import * py.sign_in(py_u, py_p) plot = ggplot(aes(x='date', y='value', colour="name"), data=my_total_ranked_line) + \ scale_x_date(breaks=date_breaks('6 months'), labels='%b %Y') + \ geom_line(size=3) + \ ggtitle('Ranking over Time') fig = plot.draw() update = {'layout':{'showlegend':True, 'legend':Legend({'x':90}), 'font':{'size':20}}} py.iplot_mpl(fig, update=update)
import matplotlib.pyplot as plt import numpy as np import seaborn as sns import plotly.plotly as py get_ipython().magic("matplotlib inline") def sigmoid(z): return 1.0 / (1.0 + np.exp(-z)) z = np.arange(-7, 7, 0.1) phi_z = sigmoid(z) plt.plot(z, phi_z) plt.axvline(0.0, color="k") plt.axhspan(0.0, 1.0, facecolor="1.0", alpha=1.0, ls="dotted") plt.axhline(y=0.5, ls="dotted", color="k") plt.yticks([0.0, 0.5, 1.0]) plt.ylim(-0.1, 1.1) plt.xlabel("z") plt.ylabel("$\phi (z)$") fig = plt.gcf() # In[9]: py.iplot_mpl(fig, strip_style=True, filename="sigmoid function")
def plotly_plot( x=None, y=None, df=None, style="scatter", title=None, filename="plotly_plot_test", xlabel=None, ylabel=None, vlines=None, hlines=None, xlim=(None, None), ylim=(None, None), dropna=True, dateextractX=False, dateextractY=False, figsize=(16, 9), plotlyplot=True, saveAs=None, **kwargs ): """Interactively plots a series or two in plotly. Must set unique `title` or `filename` for plot to exist semi-permanently, else overwritten on the next use of function. Parameters ---------- x : array, series, or list OR column name in df numpy array, pandas series, or list of primary values to plot y : array, series, or list OR column name in df numpy array, pandas series, or list of secondary values to plot df : pandas DataFrame, optional if given, uses dataframe to create x and y arrays using x and y column names given style : string argument to choose style of plot. currently implemented dist, hist, line, and scatter title, xlabel, ylabel : string, optional labels of plot. if filename not given, filename = title vlines, hlines : int or list, optional list of x/y points to make vertical/horizontal lines in the plot xlim, ylim : tuple (min, max), optional horizontal/vertical boundries of the figure dropna : boolean, optional (default is True) drop nans from series dateextractX, dateextractY : boolean, optional (default is False) try to convert x and y to datetimes using utils.date.extract_date plotlyplot: boolean, optional (default is True) set to False for prototyping filename: string, optional file name on server for plotly plot. unique label ensures plot will not be overwritten saveAs : string (default is None) If given, save the figure using saveAs as the file name. kwargs : dict additional keyworded arguments passed to plotting functions Returns ------- iframe : iframe Notes ----- ToDo - Fix autolabeling. broked it when fixing dates.. """ fig = plt.figure(figsize=figsize) ax = fig.add_subplot(1, 1, 1) yisNone = False if y is None: yisNone = True # if dataframe provided, create x (and y) array(s) if df is not None: x = df[x] if not yisNone: y = df[y] # checking if x (and y) are pandas Series, if not convert to if not isinstance(x, pd.Series): x = pd.Series(x) else: if xlabel is None: xlabel = x.name if not yisNone: if not isinstance(y, pd.Series): y = pd.Series(y) else: if ylabel is None: ylabel = y.name # if dropna, drop nan values from x (and accompanying values in y) if dropna: try: nan_indices = pd.isnull(x) if sum(nan_indices) > 0: print "nan values in series to be dropped:", sum(nan_indices) x = x[~nan_indices].reset_index(drop=True) if not yisNone: y = y[~nan_indices].reset_index(drop=True) except: pass # if y not provided: set y to x.index, swap x and y as we are interested # in plotting x on the 'y' against the index if yisNone: y = x.index x, y = y, x # try to extract_date x and y if dateextractX: try: x = utils.date.extract_date(x) print "date extracted x" except: pass if dateextractY and not yisNone: try: y = utils.date.extract_date(y) print "date extracted y" except: pass # dist or hist: distribution of x plot if style == "dist": try: sns.distplot(y, **kwargs) except: print "failed producing seaborn distribution plot.. trying hist" plt.hist(y, **kwargs) if ylabel is None: ylabel = "frequency" elif style == "hist": plt.hist(y, **kwargs) if ylabel is None: ylabel = "frequency" # line or scatter: x vs y plot elif style == "line": plt.plot(x, y, **kwargs) elif style == "scatter": plt.scatter(x, y, **kwargs) else: print "style currently not available" return None if ylim[0] is None: y_min = plt.ylim()[0] else: y_min = ylim[0] if ylim[1] is None: y_max = plt.ylim()[1] else: y_max = ylim[1] plt.ylim(y_min, y_max) if xlim[0] is None: x_min = plt.xlim()[0] else: x_min = xlim[0] if xlim[1] is None: x_max = plt.xlim()[1] else: x_max = xlim[1] plt.xlim(x_min, x_max) # vlines, hlines. should maybe export this to their own function for other # uses if vlines is not None: if not isinstance(vlines, (list, pd.core.series.Series, np.ndarray)): vlines = [vlines] for vl in vlines: plt.plot([vl, vl], [y_min, y_max]) if hlines is not None: if xlim is None: xlim = plt.xlim() if not isinstance(hlines, (list, pd.core.series.Series, np.ndarray)): hlines = [hlines] for hl in hlines: plt.plot([x_min, x_max], [hl, hl]) # title, filename handling if (title is None) and (filename != "plotly_plot_test"): title = filename if title is not None: plt.title(title, size=20) # if title is set and filename is default, set filename to title if filename == "plotly_plot_test": filename = title # x and y label handling. auto labeling hashed out for now if xlabel is not None: plt.xlabel(xlabel, size=18) if ylabel is not None: plt.ylabel(ylabel, size=18) if saveAs is not None: plt.savefig(saveAs, bbox_inches="tight", dpi=270) # render in plotly or return nothing to output static chart if plotlyplot: iframe = py.iplot_mpl(fig, filename=filename, fileopt="overwrite") print iframe.embed_code.split('src="')[1].split(".emb")[0] py.iplot_mpl(fig, filename=filename, fileopt="overwrite") return iframe
print rmax10s print vel10s bin_means, bin_edges, binnumber = scipy.stats.binned_statistic(rmax10s, vel10s, statistic='mean', bins=4) #print bin_means #print bin_edges plt.hlines(bin_means, bin_edges[:-1], bin_edges[1:], colors='g', lw=5, label='binned statistic of data') #digitizedrmax10s = np.digitize(rmax10s, bins) #print digitizedrmax10s #bin_meansrmax10s = [rmax10s[digitizedrmax10s == i].mean() for i in range(1, len(bins))] #print bin_meansrmax10s #digitizedvel10s = np.digitize(vel10s, bins) #bin_meansvel10s = [vel10s[digitizedvel10s == i].mean() for i in range(1, len(bins))] #plt.loglog(digitizedrmax10s, digitizedvel10s, '-', alpha=0.08, label='mean 10th percentile') plt.loglog(rmax10s, vel10s, 'o', alpha=0.08, label='10th-50th host mass percentile') plt.loglog(rmax50s, vel50s, 'o', alpha=0.08, label='50th-90th host mass percentile') plt.loglog(rmax90s, vel90s, 'o', alpha=0.08, label='>=90th host mass percentile') plt.loglog(rmaxxs, velxs, 'o', alpha=0.08, label='<10th host mass percentile') plt.xlim(xmin=0.38, xmax=50) plt.ylim(ymin=10.0, ymax=200) plt.xlabel('Rmax (kpc)') plt.ylabel('Vmax (km/s)') plt.title('Rmax-Vmax by Host Mass') #spearman = scipy.stats.spearmanr(rmaxs, vmaxs) #print spearman plot_mpl_fig() mpl_fig1 = plt.gcf() py_fig1 = tls.mpl_to_plotly(mpl_fig1, verbose=True) py.iplot_mpl(mpl_fig1, strip_style=True, filename='Rmax-Vmax Percentile') py_fig1_ss = tls.mpl_to_plotly(mpl_fig1, strip_style=True) py.plot(py_fig1_ss, filename='Rmax-Vmax Percentile')
ax.tick_params(axis="both", which="both", bottom="off", top="off", labelbottom="on", left="off", right="off", labelleft="on", labelsize=16) ax.set_ylim(-5, 60) ax.set_xlim(-5, 100) ax.set_ylabel('% HH in Poverty', fontsize=fontsize) ax.set_xlabel('% Population', fontsize=fontsize) # Matplotlib code is very long... But sometimes you have existing matplotlib code, right? The good news is, plotly can eat it! # In[25]: py.iplot_mpl(mpl_fig, filename='baltimore-poverty') # So, at the moment, matplotlib legends do not fully convert to plotly legends (please refer to our [user guide](https://plot.ly/python/matplotlib-to-plotly-tutorial/#Careful,-matplotlib-is-not-perfect-%28yet%29)). Let's tweak this now. # In[26]: import plotly.tools as tls # In[27]: # Convert mpl fig object to plotly fig object, resize to plotly's default. py_fig = tls.mpl_to_plotly(mpl_fig, resize=True)