def PlotDegreeDistribution(G):
    N = len(G.nodes())
    nodesDegrees = [G.degree()[i] for i in G.nodes()]
    mean_degree = sum(nodesDegrees)/len(nodesDegrees)
      
    # You typically want your plot to be ~1.33x wider than tall.  
    # Common sizes: (10, 7.5) and (12, 9)  
    plt.figure(figsize=(12, 9))  
      
    # Remove the plot frame lines. They are unnecessary chartjunk.  
    ax = plt.subplot(111)  
    ax.spines["top"].set_visible(False)  
    ax.spines["right"].set_visible(False)  
      
    ax.get_xaxis().tick_bottom()
    ax.get_yaxis().tick_left()
      
    plt.xticks(fontsize=14)
    plt.yticks(fontsize=14)
      
    plt.xlabel("Degree", fontsize=16)
    plt.ylabel("Frequency", fontsize=16)
    plt.hist(nodesDegrees,  color="#3F5D7D") #, bins = [x for x in range(max(nodesDegrees))]) #, bins=100)  
    
    xRange = range(max(nodesDegrees)) 
    h = plt.plot(xRange, [m.e**(-mean_degree)*(mean_degree**x)*N/m.factorial(x) for x in xRange], lw=2)

    mpld3.show();  
Exemple #2
0
    def plot(self, notebook=False, colormap='polar', scale=1, maptype='points', show=True, savename=None):

        # make a spatial map based on the scores
        fig = pyplot.figure(figsize=(12, 5))
        ax1 = pyplot.subplot2grid((2, 3), (0, 1), colspan=2, rowspan=2)
        if maptype is 'points':
            ax1, h1 = pointmap(self.scores, colormap=colormap, scale=scale, ax=ax1)
        elif maptype is 'image':
            ax1, h1 = imagemap(self.scores, colormap=colormap, scale=scale, ax=ax1)
        fig.add_axes(ax1)

        # make a scatter plot of sampled scores
        ax2 = pyplot.subplot2grid((2, 3), (1, 0))
        ax2, h2, samples = scatter(self.scores, colormap=colormap, scale=scale, thresh=0.01, nsamples=1000, ax=ax2, store=True)
        fig.add_axes(ax2)

        # make the line plot of reconstructions from principal components for the same samples
        ax3 = pyplot.subplot2grid((2, 3), (0, 0))
        ax3, h3, linedata = tsrecon(self.comps, samples, ax=ax3)

        plugins.connect(fig, LinkedView(h2, h3[0], linedata))
        plugins.connect(fig, HiddenAxes())

        if show and notebook is False:
            mpld3.show()

        if savename is not None:
            mpld3.save_html(fig, savename)
        elif show is False:
            return mpld3.fig_to_html(fig)
Exemple #3
0
def show_plots(plotengine, ax=None, output_dir=None):
    if plotengine == 'mpld3':
        import mpld3
        mpld3.show()

    elif plotengine == 'matplotlib':
        import matplotlib.pyplot as plt

        if not output_dir:  # None or ""
            plt.show()
        else:
            for fi in plt.get_fignums():
                plt.figure(fi)
                fig_name = getattr(plt.figure(fi), 'name', 'figure%d' % fi)
                fig_path = op.join(output_dir, '%s.png' % fig_name)
                if not op.exists(op.dirname(fig_path)):
                    os.makedirs(op.dirname(fig_path))
                plt.savefig(fig_path)
                plt.close()

    elif plotengine in ['bokeh', 'bokeh-silent']:
        import bokeh.plotting
        import tempfile
        output_dir = output_dir or tempfile.mkdtemp()
        output_name = getattr(ax, 'name', ax.title).replace(':', '-')
        output_file = op.join(output_dir, '%s.html' % output_name)

        if not op.exists(output_dir):
            os.makedirs(output_dir)
        if op.exists(output_file):
            os.remove(output_file)
        bokeh.plotting.output_file(output_file, title=ax.title, mode='inline')
        if plotengine == 'bokeh':
            bokeh.plotting.show(ax)
Exemple #4
0
def showNetwork(struct, weights, labels):
    nbLayers  = len(struct)
    networkXY = []
    colors = ['b', 'r']
    for layer in range(nbLayers):
        layerXY = []
        if layer != 4:
            sumWeightsNeuron = np.sum(np.abs(weights['layer_' + str(layer)]['param_0']), axis=1)
            maxSumWeights = np.max(sumWeightsNeuron)
        for neuron in range(struct[layer]):
            neuronXY = (layer*10, neuron-(struct[layer]-1)/2.)
            if layer != 4 :
                inputScatters = plt.scatter(neuronXY[0],neuronXY[1], alpha=(sumWeightsNeuron[neuron]/maxSumWeights)**2)
            else :
                inputScatters = plt.scatter(neuronXY[0],neuronXY[1], alpha=1)
            layerXY.append(neuronXY)
        networkXY.append(layerXY)
        tooltip = mpld3.plugins.PointLabelTooltip(inputScatters, labels=labels)
        
        if layer != 0:
            print(weights['layer_' + str(layer-1)]['param_0'].value)
            maxWeights = np.amax(np.abs(weights['layer_' + str(layer-1)]['param_0']))
            for neuronLayer in range(struct[layer]):
                for neuronLayerP in range(struct[layer-1]):
                    print(layer, neuronLayer, neuronLayerP, maxWeights)
                    
                    plt.plot([networkXY[layer][neuronLayer][0],networkXY[layer-1][neuronLayerP][0]],
                             [networkXY[layer][neuronLayer][1],networkXY[layer-1][neuronLayerP][1]],
                             #alpha=1-np.exp(-((weights['layer_' + str(layer-1)]['param_0'][neuronLayerP][neuronLayer])/3)**2)
                             alpha = (weights['layer_' + str(layer-1)]['param_0'][neuronLayerP][neuronLayer] / maxWeights)**2,
                             c = colors[int(weights['layer_' + str(layer-1)]['param_0'][neuronLayerP][neuronLayer] > 0)])
    mpld3.show()
Exemple #5
0
    def plot(self, data, notebook=False, show=True, savename=None):

        fig = pyplot.figure()
        ncenters = len(self.centers)

        colorizer = Colorize()
        colorizer.get = lambda x: self.colors[int(self.predict(x)[0])]

        # plot time series of each center
        # TODO move into a time series plotting function in viz.plots
        for i, center in enumerate(self.centers):
            ax = pyplot.subplot2grid((ncenters, 3), (i, 0))
            ax.plot(center, color=self.colors[i], linewidth=5)
            fig.add_axes(ax)

        # make a scatter plot of the data
        ax2 = pyplot.subplot2grid((ncenters, 3), (0, 1), rowspan=ncenters, colspan=2)
        ax2, h2 = scatter(data, colormap=colorizer, ax=ax2)
        fig.add_axes(ax2)

        plugins.connect(fig, HiddenAxes())

        if show and notebook is False:
            mpld3.show()

        if savename is not None:
            mpld3.save_html(fig, savename)

        elif show is False:
            return mpld3.fig_to_html(fig)
Exemple #6
0
    def graphme(self, pngfilename="my_sample_png.png"):

        import numpy as np
        import matplotlib.pyplot as plt
        import matplotlib.dates as mdates
        import mpld3
        import datetime

        """ creating background info"""
        # create a plot with as may subplots as you choose
        fig, ax = plt.subplots()
        # add a grid to the background
        ax.grid(True, alpha = 0.2)
        # the x axis contains date
        fig.autofmt_xdate()
        # the dates are year, month
        ax.fmt_xdata = mdates.DateFormatter('%Y-%m')

        if self.table not in ['MS04314', 'MS00114', 'MS04334','MS04315','MS00115']:
            final_glitch = self.decide()

            dates = sorted(final_glitch.keys())
            dates2 = [x for x in dates if final_glitch[x]['mean'] != None and final_glitch[x]['mean'] != "None"]
            vals = [final_glitch[x]['mean'] for x in dates2]
            glitched_values = ax.plot(dates2, vals, 'b-')
            ax.legend(loc=4)
            ax.set_xlabel("dates")
            ax.set_ylabel("values")
            mpld3.show()
            mpld3.save_html(fig, 'my_output_html.html')
            import pylab
            pylab.savefig(pngfilename)
Exemple #7
0
 def after(self):
     if self.draw:
         plugins.connect(
             self.fig, plugins.InteractiveLegendPlugin(
                 self.s1, self.labels, ax=self.ax))
         mpld3.show()
     else:
         pass
Exemple #8
0
def makeFig():
	plt.ylim(20,80)
	plt.title('Temperature Streaming')
	plt.grid(True)
#	plt.ylable('Temp C')
	plt.plot(tempC, 'ro-',label='Degree C')
	plt.legend(loc='upper left')
#	pyplot.show_bokeh(plt.gcf(), filename="mpltest.html")
#	plotting.session().dumpjson(file="mpltest.json")
        mpld3.show()
	def do_show(self):

		#debug
		self.debug(
			[
				'We show here',
				'first network'
			]
		)

		#/##################/#
		# First network
		#

		#network all the view things
		self.network(
			[
				'Views',
				'Panels',
				'Axes',
				'Plots'
			],
			_DoStr='Show'
		)

		#/##################/#
		# Then show the figure
		#

		#debug
		'''
		self.debug(
				[
					'We show with which device',
					('self.',self,['ShowingQtBool'])
				]
			)
		'''

		#Check
		if self.ShowingQtBool:

			#import
			from matplotlib import pyplot

			#show
			pyplot.show()

		if self.ShowingMpld3Bool:

			#import
			import mpld3

			#show
			mpld3.show()
def test_interactive_shallowPP(save_to_html=False):
    # Define left and right state (h,hu)
    ql = np.array([3.0, 5.0])
    qr = np.array([3.0, -5.0])
    # Define optional parameters (otherwise chooses default values)
    plotopts = {'g':1.0, 'time':2.0, 'tmax':5, 'hmax':10, 'humin':-15, 'humax':15}
    # Call interactive function (can be called without any argument)
    pt = shallow_water(ql,qr,**plotopts)
    if save_to_html:
        mpld3.save_html(pt, "test_shallow.html")
    mpld3.show()
Exemple #11
0
def plotter(filelist, entlist, fsizelist):
    # gets the file and entropy lists and plots the data to a neat line graph
    xtimes = [datetime.datetime.strptime(str(int(times)), '%H%M%S') for times in filelist]
    plt.plot(fsizelist, entlist, marker='o', color='green')
    plt.plot(xtimes, entlist, marker='o')
    plt.xlabel('Time')
    plt.ylabel('Entropy')
    plt.title('Entropy over time for date')

    plt.show()
    mpld3.show()
    return
def main(file_name='Greenland1km.nc'):
    '''Description'''

    # Set up the file and projection
    data = os.path.dirname(os.path.abspath(__file__)) + os.sep + '..' + os.sep \
            + 'data' + os.sep + file_name
    proj_file = pyproj.Proj('+proj=stere +ellps=WGS84 +datum=WGS84 +lat_ts=71.0 +lat_0=90 ' \
            + '+lon_0=321.0 +k_0=1.0')
    proj_lat_long = pyproj.Proj('+proj=latlong +ellps=WGS84 +datum=WGS84')
    fig, ax = plt.subplots(1,2)

    # Open up the file and grab the data we want out of it
    greenland = Dataset(data)
    x = greenland.variables['x'][:]
    y = greenland.variables['y'][:]
    nx = x.shape[0]
    ny = y.shape[0]
    y_grid, x_grid = scipy.meshgrid(y[:], x[:], indexing='ij')
    thk = greenland.variables['thk'][0]
    bheatflx = greenland.variables['bheatflx'][0]

    # Now transform the coordinates to the correct lats and lons
    lon, lat = pyproj.transform(proj_file, proj_lat_long, x_grid.flatten(), y_grid.flatten())
    lat = lat.reshape(ny,nx)
    lon = lon.reshape(ny,nx)

    # Put thickness in a basemap
    mapThk = Basemap(projection='stere',lat_0=65, lon_0=-25,\
                llcrnrlat=55,urcrnrlat=85,\
                llcrnrlon=-50,urcrnrlon=0,\
                rsphere=6371200.,resolution='l',area_thresh=10000, ax=ax[0])
    mapThk.drawcoastlines(linewidth=0.25)
    mapThk.fillcontinents(color='grey')
    mapThk.drawmeridians(np.arange(0,360,30))
    mapThk.drawparallels(np.arange(-90,90,30))
    x, y = mapThk(lon,lat)
    cs = mapThk.contour(x, y, thk, 3)

    # Put basal heat flux in a basemap
    mapFlx = Basemap(projection='stere',lat_0=65, lon_0=-25,\
                llcrnrlat=55,urcrnrlat=85,\
                llcrnrlon=-50,urcrnrlon=0,\
                rsphere=6371200.,resolution='l',area_thresh=10000, ax=ax[1])
    mapFlx.drawcoastlines(linewidth=0.25)
    mapFlx.fillcontinents(color='grey')
    mapFlx.drawmeridians(np.arange(0,360,30))
    mapFlx.drawparallels(np.arange(-90,90,30))
    x, y = mapFlx(lon,lat)
    cs = mapFlx.contour(x, y, bheatflx, 3)

    plugins.connect(fig, ClickInfo(cs))
    mpld3.show()
Exemple #13
0
    def plot_engine_timing(self):
        """
        """
        #Import seaborn to prettify the graphs if possible
        try:
            import seaborn
        except:
            pass
        try:
            import matplotlib.pyplot as plt

            width = 0.35

            s = self.timing['engines'].values()
            names = self.timing['engines'].keys()
            plt_axes = []
            plt_axes_offset = []
            for i, n in enumerate(names):
                plt_axes.append(i)
                plt_axes_offset.append(i + 0.15)

            fig, ax = plt.subplots()

            rects1 = ax.bar(plt_axes, s, width, color='r')
            ax.set_xticks(plt_axes_offset)
            ax.set_xticklabels(list(names))
            ax.set_ylabel('Time')
            ax.set_xlabel('Engine')
            plt.title('Timing')

            try:
                import mpld3
                i = 0
                for r in rects1:
                    tooltip = mpld3.plugins.LineLabelTooltip(r, label=names[i])
                    mpld3.plugins.connect(fig, tooltip)
                    i = i + 1
                mpld3.show()
            except Exception as e:
                logging.exception(e)
                logging.warn("For tooltips, install mpld3 (pip install mpld3)")
                plt.show(block=True)

        except ImportError:
            logging.critical("Cannot plot. Please ensure matplotlib "
                             "and networkx are installed.")
def test_interactive_linearPP(save_to_html=False):
    ## Define left and right state 
    ql = np.array([-2.0, 2.0]) 
    qr = np.array([0.0, -3.0])
    # Define two eigenvectors and eigenvalues (acoustics)
    zz = 2.0
    rho0 = 1.0
    r1 = np.array([zz,1.0])
    r2 = np.array([-zz,1.0])
    lam1 = zz/rho0
    lam2 = -zz/rho0
    plotopts={'q1min':-5, 'q1max':5, 'q2min':-5, 'q2max':5, 'domain':5, 'time':1, 
            'title1':"Pressure", 'title2':"Velocity"}
    pt = linear_phase_plane(ql,qr,r1,r2,lam1,lam2,**plotopts)
    if save_to_html:
        mpld3.save_html(pt, "test_linearPP.html")
    mpld3.show()
def main():
    # Open the eigenworms file    
    features_path = os.path.dirname(mv.features.__file__)
    eigenworm_path = os.path.join(features_path, mv.config.EIGENWORM_FILE)
    eigenworm_file = h5py.File(eigenworm_path, 'r')
    
    # Extract the data
    eigenworms = eigenworm_file["eigenWorms"].value

    eigenworm_file.close()

    # Print the shape of eigenworm matrix
    print(np.shape(eigenworms))

    # Plot the eigenworms
    for eigenworm_i in range(np.shape(eigenworms)[1]):
        plt.plot(eigenworms[:,eigenworm_i])
    mpld3.show()
def plot_d3js():
    # Define some CSS to control our custom labels
    css = """
    table
    {
      border-collapse: collapse;
    }
    th
    {
      color: #ffffff;
      background-color: #000000;
    }
    td
    {
      background-color: #cccccc;
    }
    table, th, td
    {
      font-family:Arial, Helvetica, sans-serif;
      border: 1px solid black;
      text-align: right;
    }
    """

    fig, ax = plt.subplots()
    ax.grid(True, alpha=0.3)
    labels = suburb_list
    x = coords[:, 0]
    y = coords[:, 1]

    points = ax.plot(x, y, 'o', color='b',
                     mec='k', ms=15, mew=1, alpha=.6)

    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_title('Ethnicity', size=20)

    tooltip = plugins.PointHTMLTooltip(points[0], labels,
                                       voffset=10, hoffset=10, css=css)
    plugins.connect(fig, tooltip)

    mpld3.show()
def plot_team(sorted_team, sorted_teamgames):

    fig = plt.figure(figsize=(12, 12))
    #ax = fig.add_axes([0.15, 0.1, 0.7, 0.7])
    grid = ImageGrid(fig, 
                     (0.2, 0.15, 0.8, 0.8),
                     #111, 
                     nrows_ncols=(1, 1),
                     direction='row', axes_pad=0.05, add_all=True,
                     label_mode='1', share_all=False,
                     cbar_location='right', cbar_mode='single',
                     cbar_size='5%', cbar_pad=0.05)

    ax = grid[0]
    ax.set_title('Game lead (each team is a column)', fontsize=20)
    ax.tick_params(axis='both', direction='out', labelsize=12)
    #im = ax.imshow(df.values, interpolation='nearest', vmax=df.max().max(),
    #               vmin=df.min().min(), cmap='RdBu')
    im = ax.imshow(sorted_teamgames.values, interpolation='nearest', vmax=120, vmin=-120, cmap='RdBu')
    #colorbar
    ax.cax.colorbar(im)
    ax.cax.tick_params(labelsize=12)

    ax.set_xticks(np.arange(sorted_teamgames.shape[1]))
    ax.set_xticklabels(sorted_team, rotation='vertical', fontweight='bold')
    ax.set_yticks(np.arange(sorted_teamgames.shape[0]))
    ax.set_yticklabels(sorted_teamgames.index)

    ax.set_ylabel("Sorted game", size=16)
    #plt.show()
    plotid = mpld3.utils.get_id(ax)


    print sorted_team
    ax_ori = RotateTick(0, sorted_team.tolist(), -90, 1,1)
    mpld3.plugins.connect(fig, ax_ori)
    mpld3.show()

    fightml = mpld3.fig_to_html(fig)
    return plotid, fightml
Exemple #18
0
def make_optional_graphs(wd):
    """ A function that can be called to graph the difference method against the ratio method if necessary.

    Do not need in production,  but used for testing on 10-1-2015 to see if we can get to the values working as Don expects
    :wd: the dictionary containing adjustments etc used to figure out the final csv 
    """

    if wd != {} and wd[wd.keys()[0]]['adj_diff'] != None and wd[wd.keys()[0]]['adj_rat'] != None:

        datelist = [x for x in sorted(wd.keys()) if wd[x]['adj_rat'] != None and wd[x]['adj_diff'] != None]
        val_diff = [wd[x]['adj_diff'] for x in datelist]
        val_rat = [wd[x]['adj_rat'] for x in datelist]

        fig, ax = plt.subplots()
        fig.autofmt_xdate()
        ax.fmt_xdata = mdates.DateFormatter('%Y-%m')
        ax.plot(datelist, val_diff, color = 'blue', linewidth= 1.2, alpha = 0.5, label = 'diff method')
        ax.plot(datelist, val_rat, color = 'red', linewidth= 0.7, label = 'ratio method')
        #ax.scatter(mainte_dates, maintes, s=30, c='red', alpha = 0.4, label='MAINTE')
        ax.legend(loc = 1)

        mpld3.show()
Exemple #19
0
def plot_user_sep(user_data, title=None, smoothing=4):
    '''
    for a user vector of shape (plans, timesteps)
    generate a plot over time for each plan. If a title is provided
    the value of the sorting criterion will be included in it
    :param user_data:
    :param title:
    :param smoothing: size of movind average smoothing
    :return:
    '''
    fig, pltgrid = plt.subplots(5, 2, figsize=(15, 15))
    fig.suptitle(title or "title", fontsize=20)
    criterion = eval_stats_row(user_data)
    for i in range(np.shape(user_data)[0]):
        pltgrid[i // 2, i % 2].plot(moving_average(user_data[i, :], smoothing))
        row = i // 2
        col = i % 2
        title2 = str(row) + "," + str(col) + " "
        if title is not None:
            title2 = title2 + title + " : " + str(criterion[i])
        pltgrid[i // 2, i % 2].set_title(title2)
        #pltgrid[i//2, i%2].set_title(cf.SORTING_CRIT + " " + str(criterion[indeces[i]]))
    fig.subplots_adjust(hspace=1.3)
    mpld3.show()
Exemple #20
0
    def fig_show(self, fig):
        """

        Opens the figure in a web browser
    
        Parameters
        ----------
        fig : Matplotlib Figure
            the input Matplotlib Figure
    
        Returns
        -------
        browser containing the figure
        """
        return mpld3.show(fig)
Exemple #21
0
    def graphme(self, pngfilename="my_sample_png.png"):

        import numpy as np
        import matplotlib.pyplot as plt
        import matplotlib.dates as mdates
        import mpld3
        import datetime
        """ creating background info"""
        # create a plot with as may subplots as you choose
        fig, ax = plt.subplots()
        # add a grid to the background
        ax.grid(True, alpha=0.2)
        # the x axis contains date
        fig.autofmt_xdate()
        # the dates are year, month
        ax.fmt_xdata = mdates.DateFormatter('%Y-%m')

        if self.table not in [
                'MS04314', 'MS00114', 'MS04334', 'MS04315', 'MS00115'
        ]:
            final_glitch = self.decide()

            dates = sorted(final_glitch.keys())
            dates2 = [
                x for x in dates if final_glitch[x]['mean'] != None
                and final_glitch[x]['mean'] != "None"
            ]
            vals = [final_glitch[x]['mean'] for x in dates2]
            glitched_values = ax.plot(dates2, vals, 'b-')
            ax.legend(loc=4)
            ax.set_xlabel("dates")
            ax.set_ylabel("values")
            mpld3.show()
            mpld3.save_html(fig, 'my_output_html.html')
            import pylab
            pylab.savefig(pngfilename)
Exemple #22
0
def draw(ins, date, result):
    path = cfg.path[AShare().get_type(ins)]
    fn = '{path}/{date}/tickab_{stock}.{date}'.format(path=path,
                                                      date=date,
                                                      stock=ins)
    with open(fn, 'rb') as f:
        df = get_tickab(f)
    deals = pd.DataFrame(result['deals'])
    deals['nt'] = deals['dt'].apply(lambda x: ((
        (x.hour * 100 + x.minute) * 100) + x.second) * 1000).astype('int32')

    deals = deals.set_index('nt')
    deals = deals.reindex(df.nTime.unique(), method='backfill',
                          limit=1).dropna()
    df = df.join(deals, on='nTime')

    t = df['nTime']
    x = list(df.index)
    p = df['nPrice']
    b = df['avg_price'].mask(df['bsflag'] != 'buy', None).dropna()
    s = df['avg_price'].mask(df['bsflag'] != 'sell', None).dropna()

    plt.figure(figsize=(20, 10), dpi=80)
    plt.xticks(
        x[::600] + x[-1:],
        ['%d:%02d' % (i / 10000000, i / 100000 % 100)
         for i in t[::600]] + ['15:00'])
    plt.yticks(np.linspace(min(p), max(p), 10, endpoint=True))
    plt.ylim(min(p), max(p))

    plt.grid(True, 'major', 'x')
    plt.scatter(list(b.index), b, 50, color='green')
    plt.scatter(list(s.index), s, 50, color='red')
    plt.plot(x, p)

    mpld3.show(ip='192.168.128.21', port=8887)
def plot_with_tsne(labels, embeddings, display_hover=True):
    """
    expects a list of email_ids and numpy ndarray of embeddings. The numpy ndarray should have shape L,D where D is the
    size of embeddings and L is the number of users
    """
    tsne = TSNE(verbose=1, method='exact')
    start = time.time()
    tsne_embs = tsne.fit_transform(embeddings)
    end = time.time()
    print('time taken by TSNE ', (end - start))

    # creating the colors
    colors = list(sb.color_palette().as_hex())
    color_list = _assign_labels_colors(labels, colors)

    fig, ax = plt.subplots()
    scatter = ax.scatter(tsne_embs[:, 0], tsne_embs[:, 1], c=color_list, s=75)
    if display_hover:
        tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=labels)
        mpld3.plugins.connect(fig, tooltip)
        mpld3.show()
    else:
        outfile = '../outputs/' + constants.RUN_ID + '_tsne-users.png'
        plt.savefig(outfile)
Exemple #24
0
def graph_func():

    columns = defaultdict(list)

    with open('data.csv', 'r', encoding='utf8') as f:
        reader = csv.reader(f, delimiter=',')
        for row in reader:
            for i in range(len(row)):
                columns[i].append(row[i])
        columns = dict(columns)

    # print(columns)

    allWords = []

    for comment in columns[1]:
        words = comment.split()
        for word in words:
            allWords.append(word)
            # print(word)

    allWords = map(str.lower, allWords)

    counter = collections.Counter(allWords)
    # print(counter)
    # print(counter.keys())
    # print(counter.values())

    x = list(counter.keys())[0:10]
    y = list(counter.values())[0:10]
    plt.title("Top 10 Words vs #times used")
    plt.xlabel("words")
    plt.ylabel("#times")
    plt.bar(x, y)

    mpld3.show()
Exemple #25
0
    ax.plot((0, pix.shape[1]), (y0, y1), '-r')

# plot rects kmeans result
for x in xs:
    ax.plot((x, x), (0, pix.shape[0]), '-b')
for y in ys:
    ax.plot((0, pix.shape[1]), (y, y), '-b')

# plot nodes found by clustering
ax.plot(nodes_coords[:, 0], nodes_coords[:, 1], 'ro')

# plot corridors nodes found by dotsKmeans
for (x, y) in cluster_coords:
    ax.plot(x, y, 'g^')

# plot whole gragh
for i, (x, y) in enumerate(graph_coords):
    plotted = []
    for j in g_whole[i]:
        xj, yj = graph_coords[j]
        ax.plot((x, xj), (y, yj), '--y')

ax.set_xlim((0, pix.shape[1]))
ax.set_ylim((pix.shape[0], 0))
ax.set_axis_off()
ax.imshow(pix, cmap=plt.cm.gray)
ax.set_title('Input image')

plt.tight_layout()
mpld3.show(open_browser=False, ip='0.0.0.0', port=8000)
Exemple #26
0
def run(offset, window):

    with open('record.json') as fd:
        data = json.load(fd, object_pairs_hook=OrderedDict)

    data = OrderedDict(reversed(list(data.items())))

    # Fixing random state for reproducibility
    np.random.seed(0)

    plt.rcdefaults()
    fig, ax = plt.subplots()

    # team member names
    team = [data[x]['info']['nick_name'] for x in data]
    y_pos = np.arange(len(team))

    # create 2-D array of tasks
    tasks = [data[x]['tasks'] for x in data]
    max_len = max([len(t) for t in tasks])
    min_date = min(
        [data[x]['tasks'][0]['start'] for x in data if data[x]['tasks']])
    null_task = {
        'name': '',
        'brief': '',
        'debrief': '',
        'start': min_date,
        'end': min_date,
        'target': min_date
    }
    tasks = [t + [null_task] * (max_len - len(t)) for t in tasks]
    layers = list(map(list, zip(*tasks)))

    bars = []

    for layer in layers:
        start = [
            datetime.strptime(x['start'], '%Y-%m-%d').date() for x in layer
        ]
        target = [
            datetime.strptime(x['target'], '%Y-%m-%d').date() for x in layer
        ]
        end = [
            datetime.strptime(x['end'], '%Y-%m-%d').date()
            if x['end'] else datetime.strptime(x['start'], '%Y-%m-%d').date()
            for x in layer
        ]

        left = [(x - date.today()).days for x in start]
        target_width = [(x[1] - x[0]).days for x in zip(start, target)]
        actual_width = [(x[1] - x[0]).days for x in zip(start, end)]

        ax.barh(
            y_pos,
            target_width,
            left=left,
            edgecolor='white',
            linewidth=2,
            color='#85aa00',
            height=0.6,
        )
        ax.barh(
            y_pos,
            actual_width,
            left=left,
            edgecolor='white',
            linewidth=2,
            color='#3b3b3b',
            height=0.3,
        )
        for i, task in enumerate(layer):
            ax.text(
                left[i] + 1,
                i + 0.05,
                task['name'],
                color='white',
                size=8,
                fontweight='bold',
            )
        # transparent bar for hovering
        bars.append(
            ax.barh(
                y_pos,
                target_width,
                left=left,
                linewidth=2,
                height=0.6,
                zorder=1000,
                alpha=0.0,
            ))

    ax.set_yticks(y_pos)
    ax.set_yticklabels(team)
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.set_xlim(
        [offset - PROPORTION * window, offset + (1 - PROPORTION) * window])

    def make_text(task):
        text = """
        <div style="background-color:#ffffff; padding:10px; border-style:solid; border-width:1px; border-color:#3b3b3b">
        <h6 style="font-size:10; color:#9b9b9b">{}</h6>
        <p style="font-size:10; color:#9b9b9b"><strong>Brief:</strong> {}</p>
        <div>
        """.format(task['name'], task['brief'])
        # if task['debrief']:
        #     text += '\n\n' + textwrap.fill('Debrief: ' + task['debrief'])
        return text

    for i, layer in enumerate(bars):
        for j, bar in enumerate(layer):
            tooltip = mpld3.plugins.PointHTMLTooltip(
                bar,
                [make_text(layers[i][j])],
                voffset=10,
                hoffset=10,
            )
            mpld3.plugins.connect(fig, tooltip)

    plt.axvline(
        x=-10,
        linewidth=1,
        linestyle='dashed',
        color='#3b3b3b',
        zorder=-1,
    )

    mpld3.show(ip='0.0.0.0', open_browser=False)
    def plot_2d_graph(self, data_frame, gp, type):

        matplotlib.rcdefaults()

        # set the matplotlib style sheet
        plt.style.use(Constants().plotfactory_pythalesians_style_sheet[Constants().plotfactory_default_stylesheet])

        if hasattr(gp, 'style_sheet'):
            plt.style.use(Constants().plotfactory_pythalesians_style_sheet[gp.style_sheet])

        scale_factor = Constants().plotfactory_scale_factor

        if hasattr(gp, 'scale_factor'): scale_factor = gp.scale_factor

        dpi = Constants().plotfactory_dpi

        if hasattr(gp, 'dpi'): dpi = gp.dpi

        width = Constants().plotfactory_width; height = Constants().plotfactory_height

        if hasattr(gp, 'width'): width = gp.width
        if hasattr(gp, 'height'): width = gp.height

        fig = plt.figure(figsize = ((width * scale_factor)/dpi, (height * scale_factor)/dpi), dpi = dpi)

        # add a subplot
        ax = fig.add_subplot(111)

        matplotlib.rcParams.update({'font.size': matplotlib.rcParams['font.size'] * scale_factor})

        # format Y axis
        y_formatter = matplotlib.ticker.ScalarFormatter(useOffset = False)
        ax.yaxis.set_major_formatter(y_formatter)

        y_axis_2_series = []
        ax2 = []
        color_2_series = []
        linewidth_2_series = []

        if hasattr(gp, 'resample'):
            data_frame = data_frame.asfreq(gp.resample)

        # create a second y axis if necessary
        if hasattr(gp, 'y_axis_2_series'):
            if gp.y_axis_2_series == []:
                pass
            else:
                y_axis_2_series = gp.y_axis_2_series
                ax2 = ax.twinx()

                # matplotlib.rcParams.update({'figure.subplot.right': matplotlib.rcParams['figure.subplot.right'] - 0.05})

                # do not use a grid with multiple y axes
                ax.yaxis.grid(False)
                ax2.yaxis.grid(False)

        # is there a second palette?
        if hasattr(gp, 'color_2_series'):
            if hasattr(gp.color_2_series, 'values'):
                color_2_series = [str(x) for x in gp.color_2_series.values]
            else:
                color_2_series = [str(x) for x in gp.color_2_series]

        # is there a second linewidth series
        if hasattr(gp, 'linewidth_2_series'):
            if hasattr(gp.linewidth_2_series, 'values'):
                linewidth_2_series = [str(x) for x in gp.linewidth_2_series.values]
            else:
                linewidth_2_series = [str(x) for x in gp.linewidth_2_series]

        # plot the lines (using custom palettes as appropriate)
        try:
            color = []; color_2 = []
            linewidth_2 = matplotlib.rcParams['axes.linewidth']

            exclude_from_color = []

            if hasattr(gp, 'color'):
                if isinstance(gp.color, list):
                    color = gp.color
                else:
                    try:
                        color = self.create_colormap(
                            len(data_frame.columns.values) - len(color_2_series), gp.color)
                    except: pass

            if hasattr(gp, 'width'):
                if isinstance(gp.width, list):
                    color = gp.color
                else:
                    try:
                        color = self.create_colormap(
                            len(data_frame.columns.values) - len(color_2_series), gp.color)
                    except: pass

            if hasattr(gp, 'color_2'):
                if isinstance(gp.color_2, list):
                    color_2 = gp.color_2
                else:
                    try:
                        color_2 = self.create_colormap(len(color_2_series), gp.color_2)
                    except: pass

            if hasattr(gp, 'exclude_from_color'):
                if not(isinstance(gp.exclude_from_color, list)):
                    gp.exclude_from_color = [gp.exclude_from_color]

                exclude_from_color = [str(x) for x in gp.exclude_from_color]

            if hasattr(gp, 'linewidth_2'):
                linewidth_2 = gp.linewidth_2

            axis_1_color_index = 0
            axis_2_color_index = 0

            if type == 'bar':
                # bottom = np.cumsum(np.vstack((np.zeros(data_frame.values.shape[1]), data_frame.values)), axis=0)[:-1]
                # bottom = np.vstack((np.zeros((data_frame.values.shape[1],), dtype=data_frame.dtype),
                #                    np.cumsum(data_frame.values, axis=0)[:-1]))
                yoff = np.zeros(len(data_frame.index.values)) # the bottom values for stacked bar chart

            # some lines we should exclude from the color and use the default palette
            for i in range(0, len(data_frame.columns.values)):
                label = str(data_frame.columns[i])
                ax_temp = self.get_axis(ax, ax2, label, y_axis_2_series)

                color_spec, axis_1_color_index, axis_2_color_index = \
                    self.get_color(label, axis_1_color_index, axis_2_color_index, color, color_2,
                                        exclude_from_color, color_2_series)

                xd = data_frame.index; yd = data_frame.ix[:,i]

                if (type == 'line'):
                    linewidth = self.get_linewidth(label, linewidth_2, linewidth_2_series)

                    ax_temp.plot(xd, yd, label = label, color = color_spec,
                                     linewidth = linewidth)
                elif(type == 'bar'):
                    ax_temp.bar(xd, yd, label = label, color = color_spec, bottom = yoff)
                    yoff = yoff + yd

                elif(type == 'scatter'):
                    ax_temp.scatter(xd, yd, label = label, color = color_spec)

                    if hasattr(gp, 'line_of_best_fit'):
                        if gp.line_of_best_fit == True:
                            self.trendline(ax_temp, xd.values, yd.values, order=1, color= color_spec, alpha=1,
                                           scale_factor = scale_factor)
        except: pass

        # format X axis
        self.format_x_axis(ax, data_frame, gp)

        try:
             fig.suptitle(gp.title, fontsize = 14 * scale_factor)
        except: pass

        try:
            source = Constants().plotfactory_source

            source_color = 'black'
            display_brand_label = False

            if hasattr(gp, 'source'):
                source = gp.source
                display_brand_label = True

            if hasattr(gp, 'source_color'):
                source_color = self.get_color_code(gp.source_color)

            if display_brand_label or Constants().plotfactory_display_brand_label:
                ax.annotate('Source: ' + source, xy = (1, 0), xycoords='axes fraction', fontsize=7 * scale_factor,
                        xytext=(-5 * scale_factor, 10 * scale_factor), textcoords='offset points',
                        ha='right', va='top', color = source_color)

        except: pass

        if hasattr(gp, 'display_brand_label'):
            if gp.display_brand_label is True:
                self.create_brand_label(ax, anno = Constants().plotfactory_brand_label, scale_factor = scale_factor)
        else:
            if Constants().plotfactory_display_brand_label is True:
                self.create_brand_label(ax, anno = Constants().plotfactory_brand_label, scale_factor = scale_factor)

        leg = []
        leg2 = []

        loc = 'best'

        # if we have two y-axis then make sure legends are in opposite corners
        if ax2 != []: loc = 2

        try:
            leg = ax.legend(loc = loc, prop={'size':10 * scale_factor})
            leg.get_frame().set_linewidth(0.0)
            leg.get_frame().set_alpha(0)

            if ax2 is not []:
                leg2 = ax2.legend(loc = 1, prop={'size':10 * scale_factor})
                leg2.get_frame().set_linewidth(0.0)
                leg2.get_frame().set_alpha(0)

        except: pass

        try:
            if gp.display_legend == False:
                if leg is not[]: leg.remove()
                if leg2 is not[]: leg.remove()

        except: pass

        try:
            plt.savefig(gp.file_output, transparent=False)
        except: pass

        try:
            if hasattr(gp, 'silent_display'):
                if gp.silent_display is False:
                    plt.show()
            else:
                plt.show()
        except:
            pass

        # convert to D3 format with mpld3
        try:
            if hasattr(gp, 'html_file_output'):
                mpld3.save_d3_html(fig, gp.html_file_output)

            if hasattr(gp, 'display_mpld3'):
                if gp.display_mpld3 == True: mpld3.show(fig)
        except: pass

        # convert to Plotly format (fragile!)
        # TODO better to create Plotly graphs from scratch rather than convert from matplotlib
        # TODO also dependent on matplotlib version for support
        try:
            if hasattr(gp, 'plotly_url'):
                plotly.tools.set_credentials_file(username = Constants().plotly_username,
                                                  api_key = Constants().plotly_api_key)

                py_fig = tls.mpl_to_plotly(fig, strip_style = True)
                plot_url = py.plot_mpl(py_fig, filename = gp.plotly_url)
        except:
            pass
Exemple #28
0
def main():
    #	tokens = read_tokens('wacoshootout3_6hrs_tokens.txt')
    #	bigrams_freqdist = get_bigrams_freqdist(tokens)
    # sm12hrs_bigrams = get_bigrams_freqdist(read_tokens('sm12hrs_tokens.txt'))
    # Regex string to parse emoticons
    emoticons_str = r"""
		(?:
			[:=;] # Eyes
			[oO\-]? # Nose (optional)
			[D\)\]\(\]/\\OpP] # Mouth
		)"""

    # Regex string to parse HTML tags, @-mentions, hashtags, URLs, numbers, etc.
    regex_str = [
        emoticons_str,
        r'<[^>]+>',  # HTML tags
        r'(?:@[\w_]+)',  # @-mentions
        r"(?:\#+[\w_]+[\w\'_\-]*[\w_]+)",  # hash-tags
        r'http[s]?://(?:[a-z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-f][0-9a-f]))+',  # URLs

        r'(?:(?:\d+,?)+(?:\.?\d+)?)',  # numbers
        r"(?:[a-z][a-z'\-_]+[a-z])",  # words with - and '
        r'(?:[\w_]+)',  # other words
        r'(?:\S)'  # anything else
    ]

    # Compile regex strings together
    tokens_regex = re.compile(r'(' + '|'.join(regex_str) + ')', re.VERBOSE | re.IGNORECASE)

    # Accumulate a list of punctuations, common (stop) words, and other common words
    # so that the word frequencies output unique words
    punctuation = list(string.punctuation)
    stop = stopwords.words('english') + punctuation + ['rt', 'via', 'san', 'marcos', 'river']

    # Initialize MongoDB collection to extract data from
    client = MongoClient()
    db = client.tweets
    # bill_collection = db.bill_words
    # flood_collection = db.flood
    charleston_collection = db.charleston_unique
    sm_flood_collection = db.sanmarcosflood
    waco_shootout_4_6hrs = db.wacoshootout_original4_6hrs
    waco_shootout_4 = db.wacoshootout_original4_12hrs
    waco_shootout_3_6hrs = db.wacoshootout_original3_6hrs
    waco_shootout_3_6hrs_test = db.wacoshootout_original3_6hrs_test
    wacoshootout_6hrs = db.wacoshootout_original_6hrs
    sm_flood_36hrs = db.sanmarcos36hrs
    sm_flood_12hrs = db.sanmarcos12hrs
    sm_flood_6hrs = db.sanmarcos6hrs
    sm_flood_3hrs = db.sanmarcos3hrs
    lafayette_12hrs = db.lafayette_shooting_unique

    # Max number of tweets to parse
    wacoshootout_6hrs_tweets = wacoshootout_6hrs.count()
    waco_shootout_4_tweets = waco_shootout_4.count()
    waco_shootout_4_6hrs_tweets = waco_shootout_4_6hrs.count()
    waco6hrs_tweets = waco_shootout_3_6hrs.count()
    max_tweets = sm_flood_collection.count()
    flood_tweets = 1000
    charleston_tweets = charleston_collection.count()
    sm_tweets = sm_flood_36hrs.count()
    sm12hrs_tweets = sm_flood_12hrs.count()
    sm6hrs_tweets = sm_flood_6hrs.count()
    sm3hrs_tweets = sm_flood_3hrs.count()
    lafayette_12hrs_tweets = lafayette_12hrs.count()

    # Number of tweets to iterate for frequency vs time plot
    collection_tweets = 1000

    # Start and end dates
    start_date = parse('2015-07-24T01:00:00Z')
    end_date = parse('2015-07-24T13:00:00Z')

    # Pymongo find() query
    date_filter = {'$and': [{'retweeted': False}, {'in_reply_to_status_id': None}, {'in_reply_to_user_id': None},
                            {'created_at': {'$gte': start_date, '$lt': end_date}}, {'retweeted_status': None}]}
    date = {'created_at': {'$gte': start_date, '$lt': end_date}}
    no_filter = {}
    retweet_filter = {'retweeted_status': None}

    #	print waco_shootout_4_tweets

    #	wacoshootout4 = parse_tweets(no_filter, tokens_regex, stop, waco_shootout_4_tweets, waco_shootout_4, True, '', db.wacoshootout4_freqdist)

    # merge_collections(waco_shootout_4_6hrs, waco_shootout_3_6hrs_test)

    print "Number of tweets: " + str(lafayette_12hrs_tweets) + '\n'
    # test_parse = parse_tweets(date, tokens_regex, stop, lafayette_12hrs_tweets, lafayette_12hrs, True, '')
    # write_tokens(test_parse, 'lafayette_12hrs_tokens')
    test_parse = read_tokens('lafayette_12hrs_tokens.txt')
    lafayette_12hrs_monograms = get_monograms_freqdist(test_parse)
    lafayette_12hrs_monograms_data = get_dataframe_dict(lafayette_12hrs_monograms, 5, date, lafayette_12hrs_tweets,
                                                        lafayette_12hrs)
    lafayette_12hrs_monograms_df = lafayette_12hrs_monograms_data[0]
    lafayette_12hrs_monograms_time = lafayette_12hrs_monograms_data[1]
    lafayette_time = lafayette_12hrs_monograms_data[2]

    axes = lafayette_12hrs_monograms_df.plot(figsize=(14, 4), colormap='spectral')
    axes.set_title("Lafayette Shooting Monograms (12 hrs)", fontsize=18)
    axes.set_xlabel("Time (UTC)")
    axes.set_ylabel("Freq")
    plt.show(axes)

    labels = list(lafayette_12hrs_monograms_df.columns.values)
    values = lafayette_12hrs_monograms_time
    time_values = lafayette_time.index.values
    print lafayette_12hrs_monograms_df
    print lafayette_time.index.values
    print lafayette_12hrs_monograms_time[1].values

    # plt.ion()
    # plt.axis('auto')
    # plt.set_cmap('spectral')
    # plt.show(False)

    # labels_index = 0
    # labels_dict = dict()
    #
    # for index in range(len(labels)):
    # 	labels_dict[labels[index]] = lafayette_12hrs_monograms_time[index].values
    #
    # print labels_dict

    # TODO: FIX LINE PLOTTING AND MULTIPLE PLOTS
    # if plt.isinteractive():
    # 	for i in range(len(time_values)):
    # 		for ii in range(len(labels)):
    # 			y = values[ii].values[i]
    # 			plt.plot(time_values[i], y, antialiased=False)
    # 		# plt.draw()
    # 		plt.pause(0.1)
    # 		# plt.draw()
    #
    # plt.show()

    for i in range(len(labels)):
        tooltip = mpld3.plugins.LineLabelTooltip(axes.get_lines()[i], labels[i])
        mpld3.plugins.connect(plt.gcf(), tooltip)

    mpld3.show()
Exemple #29
0
def bubblePrices(city):

    irrelevantAttributes = [
        "City", "Monthly Pass", "Volkswagen Golf", "Toyota Corolla",
        "Basic Utilities", "Internet", "Fitness Club", "Tennis Court Rent",
        "Preschool Month", "Primary School Year", "Rent 1 Bedroom Center",
        "Rent 1 Bedroom Outside Center", "Rent 3 Bedrooms Center",
        "Rent 3 Bedrooms Outside Center", "Price m2 Center",
        "Price m2 Outside Center", "Average Monthly Salary After Tax",
        "Mortgage Interest Rate"
    ]
    fyrirspurn = "SELECT * FROM Prices where City = " + '"' + beforeComma(
        city) + '"'
    print(fyrirspurn)
    query = cur.execute(fyrirspurn)

    cols = [column[0] for column in query.description]

    results = pd.DataFrame.from_records(data=query.fetchall(), columns=cols)
    results.drop(columns=irrelevantAttributes, inplace=True)
    print(results.columns.values)

    values = list(results.loc[0, :])[0:]
    names = []

    j = 0
    for i in cols:
        if i not in irrelevantAttributes:
            names.append(i)

            j = j + 1

    fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'), figsize=(9, 7))

    fyrirspurn = "SELECT * FROM Prices where city = " + '"' + beforeComma(
        city) + '"'
    print(fyrirspurn)
    query = cur.execute(fyrirspurn)

    cols = [column[0] for column in query.description]

    results = pd.DataFrame.from_records(data=query.fetchall(), columns=cols)
    results.drop(columns=irrelevantAttributes, inplace=True)

    values = list(results.loc[0, :])[0:]
    names = []

    j = 0
    for i in cols:
        if i not in irrelevantAttributes:
            names.append(i)

            j = j + 1

    fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'), figsize=(9, 7))

    N = len(values)
    col = np.random.rand(N)
    print(col)
    end = math.ceil(math.sqrt(N))
    # þurfum að gera samsvörun
    step = end / N
    x = np.arange(0, end, step)
    hnitx = []
    for i in range(3, end + 2):
        for j in range(end):
            if (len(hnitx) == N):
                break
            hnitx.append(i)

    hnity = []
    for i in range(N):
        hnity.append(i % end)

    #plt.figure(figsize=(9,9))

    hnitx = [x + random.random() / 4 for x in hnitx]
    hnity = [y + random.random() / 4 for y in hnity]
    mynd = ax.scatter(hnitx,
                      hnity,
                      c=col,
                      alpha=0.5,
                      s=[x * 20 for x in values])
    plt.xlim((0.8, end + 2))

    plt.xticks([], [])
    plt.yticks([], [])

    fig.patch.set_visible(False)
    ax.axis('off')

    font0 = FontProperties()
    font1 = font0.copy()
    font1.set_size('large')
    font = {
        'family': 'serif',
        'color': 'darkred',
        'weight': 'normal',
        'size': 13,
    }

    for label, x, y in zip(names, hnitx, hnity):
        r = random.random() * 2
        plt.text(x + 0.22,
                 y + 0.25,
                 label,
                 ha='right',
                 va='bottom',
                 fontdict=font)
        plt.plot([x + 0.22, x], [y + 0.25, y], 'k-', linewidth=0.5)
    # þarf að sjá

    font = {
        'family': 'serif',
        'color': 'black',
        'weight': 'normal',
        'size': 18,
    }
    ax.set_title("Bubble chart of prices of goods in " + city + " (area = €)",
                 fontdict=font)
    # vantar að staðfesta að röðin sé eins?

    tooltip = mpld3.plugins.PointLabelTooltip(
        mynd, labels=[str(x) + "€" for x in values])

    mpld3.plugins.connect(fig, tooltip)

    mpld3.show()
    def plot_2d_graph(self, data_frame, gp, chart_type):
        if gp.resample is not None: data_frame = data_frame.asfreq(gp.resample)

        # set the matplotlib style sheet & defaults
        matplotlib.rcdefaults()

        # first search PyThalesians styles, then try matplotlib
        try:
            plt.style.use(Constants().plotfactory_pythalesians_style_sheet[gp.style_sheet])
        except:
            plt.style.use(gp.style_sheet)

        matplotlib.rcParams.update({'font.size': matplotlib.rcParams['font.size'] * gp.scale_factor})

        # create figure & add a subplot
        fig = plt.figure(figsize = ((gp.width * gp.scale_factor)/gp.dpi,
                                    (gp.height * gp.scale_factor)/gp.dpi), dpi = gp.dpi)
        ax = fig.add_subplot(111)

        # format Y axis
        y_formatter = matplotlib.ticker.ScalarFormatter(useOffset = False)
        ax.yaxis.set_major_formatter(y_formatter)

        if gp.x_title != '': ax.set_xlabel(gp.x_title)
        if gp.y_title != '': ax.set_ylabel(gp.y_title)

        # create a second y axis if necessary
        ax2 = []

        if gp.y_axis_2_series != []:
            ax2 = ax.twinx()

            # do not use a grid with multiple y axes
            ax.yaxis.grid(False)
            ax2.yaxis.grid(False)

        color_cycle = matplotlib.rcParams['axes.color_cycle']

        bar_ind = np.arange(0, len(data_frame.index))

        xd, bar_ind, has_bar, no_of_bars = self.get_bar_indices(data_frame, gp, chart_type, bar_ind)

        # plot the lines (using custom palettes as appropriate)
        try:
            # get all the correct colors (and construct gradients if necessary eg. from 'blues')
            color_spec = self.create_color_list(gp, data_frame)

            # for stacked bar
            yoff = np.zeros(len(data_frame.index.values)) # the bottom values for stacked bar chart

            # for bar chart
            # bar_ind = np.arange(len(data_frame.index))
            # has_bar = False

            bar_space = 0.2
            bar_width = (1 - bar_space) / (no_of_bars)
            bar_index = 0

            # some lines we should exclude from the color and use the default palette
            for i in range(0, len(data_frame.columns.values)):

                if chart_type is not None:
                    if gp.chart_type is not None:
                        if isinstance(gp.chart_type, list):
                            chart_type = gp.chart_type[i]
                        else:
                            chart_type = gp.chart_type

                label = str(data_frame.columns[i])

                ax_temp = self.get_axis(ax, ax2, label, gp.y_axis_2_series)

                yd = data_frame.ix[:,i]

                if (chart_type == 'line'):
                    linewidth_t = self.get_linewidth(label,
                                                     gp.linewidth, gp.linewidth_2, gp.linewidth_2_series)

                    if linewidth_t is None: linewidth_t = matplotlib.rcParams['axes.linewidth']

                    ax_temp.plot(xd, yd, label = label, color = color_spec[i],
                                     linewidth = linewidth_t)

                elif(chart_type == 'bar'):
                    bar_pos = [k - (1 - bar_space) / 2. + bar_index * bar_width for k in range(0,len(bar_ind))]

                    if color_spec[i] is not None:
                        ax_temp.bar(bar_pos, yd, bar_width, label = label,
                                        color = color_spec[i])
                    else:
                        ax_temp.bar(bar_pos, yd, bar_width, label = label,
                                        color = color_cycle[i % len(color_cycle)])

                    bar_index = bar_index + 1
                    # bar_ind = bar_ind + bar_width

                    has_bar = True

                elif(chart_type == 'stacked'):
                    ax_temp.bar(xd, yd, label = label, color = color_spec[i], bottom = yoff)
                    yoff = yoff + yd

                    has_bar = True

                elif(chart_type == 'scatter'):
                    ax_temp.scatter(xd, yd, label = label, color = color_spec[i])

                    if gp.line_of_best_fit is True:
                        self.trendline(ax_temp, xd.values, yd.values, order=1, color= color_spec[i], alpha=1,
                                           scale_factor = gp.scale_factor)

            # format X axis
            self.format_x_axis(ax, data_frame, gp, has_bar, bar_ind)

        except: pass

        plt.xlabel(gp.x_title)
        plt.ylabel(gp.y_title)

        fig.suptitle(gp.title, fontsize = 14 * gp.scale_factor)

        if gp.display_source_label == True:
            ax.annotate('Source: ' + gp.source, xy = (1, 0), xycoords='axes fraction', fontsize=7 * gp.scale_factor,
                        xytext=(-5 * gp.scale_factor, 10 * gp.scale_factor), textcoords='offset points',
                        ha='right', va='top', color = gp.source_color)

        if gp.display_brand_label == True:
            self.create_brand_label(ax, anno = gp.brand_label, scale_factor = gp.scale_factor)

        leg = []
        leg2 = []

        loc = 'best'

        # if we have two y-axis then make sure legends are in opposite corners
        if ax2 != []: loc = 2

        try:
            leg = ax.legend(loc = loc, prop={'size':10 * gp.scale_factor})
            leg.get_frame().set_linewidth(0.0)
            leg.get_frame().set_alpha(0)

            if ax2 != []:
                leg2 = ax2.legend(loc = 1, prop={'size':10 * gp.scale_factor})
                leg2.get_frame().set_linewidth(0.0)
                leg2.get_frame().set_alpha(0)
        except: pass

        try:
            if gp.display_legend is False:
                if leg != []: leg.remove()
                if leg2 != []: leg.remove()
        except: pass

        try:
            plt.savefig(gp.file_output, transparent=False)
        except: pass


        ####### various matplotlib converters are unstable
        # convert to D3 format with mpld3
        try:
            if gp.display_mpld3 == True:
                mpld3.save_d3_html(fig, gp.html_file_output)
                mpld3.show(fig)
        except: pass

        # FRAGILE! convert to Bokeh format
        # better to use direct Bokeh renderer
        try:
            if (gp.convert_matplotlib_to_bokeh == True):
                from bokeh.plotting import output_file, show
                from bokeh import mpl

                output_file(gp.html_file_output)
                show(mpl.to_bokeh())
        except: pass

        # FRAGILE! convert matplotlib chart to Plotly format
        # recommend using AdapterCufflinks instead to directly plot to Plotly
        try:
            if gp.convert_matplotlib_to_plotly == True:
                plotly.tools.set_credentials_file(username = gp.plotly_username,
                                                  api_key = gp.plotly_api_key)

                py_fig = tls.mpl_to_plotly(fig, strip_style = True)
                plot_url = py.plot_mpl(py_fig, filename = gp.plotly_url)
        except:
            pass

        # display in Matplotlib
        try:
            if gp.silent_display == False: plt.show()
        except:
            pass
Exemple #31
0
def scatterplot(eigindi1, eigindi2, week):

    tafla1 = taflaEigindis(eigindi1)
    tafla2 = taflaEigindis(eigindi2)

    with con:
        if tafla1 in ["WeatherChancesEurope", "WeatherEurope"]:

            data1 = cur.execute("Select city, " + eigindi1 + " from " +
                                tafla1 + " where week = " + week)
            data1 = removeTuples(cur.fetchall())
        else:
            data1 = cur.execute("Select city, " + '"' + eigindi1 + '"' +
                                " from " + tafla1)

            data1 = removeTuples(cur.fetchall())

        if tafla2 in ["WeatherChancesEurope", "WeatherEurope"]:
            data2 = cur.execute("Select city, " + eigindi2 + " from " +
                                tafla2 + " where week = " + week)
            data2 = removeTuples(cur.fetchall())
        else:
            data2 = cur.execute("Select city, " + '"' + eigindi2 + '"' +
                                " from " + tafla2)
            data2 = removeTuples(cur.fetchall())

    # data1 = data1[0:len(data2)]

    # print("\n")
    # print(data1[0:len(data2)+1])
    # print("\n")
    # print(data2)
    # # print("\n")

    # print(data1)
    # print("\n")
    # print(data2)

    # print(len(data1))
    # print(len(data2))

    # get prófað að sleppa Venice, Berlin úr prices á meðan ég prófa þetta

    newdata = []
    newdata2 = []

    missingFromWeatherEurope = [
        "Venice", "Venice, Italy", "Berlin", "Berlin, Germany", "Palermo",
        "Palermo, Italy"
    ]

    for city in data1:

        if city[0] not in missingFromWeatherEurope and city not in newdata:
            newdata.append(city)

    for city in data2:
        if city not in newdata2:
            #print(data2[i])
            newdata2.append(city)
            #print(newdata2)

    # jöfnum þá lengdirnar, newdata2 er minni því veðurgögnin eru ekki fyrir allar borgirnar

    newdata = newdata[0:len(newdata2)]

    fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'), figsize=(9, 7))
    cities = [x[0] for x in newdata]
    cities2 = [x[0] for x in newdata2]

    # for i in range(0,len(cities)):
    #if (cities[i] != cities2[i].split(",")[0]):
    # print(cities[i])
    # print(cities2[i].split(",")[0])

    # print(Counter(cities))
    # # marseille, dublin og leipzig koma fyrir 2x í newdata
    # print(Counter(cities2))

    print(len(newdata))
    print(len(newdata2))

    for i in range(0, len(newdata) - 1):
        print(newdata[i])
        print(newdata2[i])
        print("\n")

    newdata = [x[1] for x in newdata]
    newdata2 = [x[1] for x in newdata2]
    missingCities = []
    i = 0
    for data in newdata2:
        if data == '':
            print(cities2[i])
            # fjarlægjum þetta gildi úr báðum
            newdata.pop(i)
            newdata2.pop(i)
            missingCities.append(cities2[i])
            cities2.pop(i)
        i += 1

    # print(type(newdata[1]))
    # print(type(newdata2[1]))

    # print(newdata)
    # print(newdata2)
    # print(taflaEigindis(newdata2))
    # # if( eigindi1 == "Average Monthly Salary After Tax"):
    # # for string in newdata:
    # # string.replace(',','')
    # # print(newdata)

    print(len(newdata))
    print(len(newdata2))
    N = len(newdata)
    col = np.random.rand(N)
    print(type(col))
    area = s = 10 * np.random.random(size=N)

    mynd = ax.scatter(newdata, newdata2, c=col, alpha=0.5)

    ax.set_xlabel(eigindi1.replace('_', ' '), fontsize=15)
    ax.set_ylabel(eigindi2.replace('_', ' '), fontsize=15)
    ax.set_title("Comparison of prices and weather in European Cities")
    # vantar að staðfesta að röðin sé eins?
    tooltip = mpld3.plugins.PointLabelTooltip(mynd, labels=cities2)
    mpld3.plugins.connect(fig, tooltip)

    mpld3.show()
Exemple #32
0
#!/usr/bin/env python
"""simplest example of Matplotlib to the web browser"""
from matplotlib.pyplot import figure
import mpld3

fig = figure()
ax = fig.gca()
ax.plot([1, 2, 3, 4])

mpld3.show(fig)
Exemple #33
0
    parser.add_argument("--plot",
                        action="store_true",
                        help="If a plot should be generated")
    parser.add_argument(
        "--offset",
        action="store_true",
        help="Vertically offset service to better visualize overlap")
    parser.add_argument("route_or_segment_name",
                        nargs='?',
                        help="The route to print")

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit()
    args = parser.parse_args()

    r = DEMO_ROUTE if args.demo else args.route_or_segment_name.strip()
    lr = logreader_from_route_or_segment(r, sort_by_time=True)

    data, _ = get_timestamps(lr)
    print_timestamps(data['timestamp'], data['duration'], data['start'],
                     args.relative)
    if args.plot:
        mpld3.show(
            graph_timestamps(data['timestamp'],
                             data['start'],
                             data['end'],
                             args.relative,
                             offset_services=args.offset,
                             title=r))
Exemple #34
0
def test_graph():
    plt.plot([3,1,4,1,5], 'ks-', mec='w', mew=5, ms=20)
    mpld3.show()
Exemple #35
0
    def viewPIP(self, method='w+', save=False):
        """
        Function to plot point, polygon and visualize results in interactive web session.
        Option to save as HTML with name "PIP_visualization_{methodname}.html".

        Note: this code MUST be granted permission to open a web browser to view plot.
        Otherwise, save as HTML and open.

        Colors from XKCD color survey https://xkcd.com/color/rgb/
        Scatterplot code adapted from https://mpld3.github.io/examples/scatter_tooltip.html
        Interactive legend code adapted from https://mpld3.github.io/examples/interactive_legend.html
        HTML tooltip code adapted from https://mpld3.github.io/examples/html_tooltips.html

        :param method: (str) method of computing point in polygon.  Valid inputs:
                        'ol' - points on lines only
                        'ov' - points on vertices only
                        'lv' - points on lines or vertices
                        'w' - winding number algorithm
                        'w+' - winding number PLUS any points on lines/vertices (DEFAULT)
                        'rc' - ray casting algorithm
                        'rc+' - ray casting algorithm PLUS any points on lines/vertices
        :param save: (bool) true to enable saving; default false.
                        True saves image in code location folder as "PIP_visualization_{methodname}.html".
        :return: none; opens web session, hit ctrl-c to exit.
        """
        # run PIP
        inOut = self.pointInPolygon(method=method)

        # colors drawn from
        outcolor = "#fd4659" # watermelon
        incolor = "#01386a" # marine blue

        # initialize plot with space for legend
        fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'))
        fig.subplots_adjust(right=0.8)

        # add polygon to image
        poly = Pgon((self.__polygon[:, [0, 1]]),
                    alpha=0.7,
                    facecolor="grey",
                    edgecolor='none',
                    label="Polygon")
        ax.add_patch(poly)

        # collection of all plotted objects
        plotted_objects = [poly]

        # break into yes and no point lists
        outPts = np.empty([0, 3])
        inPts = np.empty([0, 3])

        # creator for tooltip labels
        outLabel = []
        inLabel = []

        # break into inpoints/outpoints collections, with appropriate labels in html
        for i in range(len(inOut)):
            if inOut[i]:
                inPts = np.vstack([inPts,self.__points[i, :]])
                inLabel.append('<p style="color:'+incolor+
                               ';"><i>Inside</i> <br> Pt {0}: ({1:.0f}, {2:.0f})</p>'.format(i + 1, self.__points[i, 0],
                                                                                             self.__points[i, 1]))
            else:
                outPts = np.vstack([outPts,self.__points[i, :]])
                outLabel.append('<p style="color:'+outcolor+
                                ';"><i>Outside</i> <br> Pt {0}: ({1:.0f}, {2:.0f})</p>'.format(i + 1, self.__points[i, 0],
                                                                                               self.__points[i, 1]))
        # scatterplot for points outside
        outScat = ax.scatter(outPts[:, 0],
                             outPts[:, 1],
                             c=outcolor,
                             s=500,
                             alpha=0.7,
                             cmap=plt.cm.jet,
                             edgecolors='none',
                             label="Outside")

        # scatterplot for points inside
        inScat = ax.scatter(inPts[:, 0],
                             inPts[:, 1],
                             c=incolor,
                             s=500,
                             alpha=0.7,
                             cmap=plt.cm.jet,
                             edgecolors='none',
                             label="Inside")

        # create html tooltips
        htmltooltip1 = mpld3.plugins.PointHTMLTooltip(outScat, labels=outLabel, hoffset=10, voffset=10)
        htmltooltip2 = mpld3.plugins.PointHTMLTooltip(inScat, labels=inLabel, voffset=10, hoffset=10)

        # add scatterplots to object collections
        plotted_objects.append(outScat)
        plotted_objects.append(inScat)

        # add title, axes
        title = "Point in Polygon"
        method_text = ""
        if 'w' in method:
            method_text += "Winding Number Algorithm"
        elif 'rc' in method:
            method_text += "Ray Casting Algorithm"
        elif method == 'ol':
            method_text += "Points on Lines (only)"
        elif method == 'ov':
            method_text += "Points on Vertices (only)"
        elif method == 'lv':
            method_text += "Points on Lines and Vertices (only)"

        if '+' in method:
            method_text += ", lines and vertices included."

        # add title
        ax.set_title(title, size=20)
        # add method information as text
        ax.text(min(min(self.__polygon[:, 0]), min(self.__points[:, 0])) - 0.5,
                min(min(self.__polygon[:, 1]), min(self.__points[:, 1])) - 0.5,
                method_text, size=10, style='italic')
        ax.grid(color='white', linestyle='solid')
        ax.set_xlabel("x position")
        ax.set_ylabel("y position")

        # add interactive legend to plot
        interactive_legend = mpld3.plugins.InteractiveLegendPlugin(plotted_objects,
                                                                   ["Polygon", "Outside", "Inside"],
                                                                   alpha_unsel=0.3,
                                                                   alpha_over=1.5)

        # connect all plugins to figure
        mpld3.plugins.connect(fig, htmltooltip1, htmltooltip2, interactive_legend)

        # if save is true, will save to an html as PIP_visualization.html
        if save:
            mpld3.save_html(fig, "PIP_visualization_"+method+".html")

        # display in local web browser (hit Ctrl-C to exit)
        mpld3.show()

        pass
        return classification


km = K_Means(3)
dataNumeric = [[float(tuple[0]), float(tuple[1])] for tuple in data]
#dataNumeric = [[float(tuple[0]),float(tuple[1]),float(tuple[2]), float(tuple[3])] for tuple in data]
km.fit(dataNumeric)
print(km)

# plotting takes forever becuase of the number of points. However, it's working if you're patient enough.
colors = 10 * ["r", "g", "c", "b", "k"]

for centroid in km.centroids:
    plt.scatter(km.centroids[centroid][0],
                km.centroids[centroid][1],
                s=130,
                marker="x")

    for classification in km.classes:
        color = colors[classification]
        for features in km.classes[classification]:
            plt.scatter(features[0], features[1], color=color, s=30)

    mpld3.show()  #launch a web server to view an d3/html figure representation

dataNumeric = [[float(tuple[0]), float(tuple[1])] for tuple in data]
#dataNumeric = [[float(tuple[0]),float(tuple[1]),float(tuple[2]), float(tuple[3])] for tuple in data] # start longitude and latitude
dataNumeric

km.centroids
import matplotlib.pyplot as plt, mpld3

plt.plot([3, 1, 4, 1, 5], 'ks-', mec='w', mew=5, ms=20)
mpld3.show()
Exemple #38
0
             .on("mouseover", function(d, i){
                            d3.select(this).transition().duration(50)
                              .style("stroke-opacity", alpha_fg); })
             .on("mouseout", function(d, i){
                            d3.select(this).transition().duration(200)
                              .style("stroke-opacity", alpha_bg); });
      }
    };
    """

    def __init__(self, lines):
        self.lines = lines
        self.dict_ = {"type": "linehighlight",
                      "line_ids": [utils.get_id(line) for line in lines],
                      "alpha_bg": lines[0].get_alpha(),
                      "alpha_fg": 1.0}


N_paths = 50
N_steps = 100

x = np.linspace(0, 10, 100)
y = 0.1 * (np.random.random((N_paths, N_steps)) - 0.5)
y = y.cumsum(1)

fig, ax = plt.subplots(subplot_kw={'xticks': [], 'yticks': []})
lines = ax.plot(x, y.T, color='blue', lw=4, alpha=0.1)
plugins.connect(fig, HighlightLines(lines))

mpld3.show()
Exemple #39
0
def func(file):
    df = pd.read_excel(file)
    df.groupby('CWL Name').count()['Employee Code'].plot.bar()
    mpld3.show()
Exemple #40
0
def display_mult_flat(x_train, start, stop):
    images = x_train[start].reshape([1, 784])
    for i in range(start + 1, stop):
        images = np.concatenate((images, x_train[i].reshape([1, 784])))
    plt.imshow(images, cmap=plt.get_cmap('gray_r'))
    mpld3.show(port=8000)
def plot_summary(game):

    x = game.team1score
    y = game.team2score

    fig, axScatter = plt.subplots(figsize=(12, 12), subplot_kw=dict(axisbg='#EEEEEE'))

    # the scatter plot:
    size = x + y
    color = np.fabs(x - y)
    scatter = axScatter.scatter(x, y, s=size, c=color, alpha=0.5)
    axScatter.set_aspect(1.)
    axScatter.set_xlabel('team1 score', size=20)
    axScatter.set_ylabel('team2 score', size=20)
    axScatter.grid(color='white', linestyle='solid')

    # create new axes on the right and on the top of the current axes
    # The first argument of the new_vertical(new_horizontal) method is
    # the height (width) of the axes to be created in inches.
    divider = make_axes_locatable(axScatter)
    axHistx = divider.append_axes("top", 1.2, pad=0.2, sharex=axScatter, axisbg='#EEEEEE')
    axHisty = divider.append_axes("right", 1.2, pad=0.3, sharey=axScatter, axisbg='#EEEEEE')

    # make some labels invisible
    plt.setp(axHistx.get_xticklabels() + axHisty.get_yticklabels(),
             visible=False)

    # now determine nice limits by hand:
    binwidth = 5
    xymax = np.max([np.max(np.fabs(x)), np.max(np.fabs(y))])
    lim = (int(xymax/binwidth) + 1)*binwidth

    binnum = np.arange(0, lim + binwidth, binwidth)
    axHistx.hist(x, bins=binnum)
    axHisty.hist(y, bins=binnum, orientation='horizontal')


    # the xaxis of axHistx and yaxis of axHisty are shared with axScatter,
    # thus there is no need to manually adjust the xlim and ylim of these
    # axis.

    #axHistx.axis["bottom"].major_ticklabels.set_visible(False)
    for tl in axHistx.get_xticklabels():
        tl.set_visible(False)
    axHistx.set_yticks([0, 30])

    #axHisty.axis["left"].major_ticklabels.set_visible(False)
    for tl in axHisty.get_yticklabels():
        tl.set_visible(False)
    axHisty.set_xticks([0, 30])

    #plt.draw()
    #plt.show()
    plotid = mpld3.utils.get_id(axScatter)


    # Define some CSS to control our custom labels
    css = """
    table
    {
      border-collapse: collapse;
    }
    th
    {
      color: #ffffff;
      background-color: #aaa;
    }
    td
    {
      background-color: #cccccc;
    }
    table, th, td
    {
      font-family:Arial, Helvetica, sans-serif;
      border: 1px solid gray;
      text-align: middle;
    }
    """

    #labels = ['Round {}, {} vs. {}, {}:{}'.format(g[1], g[2], g[3], g[4], g[5]) for g in game.itertuples()]
    labels = []
    print game.shape
    for i in range(game.shape[0]):
        label = game.ix[[i],:].T
        label.columns = ['Game Info']
        labels.append(str(label.to_html()))

    tooltip = mpld3.plugins.PointHTMLTooltip(scatter, labels=labels, voffset=10, hoffset=10, css=css)
    mpld3.plugins.connect(fig, tooltip)
    mpld3.show()

    fightml = mpld3.fig_to_html(fig)
    return plotid, fightml
Exemple #42
0
def bubbleIndices(cost, city):
    if (cost):
        fyrirspurn = "SELECT * FROM CostOfLivingIndex where city = " + '"' + city + '"'
    else:
        fyrirspurn = "SELECT * FROM QualityOfLifeIndex where city = " + '"' + city + '"'

    irrelevantAttributes = ["City", "Rank", "index"]

    ### allt fyrir neðan er sama og í Prices dæminu
    print(fyrirspurn)
    query = cur.execute(fyrirspurn)

    cols = [column[0] for column in query.description]

    results = pd.DataFrame.from_records(data=query.fetchall(), columns=cols)
    results.drop(columns=irrelevantAttributes, inplace=True)

    values = list(results.loc[0, :])[0:]
    names = []

    j = 0
    for i in cols:
        if i not in irrelevantAttributes:
            names.append(i)

            j = j + 1

    print(len(values))
    print(len(names))
    fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'), figsize=(9, 7))

    N = len(values)

    #col = np.arange(0.1,0.99,0.98/N)
    col = np.random.random_integers(0, N - 1)
    print(col)
    #print(len(col))
    colors = [
        'yellow', 'red', 'green', 'blue', 'black', 'purple', 'brown', 'pink'
    ]

    end = math.ceil(math.sqrt(N))
    # þurfum að gera samsvörun
    step = end / N
    x = np.arange(0, end, step)
    hnitx = []
    for i in range(1, end + 1):
        for j in range(end):
            if (len(hnitx) == N):
                break
            hnitx.append(i)

    hnity = []
    for i in range(N):
        hnity.append(i % end)

    #plt.figure(figsize=(9,9))

    # hnitx = [x + random.random()/4 for x in hnitx]
    # hnity = [y + random.random()/4 for y in hnity]
    print(hnitx)
    print(hnity)
    mynd = ax.scatter(hnitx,
                      hnity,
                      c=colors,
                      alpha=0.5,
                      s=[x * 20 for x in values])
    plt.xlim((0, end))
    plt.ylim((-1, end))

    plt.xticks([], [])
    plt.yticks([], [])

    fig.patch.set_visible(False)
    ax.axis('off')

    font0 = FontProperties()
    font1 = font0.copy()
    font1.set_size('large')
    fonts = []
    for i in range(N):

        fonts.append({
            'family': 'serif',
            'color': colors[i],
            'weight': 'normal',
            'size': 10,
        })

    j = 0
    for label, x, y in zip(names, hnitx, hnity):

        plt.text(x + 0.22,
                 y + 0.25,
                 label,
                 ha='right',
                 va='bottom',
                 fontdict=fonts[j])
        j = j + 1
        #plt.plot([x+0.22,x], [y+0.25,y], 'k-', linewidth = 0.5)
    # þarf að sjá

    font = {
        'family': 'serif',
        'color': 'black',
        'weight': 'normal',
        'size': 18,
    }
    ax.set_title("Bubble chart of prices of goods in " + city + " (area = €)",
                 fontdict=font)
    # vantar að staðfesta að röðin sé eins?

    tooltip = mpld3.plugins.PointLabelTooltip(mynd, labels=values)

    mpld3.plugins.connect(fig, tooltip)

    mpld3.show()
Exemple #43
0
def plotData(NQuery, input_table, FigureStrBase, SurfMin=1e-1*u.M_sun/u.pc**2,
             SurfMax=1e5*u.M_sun/u.pc**2, VDispMin=1e-1*u.km/u.s,
             VDispMax=3e2*u.km/u.s, RadMin=1e-2*u.pc, RadMax=1e3*u.pc,
             interactive=True):

    """
    This is where documentation needs to be added

    Parameters
    ----------
    NQuery
    FigureStrBase : str
        The start of the output filename, e.g. for "my_file.png" it would be
        my_file
    SurfMin
    SurfMax
    VDispMin
    VDispMax
    RadMin
    RadMax
    """
    figure = matplotlib.figure.Figure()
    if interactive:
        from matplotlib import pyplot
        from matplotlib import _pylab_helpers
        backend = getattr(matplotlib.backends, 'backend_{0}'.format(matplotlib.rcParams['backend']).lower())
        canvas = backend.FigureCanvas(figure)
        figmanager = backend.FigureManager(canvas, 1)
        figmanager.canvas.figure.number = 1
        _pylab_helpers.Gcf.set_active(figmanager)
    else:
        canvas = FigureCanvasAgg(figure)
    ax = figure.gca()

    d = input_table
    Author = d['Names']
    Run = d['IDs']
    SurfDens = d['SurfaceDensity']
    VDisp = d['VelocityDispersion']
    Rad = d['Radius']
    if d['IsSimulated'].dtype == 'bool':
        IsSim = d['IsSimulated']
    else:
        IsSim = d['IsSimulated'] == 'True'

    UseSurf = (SurfDens > SurfMin) & (SurfDens < SurfMax)
    UseVDisp = (VDisp > VDispMin) & (VDisp < VDispMax)
    UseRad = (Rad > RadMin) & (Rad < RadMax)
    Use = UseSurf & UseVDisp & UseRad
    Obs = (~IsSim) & Use
    Sim = IsSim & Use

    UniqueAuthor = list(set(Author[Use]))[4]
    NUniqueAuthor = len(UniqueAuthor)

    #print d
    #print d[Use]
    # print 'Authors:', UniqueAuthor

    #colors = random.sample(matplotlib.colors.cnames, NUniqueAuthor)
    colors = list(matplotlib.cm.jet(np.linspace(0,1,NUniqueAuthor)))
    random.shuffle(colors)

    # NOTE this does NOT work with mpld3
    # ax.loglog()

    scatters = []
    labels = []

    markers = ['o', 's']
    for iAu,color in zip(UniqueAuthor, colors) :
        UsePlot = (Author == iAu) & Use
        ObsPlot = ((Author == iAu) & (~IsSim)) & Use
        SimPlot = ((Author == iAu) & (IsSim)) & Use

        if any(ObsPlot):
            print iAu
            scatters.append(ax.scatter(np.log10(SurfDens[ObsPlot]), np.log10(VDisp[ObsPlot]),
                        marker=markers[0],
                        s=(np.log10(np.array(Rad[ObsPlot]))-np.log10(RadMin.value)+0.5)**3.,
                        color=color, alpha=0.5))

            # for row in d[ObsPlot]:
            #     row_html = [str(j) for j in d[ObsPlot].pformat(html=True)]
            #     labels.append("\n ".join(row_html))
            labels = ['<h1>{title}</h1>'.format(title=i) for i in range(len(d[ObsPlot]))]

        if any(SimPlot):
            scatters.append(ax.scatter(np.log10(SurfDens[SimPlot]), np.log10(VDisp[SimPlot]),
                        marker=markers[1],
                        s=(np.log10(np.array(Rad[SimPlot]))-np.log10(RadMin.value)+0.5)**3.,
                        color=color, alpha=0.5))

            # for row in d[SimPlot]:
            #     row_html = [str(j) for j in d[SimPlot].pformat(html=True)]

            #     labels.append("\n ".join(row_html))

    if any(Obs):
        ax.scatter(np.log10(SurfDens[Obs]), np.log10(VDisp[Obs]),
                    marker=markers[0],
                    s=(np.log10(np.array(Rad[Obs]))-np.log10(RadMin.value)+0.5)**3.,
                    facecolors='none', edgecolors='black',
                    alpha=0.5)

    if any(Sim):
        ax.scatter(np.log10(SurfDens[Sim]), np.log10(VDisp[Sim]),
                    marker=markers[1],
                    s=(np.log10(np.array(Rad[Sim]))-np.log10(RadMin.value)+0.5)**3.,
                    facecolors='none', edgecolors='black',
                    alpha=0.5)

    ax.set_xlabel('$\Sigma$ [M$_{\odot}$ pc$^{-2}$]', fontsize=16)
    ax.set_ylabel('$\sigma$ [km s$^{-1}$]', fontsize=16)

    box = ax.get_position()
    ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

    ax.set_xlim((np.log10(SurfMin.to(u.M_sun/u.pc**2).value),
                 np.log10(SurfMax.to(u.M_sun/u.pc**2).value)))
    ax.set_ylim((np.log10(VDispMin.to(u.km/u.s).value),
                 np.log10(VDispMax.to(u.km/u.s).value)))

    # ax.legend(UniqueAuthor, loc='center left', bbox_to_anchor=(1.0, 0.5),
    #           prop={'size':12}, markerscale = .7, scatterpoints = 1)

    labels = ['<h1>{title}</h1>'.format(title=i) for i in range(len(d))]

    tooltip = plugins.PointHTMLTooltip(scatters[0], labels,
                                   voffset=10, hoffset=10)
    plugins.connect(figure, tooltip)

    # figure.savefig(FigureStrBase+NQuery+'.png',bbox_inches='tight',dpi=150)
    # figure.savefig(FigureStrBase+NQuery+'.pdf',bbox_inches='tight',dpi=150)

    if interactive:
        # from matplotlib import pyplot as plt
        # plt.ion()
        # plt.show()

        mpld3.show()

    return FigureStrBase+NQuery+'.png'
Exemple #44
0
# %%

# Distribution of document length?

import matplotlib
matplotlib.use('Agg')

import pandas as pd
import matplotlib.pyplot as plt
import mpld3
lengths = list(map(len, train_data))
le = pd.Series(lengths)
plt.clf()
le[le < 2000].hist()
mpld3.show(open_browser=False)

# %%

# Remove stopwords

import nltk
stopwords = nltk.corpus.stopwords.words('english')

# %%

# Tokenizing + stemming

import spacy
import re
from functools import partial
Exemple #45
0
def show_graph():
    net.show("facebook_net.html")
    plt.axis('off')  # hide the axes
    mpld3.show()
    def plot_2d_graph(self, data_frame, gp, chart_type):

        if gp is None: gp = GraphProperties()
        if gp.chart_type is None and chart_type is None: chart_type = 'line'

        if gp.resample is not None: data_frame = data_frame.asfreq(gp.resample)

        self.apply_style_sheet(gp)

        # create figure & add a subplot
        fig = plt.figure(figsize=((gp.width * gp.scale_factor) / gp.dpi,
                                  (gp.height * gp.scale_factor) / gp.dpi),
                         dpi=gp.dpi)
        ax = fig.add_subplot(111)

        if gp.x_title != '': ax.set_xlabel(gp.x_title)
        if gp.y_title != '': ax.set_ylabel(gp.y_title)

        plt.xlabel(gp.x_title)
        plt.ylabel(gp.y_title)

        fig.suptitle(gp.title, fontsize=14 * gp.scale_factor)

        # format Y axis
        y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False)
        ax.yaxis.set_major_formatter(y_formatter)

        # create a second y axis if necessary
        ax2 = []

        if gp.y_axis_2_series != []:
            ax2 = ax.twinx()

            # do not use a grid with multiple y axes
            ax.yaxis.grid(False)
            ax2.yaxis.grid(False)

        # matplotlib 1.5
        try:
            cyc = matplotlib.rcParams['axes.prop_cycle']
            color_cycle = [x['color'] for x in cyc]
        except KeyError:
            # pre 1.5
            pass
            # color_cycle =  matplotlib.rcParams['axes.color_cycle']

        bar_ind = np.arange(0, len(data_frame.index))

        # for bar charts, create a proxy x-axis (then relabel)
        xd, bar_ind, has_bar, no_of_bars = self.get_bar_indices(
            data_frame, gp, chart_type, bar_ind)

        # plot the lines (using custom palettes as appropriate)
        try:
            # get all the correct colors (and construct gradients if necessary eg. from 'blues')
            color_spec = self.create_color_list(gp, data_frame)

            # for stacked bar
            yoff_pos = np.zeros(len(data_frame.index.values)
                                )  # the bottom values for stacked bar chart
            yoff_neg = np.zeros(len(data_frame.index.values)
                                )  # the bottom values for stacked bar chart

            zeros = np.zeros(len(data_frame.index.values))

            # for bar chart
            bar_space = 0.2
            bar_width = (1 - bar_space) / (no_of_bars)
            bar_index = 0

            has_matrix = False

            # some lines we should exclude from the color and use the default palette
            for i in range(0, len(data_frame.columns.values)):

                if gp.chart_type is not None:
                    if isinstance(gp.chart_type, list):
                        chart_type = gp.chart_type[i]
                    else:
                        chart_type = gp.chart_type

                if chart_type == 'heatmap':
                    # TODO experimental!
                    # ax.set_frame_on(False)
                    ax.pcolor(data_frame, cmap=plt.cm.Blues, alpha=0.8)
                    # plt.colorbar()
                    has_matrix = True
                    break

                label = str(data_frame.columns[i])

                ax_temp = self.get_axis(ax, ax2, label, gp.y_axis_2_series)

                yd = data_frame.ix[:, i]

                if color_spec[i] is None:
                    color_spec[i] = color_cycle[i % len(color_cycle)]

                if (chart_type == 'line'):
                    linewidth_t = self.get_linewidth(label, gp.linewidth,
                                                     gp.linewidth_2,
                                                     gp.linewidth_2_series)

                    if linewidth_t is None:
                        linewidth_t = matplotlib.rcParams['axes.linewidth']

                    ax_temp.plot(xd,
                                 yd,
                                 label=label,
                                 color=color_spec[i],
                                 linewidth=linewidth_t)

                elif (chart_type == 'bar'):
                    # for multiple bars we need to allocate space properly
                    bar_pos = [
                        k - (1 - bar_space) / 2. + bar_index * bar_width
                        for k in range(0, len(bar_ind))
                    ]

                    ax_temp.bar(bar_pos,
                                yd,
                                bar_width,
                                label=label,
                                color=color_spec[i])

                    bar_index = bar_index + 1

                elif (chart_type == 'stacked'):
                    bar_pos = [
                        k - (1 - bar_space) / 2. + bar_index * bar_width
                        for k in range(0, len(bar_ind))
                    ]

                    yoff = np.where(yd > 0, yoff_pos, yoff_neg)

                    ax_temp.bar(bar_pos,
                                yd,
                                label=label,
                                color=color_spec[i],
                                bottom=yoff)

                    yoff_pos = yoff_pos + np.maximum(yd, zeros)
                    yoff_neg = yoff_neg + np.minimum(yd, zeros)

                    # bar_index = bar_index + 1

                elif (chart_type == 'scatter'):
                    ax_temp.scatter(xd, yd, label=label, color=color_spec[i])

                    if gp.line_of_best_fit is True:
                        self.trendline(ax_temp,
                                       xd.values,
                                       yd.values,
                                       order=1,
                                       color=color_spec[i],
                                       alpha=1,
                                       scale_factor=gp.scale_factor)

            # format X axis
            self.format_x_axis(ax, data_frame, gp, has_bar, bar_ind,
                               has_matrix)

        except:
            pass

        if gp.display_source_label == True:
            ax.annotate('Source: ' + gp.source,
                        xy=(1, 0),
                        xycoords='axes fraction',
                        fontsize=7 * gp.scale_factor,
                        xytext=(-5 * gp.scale_factor, 10 * gp.scale_factor),
                        textcoords='offset points',
                        ha='right',
                        va='top',
                        color=gp.source_color)

        if gp.display_brand_label == True:
            self.create_brand_label(ax,
                                    anno=gp.brand_label,
                                    scale_factor=gp.scale_factor)

        leg = []
        leg2 = []

        loc = 'best'

        # if we have two y-axis then make sure legends are in opposite corners
        if ax2 != []: loc = 2

        try:
            leg = ax.legend(loc=loc, prop={'size': 10 * gp.scale_factor})
            leg.get_frame().set_linewidth(0.0)
            leg.get_frame().set_alpha(0)

            if ax2 != []:
                leg2 = ax2.legend(loc=1, prop={'size': 10 * gp.scale_factor})
                leg2.get_frame().set_linewidth(0.0)
                leg2.get_frame().set_alpha(0)
        except:
            pass

        try:
            if gp.display_legend is False:
                if leg != []: leg.remove()
                if leg2 != []: leg.remove()
        except:
            pass

        try:
            plt.savefig(gp.file_output, transparent=False)
        except:
            pass

        ####### various matplotlib converters are unstable
        # convert to D3 format with mpld3
        try:
            # output matplotlib charts externally to D3 based libraries
            import mpld3

            if gp.display_mpld3 == True:
                mpld3.save_d3_html(fig, gp.html_file_output)
                mpld3.show(fig)
        except:
            pass

        # FRAGILE! convert to Bokeh format
        # better to use direct Bokeh renderer
        try:
            if (gp.convert_matplotlib_to_bokeh == True):
                from bokeh.plotting import output_file, show
                from bokeh import mpl

                output_file(gp.html_file_output)
                show(mpl.to_bokeh())
        except:
            pass

        # FRAGILE! convert matplotlib chart to Plotly format
        # recommend using AdapterCufflinks instead to directly plot to Plotly
        try:
            import plotly.plotly as py
            import plotly
            import plotly.tools as tls

            if gp.convert_matplotlib_to_plotly == True:
                plotly.tools.set_credentials_file(username=gp.plotly_username,
                                                  api_key=gp.plotly_api_key)

                py_fig = tls.mpl_to_plotly(fig, strip_style=True)
                plot_url = py.plot_mpl(py_fig, filename=gp.plotly_url)
        except:
            pass

        # display in matplotlib window
        try:
            if Constants.plotfactory_silent_display == True:
                pass
            elif gp.silent_display == False:
                plt.show()

        except:
            pass
Exemple #47
0
def four_group_plot(csv_file,
                    x_col,
                    y_col,
                    group1_col,
                    group2_col,
                    group3_col,
                    show_only_col,
                    show_only_col_flag=True):
    data = pd.read_csv(csv_file)

    data_group1 = data.groupby(group1_col)
    group2_labels = data[group2_col]

    fig, ax = plt.subplots()
    ax.margins(0.05)

    group1_marker_type = ['o', 's']
    group1_marker_size = [80, 100]
    group2_colors = ['purple', 'orange']
    group3_colors = ['purple', 'orange']
    group3_marker_size = [20, 30]

    # PLOT GROUP 1 and Group 2
    # TO be replaced by backgorund mesh
    # g1_cnt = 0
    # for name1,group1 in data_group1:
    #     marker=group1_marker_type[g1_cnt]
    #     marker_size = group1_marker_size[g1_cnt]
    #     data_group2 = group1.groupby(group2_col)
    #     g2_cnt=0
    #     for name2,group2 in data_group2:
    #         ax.scatter(group2[x_col],group2[y_col],marker=marker,s= marker_size,color=group2_colors[g2_cnt],alpha=.4)
    #         g2_cnt+=1
    #     g1_cnt+=1

    # Plot with just group 1
    # Plot with just group 1 and group 3
    g1_cnt = 0
    for name1, group1 in data_group1:
        marker = group1_marker_type[g1_cnt]
        marker_size = group3_marker_size[g1_cnt]
        data_group3 = group1.groupby(group3_col)
        g3_cnt = 0
        for name3, group3 in data_group3:
            lbl_str = group3_col + ': ' + str(name3)
            if marker == 'o':
                ax.scatter(group3[x_col],
                           group3[y_col],
                           marker=marker,
                           s=marker_size,
                           color=group3_colors[g3_cnt],
                           alpha=.6,
                           label=lbl_str)
            g3_cnt += 1
        g1_cnt += 1

    # g1_cnt=0
    # for name1,group1 in data_group1:
    #     marker=group1_marker_type[g1_cnt]
    #     marker_size=group1_marker_size[g1_cnt]
    #     ax.scatter(group1[x_col], group1[y_col], marker=marker, s= marker_size,facecolors='cyan',edgecolors='k',alpha=.1,label=group1_col+'_'+str(name1))
    #     g1_cnt+=1
    #     break

    ### show_only_col
    show_only_data = data[data[show_only_col] == show_only_col_flag]
    ax.scatter(show_only_data[x_col],
               show_only_data[y_col],
               marker='*',
               color='red',
               label='Please Label Manually (' + show_only_col + ')')
    #
    # ax.legend()
    # plt.show()

    handles, labels = ax.get_legend_handles_labels()  # return lines and labels
    interactive_legend = plugins.InteractiveLegendPlugin(
        zip(handles, ax.collections), labels)
    # alpha_unsel=0.5,
    # alpha_over=1.5,
    # start_visible=True)

    plugins.connect(fig, interactive_legend)
    ax.set_xlabel(x_col)
    ax.set_ylabel(y_col)
    ax.set_title('LOW DIM VISUALIZATION', size=20)
    mpld3.show()
Exemple #48
0
def plotData(NQuery, input_table, FigureStrBase, html_dir=None, png_dir=None,
             xvariable='SurfaceDensity', yvariable='VelocityDispersion',
             zvariable='Radius',
             xMin=None, xMax=None, yMin=None, yMax=None, zMin=None, zMax=None,
             interactive=False, show_log=True, min_marker_width=3,
             max_marker_width=0.05):
    """
    This is where documentation needs to be added

    Parameters
    ----------
    NQuery
    FigureStrBase : str
        The start of the output filename, e.g. for "my_file.png" it would be
        my_file
    xMin
    xMax
    yMin
    yMax
    zMin
    zMax
    min_marker_width : int or float, optional
        Sets the pixel width of the smallest marker to be plotted. If <1,
        it is interpreted to be a fraction of the total pixels along the
        shortest axis.
    max_marker_width : int or float, optional
        Sets the pixel width of the smallest marker to be plotted. If <1,
        it is interpreted to be a fraction of the total pixels along the
        shortest axis.

    """
    if len(input_table) == 0:
        raise ValueError("The input table is empty.")

    figure = matplotlib.figure.Figure()
    if interactive:
        from matplotlib import pyplot
        from matplotlib import _pylab_helpers
        backend = getattr(matplotlib.backends, 'backend_{0}'.format(matplotlib.rcParams['backend']).lower())
        canvas = backend.FigureCanvas(figure)
        figmanager = backend.FigureManager(canvas, 1)
        figmanager.canvas.figure.number = 1
        _pylab_helpers.Gcf.set_active(figmanager)
    else:
        figure = matplotlib.figure.Figure()
        canvas = FigureCanvasAgg(figure)
    ax = figure.gca()

    d = input_table
    Author = d['Names']
    Run = d['IDs']
    x_ax = d[xvariable]
    y_ax = d[yvariable]
    z_ax = d[zvariable]

    # Check if limits are given
    if xMin is None:
        xMin = x_ax.min()
    if xMax is None:
        xMax = x_ax.max()

    if yMin is None:
        yMin = y_ax.min()
    if yMax is None:
        yMax = y_ax.max()

    if zMin is None:
        zMin = z_ax.min()
    if zMax is None:
        zMax = z_ax.max()

    if d['IsSimulated'].dtype == 'bool':
        IsSim = d['IsSimulated']
    else:
        IsSim = d['IsSimulated'] == 'True'

    if show_log:
        if not label_dict_html[xvariable].startswith('log'):
            label_dict_html[xvariable] = 'log ' + label_dict_html[xvariable]
            label_dict_html[yvariable] = 'log ' + label_dict_html[yvariable]
        if not label_dict_png[xvariable].startswith('log'):
            label_dict_png[xvariable] = 'log ' + label_dict_png[xvariable]
            label_dict_png[yvariable] = 'log ' + label_dict_png[yvariable]

    # Select points within the limits
    Use_x_ax = (x_ax > xMin) & (x_ax < xMax)
    Use_y_ax = (y_ax > yMin) & (y_ax < yMax)
    Use_z_ax = (z_ax > zMin) & (z_ax < zMax)
    # intersects the three subsets defined above
    Use = Use_x_ax & Use_y_ax & Use_z_ax

    nptstoplot = np.count_nonzero(Use)
    if nptstoplot == 0:
        log.debug("Use: {0}".format(Use))
        log.debug("Use_x_ax: {0}".format(Use_x_ax))
        log.debug("xmin: {0} xmax: {1}".format(xMin, xMax))
        log.debug("x_ax: {0}".format(x_ax))
        log.debug("Use_y_ax: {0}".format(Use_y_ax))
        log.debug("ymin: {0} ymax: {1}".format(yMin, yMax))
        log.debug("y_ax: {0}".format(y_ax))
        log.debug("Use_z_ax: {0}".format(Use_z_ax))
        log.debug("zmin: {0} zmax: {1}".format(zMin, zMax))
        log.debug("z_ax: {0}".format(z_ax))
        return None,None
    else:
        log.debug("Found {0} points to plot".format(nptstoplot))

    UniqueAuthor = list(set(Author[Use]))
    NUniqueAuthor = len(UniqueAuthor)

    colors = list(matplotlib.cm.jet(np.linspace(0, 1, NUniqueAuthor)))
    random.seed(12)
    random.shuffle(colors)

    # NOTE this does NOT work with mpld3
    # ax.loglog()

    # Set marker sizes based on a minimum and maximum pixel size, then scale
    # the rest between.

    bbox = \
        ax.get_window_extent().transformed(figure.dpi_scale_trans.inverted())

    min_axis_size = min(bbox.width, bbox.height) * figure.dpi

    if max_marker_width < 1:
        max_marker_width *= min_axis_size

    if min_marker_width < 1:
        min_marker_width *= min_axis_size

    marker_conversion = max_marker_width / \
        (np.log10(z_ax[Use].max())-np.log10(z_ax[Use].min()))

    marker_widths = (marker_conversion *
                     (np.log10(np.array(z_ax))-np.log10(z_ax[Use].min())) +
                     min_marker_width)

    marker_sizes = marker_widths**2

    scatters = []

    markers = ['o', 's']
    for iAu, color in zip(UniqueAuthor, colors):
        ObsPlot = ((Author == iAu) & (~IsSim)) & Use
        SimPlot = ((Author == iAu) & (IsSim)) & Use

        if show_log:
            plot_x = np.log10(x_ax)
            plot_y = np.log10(y_ax)

        if any(ObsPlot):
            # Change to logs on next commit
            scatter = \
                ax.scatter(plot_x[ObsPlot], plot_y[ObsPlot], marker=markers[0],
                           s=marker_sizes[ObsPlot],
                           color=color, alpha=0.5, edgecolors='k',
                           label=iAu)

            scatters.append(scatter)

            labels = []

            for row in d[ObsPlot]:
                colnames = ['<div>{title}</div>'.format(title=col)
                            for col in row.colnames]
                values = ['<div>{title}</div>'.format(title=str(val))
                          for val in row]

                label = ""

                for col, val in zip(colnames, values):
                    label += col+" "+val+" \n "

                labels.append(label)

            tooltip = plugins.PointHTMLTooltip(scatter, labels,
                                               voffset=10, hoffset=10)
            plugins.connect(figure, tooltip)

        if any(SimPlot):
            # Change to logs on next commit
            scatter = \
                ax.scatter(plot_x[SimPlot], plot_y[SimPlot], marker=markers[1],
                           s=marker_sizes[SimPlot],
                           color=color, alpha=0.5, edgecolors='k',
                           label=iAu)

            scatters.append(scatter)

            labels = []

            for row in d[SimPlot]:
                colnames = ['<div>{title}</div>'.format(title=col)
                            for col in row.colnames]
                values = ['<div>{title}</div>'.format(title=str(val))
                          for val in row]

                label = ""

                for col, val in zip(colnames, values):
                    label += col+" "+val+" \n "

                labels.append(label)

            tooltip = plugins.PointHTMLTooltip(scatter, labels,
                                               voffset=10, hoffset=10, css=css)
            plugins.connect(figure, tooltip)

    ax.set_xlabel(label_dict_html[xvariable], fontsize=16)
    ax.set_ylabel(label_dict_html[yvariable], fontsize=16)

    # Set plot limits. Needed for conversion of pixel units to plot units.

    # Pad the maximum marker width on.
    inv = ax.transData.inverted()
    pad_x, pad_y = inv.transform((marker_widths.max(), marker_widths.max())) - \
        inv.transform((0.0, 0.0))

    if show_log:
        ax.set_xlim(np.log10(xMin.value)-pad_x, np.log10(xMax.value)+pad_x)
        ax.set_ylim(np.log10(yMin.value)-pad_y, np.log10(yMax.value)+pad_y)
    else:
        ax.set_xlim(xMin.value - pad_x, xMax.value + pad_x)
        ax.set_ylim(yMin.value - pad_y, yMax.value + pad_y)

    box = ax.get_position()
    ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

    # ax.legend(UniqueAuthor, loc='center left', bbox_to_anchor=(1.0, 0.5),
    #           prop={'size':12}, markerscale = .7, scatterpoints = 1)

    if hasattr(mpld3.plugins, 'InteractiveLegendPlugin'):
        plugins.connect(figure,
                        plugins.InteractiveLegendPlugin(scatters,
                                                        UniqueAuthor,
                                                        alpha_unsel=0.0,
                                                        alpha_over=0.5))

    # Adding fake points to show the size

    # Try floor and ceil. Pick the one closest to the max/min.
    max_z = round_to_pow_10(z_ax[Use].max())
    min_z = round_to_pow_10(z_ax[Use].min())
    mid_z = round_to_pow_10((max_z + min_z) / 2., log=False)
    if mid_z == max_z:
        fake_z_marker_width = np.array([max_z])
    elif mid_z == max_z or mid_z == min_z:
        fake_z_marker_width = np.array([max_z, min_z])
    else:
        fake_z_marker_width = np.array([max_z, mid_z, min_z])

    fake_marker_sizes = (marker_conversion *
                         (fake_z_marker_width-np.log10(z_ax[Use].min())) +
                         min_marker_width)**2

    # Set the axis fraction to plot the points at. Adjust if the largest
    # will overlap with the next.
    sep_ax_frac = 0.05

    if np.sqrt(fake_marker_sizes[0])/float(min_axis_size) > 0.05:
        sep_ax_frac = np.sqrt(fake_marker_sizes[0])/float(min_axis_size) \
            + 0.005

    xfake = [0.1] * fake_z_marker_width.shape[0]
    yfake = [0.95 - sep_ax_frac*x for x in range(fake_z_marker_width.shape[0])]

    # xfake = [xax_limits[0] + xax_limits[0]*2.,
    #          xax_limits[0] + xax_limits[0]*2.,
    #          xax_limits[0] + xax_limits[0]*2.]
    # yfake = [yax_limits[1] - yax_limits[1]*0.01,
    #          yax_limits[1] - yax_limits[1]*0.3,
    #          yax_limits[1] - yax_limits[1]*0.6]

    ax.scatter(np.array(xfake), np.array(yfake), marker='+',
               s=fake_marker_sizes,
               transform=ax.transAxes,
               facecolors='g')
    for xf, yf, rad in zip(xfake, yfake, fake_z_marker_width):
        ax.text(xf + 0.05, yf-0.01, str(10**rad) + ' ' + str(zMin.unit),
                transform=ax.transAxes)

    # Saving the plots

    if html_dir is None:
        html_dir = ""

    if png_dir is None:
        png_dir = ""

    html_file = os.path.join(html_dir, FigureStrBase+NQuery+'.html')
    png_file = os.path.join(png_dir, FigureStrBase+NQuery+".png")

    html = mpld3.fig_to_html(figure)
    with open(html_file, 'w') as f:
       f.write(html)

    if interactive:
        # from matplotlib import pyplot as plt
        # plt.ion()
        # plt.show()

        mpld3.show()

    # Clear out the plugins
    plugins.clear(figure)

    # Use latex labels for the mpl outputted plot
    ax.set_xlabel(label_dict_png[xvariable], fontsize=16)
    ax.set_ylabel(label_dict_png[yvariable], fontsize=16)

    # Shrink current axis by 20%
    box = ax.get_position()
    ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

    # Put a legend to the right of the current axis
    legend = ax.legend(loc='center left', bbox_to_anchor=(1, 0.5),
                       handlelength=4)
    legend.draw_frame(False)

    figure.savefig(png_file, bbox_inches='tight', dpi=150)
    # figure.savefig(FigureStrBase+NQuery+'.pdf',bbox_inches='tight',dpi=150)

    return html_file, png_file
Exemple #49
0
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument(
        "--relative",
        action="store_true",
        help="Make timestamps relative to the start of each frame")
    parser.add_argument("--demo",
                        action="store_true",
                        help="Use the demo route instead of providing one")
    parser.add_argument("--plot",
                        action="store_true",
                        help="If a plot should be generated")
    parser.add_argument("route_or_segment_name",
                        nargs='?',
                        help="The route to print")

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit()
    args = parser.parse_args()

    r = DEMO_ROUTE if args.demo else args.route_or_segment_name.strip()
    lr = logreader_from_route_or_segment(r, sort_by_time=True)

    data, _ = get_timestamps(lr)
    print_timestamps(data['timestamp'], data['duration'], data['start'],
                     args.relative)
    if args.plot:
        mpld3.show(
            graph_timestamps(data['timestamp'], data['start'], data['end'],
                             args.relative))
    def plot_2d_graph(self, data_frame, gp, chart_type):
        if gp.resample is not None:
            data_frame = data_frame.asfreq(gp.resample)

        # set the matplotlib style sheet & defaults
        matplotlib.rcdefaults()

        # first search PyThalesians styles, then try matplotlib
        try:
            plt.style.use(Constants().plotfactory_pythalesians_style_sheet[gp.style_sheet])
        except:
            plt.style.use(gp.style_sheet)

        matplotlib.rcParams.update({"font.size": matplotlib.rcParams["font.size"] * gp.scale_factor})

        # create figure & add a subplot
        fig = plt.figure(
            figsize=((gp.width * gp.scale_factor) / gp.dpi, (gp.height * gp.scale_factor) / gp.dpi), dpi=gp.dpi
        )
        ax = fig.add_subplot(111)

        # format Y axis
        y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False)
        ax.yaxis.set_major_formatter(y_formatter)

        # create a second y axis if necessary
        ax2 = []

        if gp.y_axis_2_series != []:
            ax2 = ax.twinx()

            # do not use a grid with multiple y axes
            ax.yaxis.grid(False)
            ax2.yaxis.grid(False)

        # plot the lines (using custom palettes as appropriate)
        try:
            # get all the correct colors (and construct gradients if necessary eg. from 'blues')
            color_spec = self.create_color_list(gp, data_frame)

            if type == "bar":
                # bottom = np.cumsum(np.vstack((np.zeros(data_frame.values.shape[1]), data_frame.values)), axis=0)[:-1]
                # bottom = np.vstack((np.zeros((data_frame.values.shape[1],), dtype=data_frame.dtype),
                #                    np.cumsum(data_frame.values, axis=0)[:-1]))
                yoff = np.zeros(len(data_frame.index.values))  # the bottom values for stacked bar chart

            # some lines we should exclude from the color and use the default palette
            for i in range(0, len(data_frame.columns.values)):

                if chart_type is not None:
                    if gp.chart_type is not None:
                        if isinstance(gp.type, list):
                            chart_type = gp.chart_type[i]
                        else:
                            chart_type = gp.chart_type

                label = str(data_frame.columns[i])

                ax_temp = self.get_axis(ax, ax2, label, gp.y_axis_2_series)

                xd = data_frame.index
                yd = data_frame.ix[:, i]

                if chart_type == "line":
                    linewidth_t = self.get_linewidth(label, gp.linewidth, gp.linewidth_2, gp.linewidth_2_series)

                    if linewidth_t is None:
                        linewidth_t = matplotlib.rcParams["axes.linewidth"]

                    ax_temp.plot(xd, yd, label=label, color=color_spec[i], linewidth=linewidth_t)
                elif chart_type == "bar":
                    ax_temp.bar(xd, yd, label=label, color=color_spec[i], bottom=yoff)
                    yoff = yoff + yd

                elif chart_type == "scatter":
                    ax_temp.scatter(xd, yd, label=label, color=color_spec[i])

                    if gp.line_of_best_fit is True:
                        self.trendline(
                            ax_temp,
                            xd.values,
                            yd.values,
                            order=1,
                            color=color_spec[i],
                            alpha=1,
                            scale_factor=gp.scale_factor,
                        )
        except:
            pass

        # format X axis
        self.format_x_axis(ax, data_frame, gp)

        fig.suptitle(gp.title, fontsize=14 * gp.scale_factor)

        if gp.display_source_label == True:
            ax.annotate(
                "Source: " + gp.source,
                xy=(1, 0),
                xycoords="axes fraction",
                fontsize=7 * gp.scale_factor,
                xytext=(-5 * gp.scale_factor, 10 * gp.scale_factor),
                textcoords="offset points",
                ha="right",
                va="top",
                color=gp.source_color,
            )

        if gp.display_brand_label == True:
            self.create_brand_label(ax, anno=gp.brand_label, scale_factor=gp.scale_factor)

        leg = []
        leg2 = []

        loc = "best"

        # if we have two y-axis then make sure legends are in opposite corners
        if ax2 != []:
            loc = 2

        try:
            leg = ax.legend(loc=loc, prop={"size": 10 * gp.scale_factor})
            leg.get_frame().set_linewidth(0.0)
            leg.get_frame().set_alpha(0)

            if ax2 != []:
                leg2 = ax2.legend(loc=1, prop={"size": 10 * gp.scale_factor})
                leg2.get_frame().set_linewidth(0.0)
                leg2.get_frame().set_alpha(0)
        except:
            pass

        try:
            if gp.display_legend is False:
                if leg != []:
                    leg.remove()
                if leg2 != []:
                    leg.remove()
        except:
            pass

        try:
            plt.savefig(gp.file_output, transparent=False)
        except:
            pass

        ####### various matplotlib converters are unstable
        # convert to D3 format with mpld3
        try:
            if gp.display_mpld3 == True:
                mpld3.save_d3_html(fig, gp.html_file_output)
                mpld3.show(fig)
        except:
            pass

        # FRAGILE! convert to Bokeh format
        # better to use direct Bokeh renderer
        try:
            if gp.convert_matplotlib_to_bokeh == True:
                from bokeh.plotting import output_file, show
                from bokeh import mpl

                output_file(gp.html_file_output)
                show(mpl.to_bokeh())
        except:
            pass

        # FRAGILE! convert matplotlib chart to Plotly format
        # recommend using AdapterCufflinks instead to directly plot to Plotly
        try:
            if gp.convert_matplotlib_to_plotly == True:
                plotly.tools.set_credentials_file(username=gp.plotly_username, api_key=gp.plotly_api_key)

                py_fig = tls.mpl_to_plotly(fig, strip_style=True)
                plot_url = py.plot_mpl(py_fig, filename=gp.plotly_url)
        except:
            pass

        # display in Matplotlib
        try:
            if gp.silent_display == False:
                plt.show()
        except:
            pass
Exemple #51
0
def graphData(stock, MA1, MA2, dateRange=365):
    try:
        # variable for txt file, read in next part
        stockFile = stock+'.txt'
        
        # multiple variables: Date,close, high, low, open, volume
        # for the first element[0], date, read it as YearMonthDay
        date, closep, highp, lowp, openp, volume = np.loadtxt(stockFile, delimiter=',', unpack=True, converters={0:mdates.strpdate2num('%Y%m%d')})
        
        
        # Using the data read in to put into candlestick array
        x = 0 
        y = len(date)
        candleAr = []
        while x<y:
            appendLine = date[x],openp[x],closep[x],highp[x],lowp[x],volume[x]
            candleAr.append(appendLine)
            x+=1

        # using close data with either 12 or 26 day moving average
        Av1 = movingaverage(closep, MA1)
        Av2 = movingaverage(closep, MA2)
        # Starting point of data since we need at least 12 or 26 data points to get an SMAS
        SP = len(date[MA2-1:])
        
        label1=str(MA1)+' SMA'
        label2=str(MA2)+' SMA'
       
        # background color of .png
        #fig = plt.figure(facecolor = '#07000d')
        fig = plt.figure(facecolor = '#00355A')
        # Version 1.0
        # How many graphs appear on png, in this case, it has place for 2 and we are choosing slot 1
        # ax1 = plt.subplot(2,1,1)
        
        # Making the graph format more detailed
        ax1 = plt.subplot2grid((5,4), (0,0), rowspan=4, colspan=4, axisbg='#30414D') 
        #Black = #07000d

        # Calling the Candlestick function made from the previous while loop
        candlestick(ax1, candleAr, width=0.7, colorup='#9eff15', colordown='#ff1717') 

        # Graphing the date vs moving average starting from SP to current
        ax1.plot(date[-SP:],Av1[-SP:],'#5998ff', label = label1, linewidth=1.5)
        ax1.plot(date[-SP:],Av2[-SP:],'#e1edf9', label = label2, linewidth=1.5)

        #ax1.grid(True)
        # Changes color to white
        #ax1.grid(True, color='w')
        #ax1.set_xticklabels(mdates.num2date(date))
        # Shows a max of 10 dates on x-axis, formats it it Year-Month-Day
        #ax1.xaxis.set_major_locator(mticker.MaxNLocator(10))
        #ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))

        # Changes to lighter color
        ax1.yaxis.label.set_color("black")
        ax1.spines['bottom'].set_color("#5998ff")
        ax1.spines['top'].set_color("#5998ff")
        ax1.spines['left'].set_color("#5998ff")
        ax1.spines['right'].set_color("#5998ff")
        ax1.tick_params(axis='y', colors = 'black')
        #ax1.xlim()
        
        plt.ylabel('Stock price')
        
        maLeg = plt.legend(loc=9, ncol=2, prop={'size':10}, fancybox=True)
        maLeg.get_frame().set_alpha(0.4)
               
        # Later, used to tell filler the minimum to fill to.
        volumeMin = 0 #volume.min()
        
        # Making the graph format more detailed, also make it black
        ax2 = plt.subplot2grid((5,4), (4,0), sharex=ax1, rowspan=4, colspan=4, axisbg='#30414D') 
        #Black = #07000d
        
        # Changes the bars into teal colored, and facecolored with some fill
        ax2.plot(date,volume, '#00ffe8', linewidth = .8)
        # Using the volumeMin to start as bottom, for stylistic purpose
        ax2.fill_between(date, volumeMin, volume, facecolor='#00ffe8', alpha=0.5)

        ax2.axes.yaxis.set_ticklabels([])
        ax2.grid(True)
        
        ax2.spines['bottom'].set_color("#5998ff")
        ax2.spines['top'].set_color("#5998ff")
        ax2.spines['left'].set_color("#5998ff")
        ax2.spines['right'].set_color("#5998ff")
        ax2.tick_params(axis='x', colors = 'black')
        ax2.tick_params(axis='y', colors = 'black')

        ax1.xaxis_date()
        ax2.xaxis_date()

        
        # rotate the date on x-axis for visual appeasement 
        # This is redundant, as we don't need this, it is remained blacked out
        for label in ax1.xaxis.get_ticklabels():
            label.set_rotation(90)

        # Changed the Volume label to white form Version 1.0, and shifted left
        plt.ylabel('Volume', color='black', labelpad=20)
        
        for label in ax2.xaxis.get_ticklabels():
            label.set_rotation(45)
        
        sortDate(date,dateRange)
        # all adjustments on format of graph png
        plt.subplots_adjust(left=.10, bottom=.15, right=.93, top=.95, wspace=.20, hspace=0)
        plt.xlabel('Date')
        #Version 1.0, adjusted above to match the new scheme of png.
        #plt.ylabel('Volume')
        plt.suptitle(stock+' Stock Price',color='black')
        plt.setp(ax1.get_xticklabels(), visible=False)
        plt.setp(ax2.get_yticklabels(), visible=False)
        #show(plt)        
        #plt.show() # show the plot
        mpld3.show()
        fig.savefig('example.png', facecolor=fig.get_facecolor())
        
    except Exception, e:
        print 'failed main loop',str(e)
def func(ra, dec, mag, fov):

    # sorting objects under the user input of limiting magnitude

    mag_ng = ng[(ng["V"] <= mag)]
    mag_ms = ms[(ms['V'] <= mag)]
    mag_ty1 = ty1[(ty1['V'] <= mag)]
    mag_ty2 = ty2[(ty2['V'] <= mag)]

    # breathing space around the fov circle in the plot
    xl = ra - fov / 2 - fov / 10
    xr = ra + fov / 2 + fov / 10
    yb = dec - fov / 2 - fov / 10
    yt = dec + fov / 2 + fov / 10

    fig, ax = plt.subplots(figsize=(15, 7))
    ax.set_xlabel('Right Ascension (degrees)', fontsize=20)
    ax.set_ylabel('Declination (degrees)', fontsize=20)
    ax.scatter(ra, dec, s=50, marker='P', color='yellow', zorder=10)

    # sorting objects in messier_objects.csv according to objects
    cl_ms = ms[(ms["TYPE"] == 'OpC') | (ms["TYPE"] == 'GlC') |
               (ms["TYPE"] == 'Cl*')]
    pn_ms = ms[(ms["TYPE"] == 'PN')]
    ga_ms = ms[(ms["TYPE"] == 'G') | (ms["TYPE"] == 'Sy2') |
               (ms["TYPE"] == 'IG') | (ms["TYPE"] == 'GiG') |
               (ms["TYPE"] == 'GiP') | (ms["TYPE"] == 'SyG') |
               (ms["TYPE"] == 'SBG') | (ms["TYPE"] == 'BiC') |
               (ms["TYPE"] == 'H2G')]
    re_ms = ms[(ms["TYPE"] == 'HII') | (ms["TYPE"] == 'As*') |
               (ms["TYPE"] == 'LIN') | (ms["TYPE"] == 'mul') |
               (ms["TYPE"] == 'RNe') | (ms["TYPE"] == 'AGN')]

    print(
        f"Observing RA: {ra} deg, DEC: {dec} deg, FoV: {fov} deg, Limiting Magnitude: {mag}"
    )

    ax.set_axisbelow(True)
    ax.minorticks_on()
    ax.grid(which='major', alpha=0.3)
    ax.grid(which='minor',
            linestyle=':',
            linewidth='0.5',
            color='black',
            alpha=0.3)
    ax.set_facecolor('teal')

    # scatter messier
    mag = cl_ms["V"].fillna(1)
    flux = 10**(-mag / 2.5)
    p1 = ax.scatter(cl_ms['RAJ2000'],
                    cl_ms['DEJ2000'],
                    color='darkorange',
                    s=17,
                    zorder=10,
                    edgecolor="black")
    l1 = [
        "[RA {0:.5f} \v DEC {1:.5f} \v Constellation {2} \v Name {3}]".format(
            i, j, k, l, '.2f', '.2f', '%s', '%s')
        for i, j, k, l in zip(cl_ms['RAJ2000'], cl_ms['DEJ2000'],
                              cl_ms['Constellation'], cl_ms["Common Name"])
    ]
    t1 = plugins.PointLabelTooltip(p1, l1)
    plugins.connect(fig, t1)

    mag = pn_ms["V"].fillna(1)
    flux = 10**(-mag / 2.5)
    p2 = ax.scatter(pn_ms['RAJ2000'],
                    pn_ms['DEJ2000'],
                    color='blue',
                    s=17,
                    zorder=10,
                    edgecolor="black")
    l2 = [
        "[RA {0:.5f} \v DEC {1:.5f} \v Constellation {2} \v Name {3}]".format(
            i, j, k, l, '.2f', '.2f', '%s', '%s')
        for i, j, k, l in zip(pn_ms['RAJ2000'], pn_ms['DEJ2000'],
                              pn_ms['Constellation'], pn_ms["Common Name"])
    ]
    t2 = plugins.PointLabelTooltip(p2, l2)
    plugins.connect(fig, t2)

    mag = ga_ms["V"].fillna(1)
    flux = 10**(-mag / 2.5)
    p3 = ax.scatter(ga_ms['RAJ2000'],
                    ga_ms['DEJ2000'],
                    color='red',
                    s=17,
                    zorder=20,
                    edgecolor="black")
    l3 = [
        "[RA {0:.5f} \v DEC {1:.5f} \v Constellation {2} \v Name {3}]".format(
            i, j, k, l, '.2f', '.2f', '%s', "%s")
        for i, j, k, l in zip(ga_ms['RAJ2000'], ga_ms['DEJ2000'],
                              ga_ms['Constellation'], ga_ms["Common Name"])
    ]
    t3 = plugins.PointLabelTooltip(p3, l3)
    plugins.connect(fig, t3)

    mag = re_ms["V"].fillna(1)
    flux = 10**(-mag / 2.5)
    p4 = ax.scatter(re_ms['RAJ2000'],
                    re_ms['DEJ2000'],
                    c='darkmagenta',
                    s=17,
                    edgecolor="black")
    l4 = [
        "[RA {0:.5f} \v DEC {1:.5f} \v Constellation {2} \v Name {3}]".format(
            i, j, k, l, '.2f', '.2f', '%s', '%s')
        for i, j, k, l in zip(re_ms['RAJ2000'], re_ms['DEJ2000'],
                              re_ms['Constellation'], re_ms["Common Name"])
    ]
    t4 = plugins.PointLabelTooltip(p4, l4)
    plugins.connect(fig, t4)

    # scatter ngc
    mag = mag_ng['V'].fillna(1)
    flux = 10**(-mag / 2.5)
    p5 = ax.scatter(mag_ng['RAJ2000'],
                    mag_ng['DEJ2000'],
                    c='darkmagenta',
                    s=80 * flux)
    l5 = [
        "[RA {0:.5f} \v DEC {1:.5f} \v Constellation {2} \v Name {3}]".format(
            i, j, k, l, '.2f', '.2f', '%s', '%s')
        for i, j, k, l in zip(mag_ng['RAJ2000'], mag_ng['DEJ2000'],
                              mag_ng['Constellation'], mag_ng['Name'])
    ]
    t5 = plugins.PointLabelTooltip(p5, l5)
    plugins.connect(fig, t5)

    # scatter tycho-1
    mag = mag_ty1['V'].fillna(1)
    flux = 10**(-mag / 2.5)
    p6 = ax.scatter(mag_ty1['RAJ2000'],
                    mag_ty1['DEJ2000'],
                    c='white',
                    s=80 * flux)
    l6 = [
        "[RA {0:.5f} \v DEC {1:.5f} \v Constellation {2} \v Name {3}]".format(
            i, j, k, l, '.2f', '.2f', '%s', '%s')
        for i, j, k, l in zip(mag_ty1['RAJ2000'], mag_ty1['DEJ2000'],
                              mag_ty1['Constellation'], mag_ty1['Name'])
    ]
    t6 = plugins.PointLabelTooltip(p6, l6)
    plugins.connect(fig, t6)

    # scatter tycho-2
    # mag = mag_ty2['V'].fillna(1)
    # flux = 10**(-mag/2.5)
    # p7 = ax.scatter(mag_ty2['RAJ2000'], mag_ty2['DEJ2000'], c='white', s= 80*flux)
    # l7 = ["[RA {0:.5f} \v DEC {1:.5f} \v Constellation {2} ]".format(i,j,k,'.2f','.2f','%s') for i,j,k in zip(mag_ty2['RAJ2000'],mag_ty2['DEJ2000'],mag_ty2['Constellation'])]
    # t7 = plugins.PointLabelTooltip(p7,l7)
    # plugins.connect(fig, t7)

    points = [p1, p2, p3, p4, p5, p6]
    labels = [l1, l2, l3, l4, l5, l6]
    plugins.connect(fig, ClickInfo(points, labels))

    # constellation borders
    for i in cb['Constellation'].unique():
        x = cb[(cb['Constellation'] == i)]['RAJ2000']
        y = cb[(cb['Constellation'] == i)]['DEJ2000']
        x1 = x.mean()
        y1 = y.mean()
        ax.scatter(x, y, color='darkgreen', s=20 / fov)
        ax.annotate('%s' % i, (x1, y1), size=13)

    ax.add_artist(plt.Circle((ra, dec), color='#00af08', zorder=1, alpha=0.5))
    # ax.set_xlim([xl,xr])
    # ax.set_ylim([yb,yt])

    cluster = mlines.Line2D([], [],
                            color='darkorange',
                            marker=cl,
                            linestyle='None',
                            markersize=15,
                            label='Clusters',
                            markeredgecolor="black")
    planetary_neb = mlines.Line2D([], [],
                                  color='blue',
                                  marker=cl,
                                  linestyle='None',
                                  markersize=15,
                                  label='Planetary Nebula',
                                  markeredgecolor="black")
    galaxy = mlines.Line2D([], [],
                           color='Red',
                           marker=cl,
                           linestyle='None',
                           markersize=15,
                           label='Galaxies',
                           markeredgecolor="black")
    other = mlines.Line2D([], [],
                          color='darkmagenta',
                          marker=cl,
                          linestyle='None',
                          markersize=15,
                          label='Other NGC/Messier',
                          markeredgecolor="black")
    stars = mlines.Line2D([], [],
                          color='white',
                          marker=cl,
                          linestyle='None',
                          markersize=15,
                          label='Stars',
                          markeredgecolor="black")
    plt.legend(handles=[cluster, planetary_neb, galaxy, other, stars],
               labelspacing=2,
               ncol=5,
               borderpad=1,
               loc='lower center')

    mpld3.show()
Exemple #53
0
def plotTop40(config, catalog):
    from datetime import datetime
    from epiweeks import Week
    
    pivot = 'artist'
    # we rank artists by most plays, take the top 25 and 
    # add their played tracks to the playlist.
    #pivot = config['pivot'] if 'pivot' in config else 'artist'

    result = {}
    print(f'Grouping by {pivot}')
    for r in catalog:
        if r['track'] is None:
            continue
        artistid = r[pivot]['name']
        if pivot == 'track':
            artistid += ';;'+r['artist']['name']
        week = Week.fromdate(datetime.strptime(r['airdate'], '%Y-%m-%dT%H:%M:%SZ'))
        if week not in result:
            result[week]={}
        if artistid not in result[week]:
            result[week][artistid]={'track':r, 'plays':set(), 'songs':set()}
        
        result[week][artistid]['plays'].add(r['airdate']) # count by unique timestamps. Sometimes the playlist has duplicates.
        result[week][artistid]['songs'].add((r['artist']['name'],r['track']['name']))
    print([w for w in result])
    
    all_results = result
    if 0:
        all_artists = []
        plots = {}
        N=10
        W=20
        weeks = list(sorted(all_results))[-W:]
        for w in weeks:
            result = all_results[w]
            topN = list(sorted(result, key=lambda x: len(result[x]['plays']), reverse=True))[:N]
            print(topN)
            for i,a in enumerate(topN):
                if a not in all_artists:
                    all_artists.append(a)
                if a not in plots:
                    plots[a]=[]
                plots[a].append((w,i))
            #ranks.append(topN)
        y0_values = dict((k,N-v+1) for v,k in enumerate(all_artists))
        x_values = dict([(k,v+1) for (v,k) in enumerate(weeks)])
        import matplotlib.pyplot as plt
        from math import isnan
        fig, ax = plt.subplots(figsize=(12,8)) #subplot_kw=dict(axisbg='#EEEEEE'))
        for a in plots:
            lookup = dict(plots[a])
            X = [x_values[w] for w in weeks] #[x_values[w] for (w,_) in plots[a]]
            Y = [N-lookup[w] if w in lookup else float('NaN') for w in weeks] #N-v for (_,v) in plots[a]]
            ax.plot(X, Y, 'o-')
            for i in range(len(X)):
                if i==0 or (not isnan(Y[i]) and isnan(Y[i-1])):
                    ax.text(X[i],Y[i]+0.15, a, ha='center', fontsize=8)
            ax.set_yticks(range(N+1))
            ax.set_yticklabels(['']+[str(N-y) for y in range(N)])
        plt.show()

    threshold = Week.fromdate(datetime(2020,5,26))
    summary = {}
    from collections import Counter
    denom = Counter()
    for w in all_results:
        isprior = w<threshold
        denom[isprior]+=1
        current = all_results[w]
        for a in current:
            if a not in summary:
                summary[a]=Counter()
            summary[a][isprior]+=len(current[a]['plays'])
    import matplotlib.pyplot as plt
    fig, ax = plt.subplots(figsize=(12,8)) 
    labels = []
    N=20
    for i,a in enumerate(list(sorted(summary, key = lambda x: summary[x][True]+summary[x][False]))[-N:]):
        ax.plot([2*i-0.5,2*i+0.5], [summary[a][True]/denom[True], summary[a][False]/denom[False]],'o-')
        labels.append(a)
        print('{}\t{}\t{}'.format(a, summary[a][True]/denom[True], summary[a][False]/denom[False]))
    ax.set_xticks([2*i for i in range(N)])
    ax.set_xticklabels(labels, rotation=90)
    ax.set_ylabel('Plays per Week')
    plt.tight_layout()
    plt.savefig('playsPerWeek.png')
    plt.show()

    if 0:
        import mpld3
        from math import log, ceil

        data = [(x,y,ctr[(x,y)]) for (x,y) in ctr]
        x_track=[d[0] for d in data]
        y_artist=[d[1] for d in data]
        count = [len(d[2]) for d in data]
        labels = [','.join(d[2]) for d in data]
        
        fig, ax = plt.subplots(figsize=(4,2)) #subplot_kw=dict(axisbg='#EEEEEE'))
        scatter = ax.scatter(x_track, y_artist, s=[3*log(c+1) for c in count])
        xint = range(min(x_track), ceil(max(x_track))+1)
        #ax.set_xticks(xint)
        ax.set_xlabel('Track Plays')
        ax.set_ylabel('Artist Plays')
        tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=labels)
        mpld3.plugins.connect(fig, tooltip)
        mpld3.show()
    def plot_2d_graph(self, data_frame, gp, chart_type):

        if gp is None: gp = GraphProperties()
        if gp.chart_type is None and chart_type is None: chart_type = 'line'

        if gp.resample is not None: data_frame = data_frame.asfreq(gp.resample)

        self.apply_style_sheet(gp)

        # create figure & add a subplot
        fig = plt.figure(figsize = ((gp.width * gp.scale_factor)/gp.dpi,
                                    (gp.height * gp.scale_factor)/gp.dpi), dpi = gp.dpi)

        ax = fig.add_subplot(111)

        if gp.x_title != '': ax.set_xlabel(gp.x_title)
        if gp.y_title != '': ax.set_ylabel(gp.y_title)

        plt.xlabel(gp.x_title)
        plt.ylabel(gp.y_title)

        fig.suptitle(gp.title, fontsize = 14 * gp.scale_factor)

        # format Y axis
        y_formatter = matplotlib.ticker.ScalarFormatter(useOffset = False)
        ax.yaxis.set_major_formatter(y_formatter)

        # create a second y axis if necessary
        ax2 = []

        if gp.y_axis_2_series != []:
            ax2 = ax.twinx()

            # do not use a grid with multiple y axes
            ax.yaxis.grid(False)
            ax2.yaxis.grid(False)

        # matplotlib 1.5
        try:
            cyc = matplotlib.rcParams['axes.prop_cycle']
            color_cycle =  [x['color'] for x in cyc]
        except KeyError:
            # pre 1.5
            pass
            # color_cycle =  matplotlib.rcParams['axes.color_cycle']

        bar_ind = np.arange(0, len(data_frame.index))

        # for bar charts, create a proxy x-axis (then relabel)
        xd, bar_ind, has_bar, no_of_bars = self.get_bar_indices(data_frame, gp, chart_type, bar_ind)

        # plot the lines (using custom palettes as appropriate)
        try:
            # get all the correct colors (and construct gradients if necessary eg. from 'blues')
            color_spec = self.create_color_list(gp, data_frame)

            # for stacked bar
            yoff_pos = np.zeros(len(data_frame.index.values)) # the bottom values for stacked bar chart
            yoff_neg = np.zeros(len(data_frame.index.values)) # the bottom values for stacked bar chart

            zeros = np.zeros(len(data_frame.index.values))

            # for bar chart
            bar_space = 0.2
            bar_width = (1 - bar_space) / (no_of_bars)
            bar_index = 0

            has_matrix = False

            # some lines we should exclude from the color and use the default palette
            for i in range(0, len(data_frame.columns.values)):

                if gp.chart_type is not None:
                    if isinstance(gp.chart_type, list):
                        chart_type = gp.chart_type[i]
                    else:
                        chart_type = gp.chart_type

                if chart_type == 'heatmap':
                    # TODO experimental!
                    # ax.set_frame_on(False)
                    ax.pcolor(data_frame, cmap=plt.cm.Blues, alpha=0.8)
                    # plt.colorbar()
                    has_matrix = True
                    break

                label = str(data_frame.columns[i])

                ax_temp = self.get_axis(ax, ax2, label, gp.y_axis_2_series)

                yd = data_frame.ix[:,i]

                if color_spec[i] is None:
                    color_spec[i] = color_cycle[i % len(color_cycle)]

                if (chart_type == 'line'):
                    linewidth_t = self.get_linewidth(label,
                                                     gp.linewidth, gp.linewidth_2, gp.linewidth_2_series)

                    if linewidth_t is None: linewidth_t = matplotlib.rcParams['axes.linewidth']

                    ax_temp.plot(xd, yd, label = label, color = color_spec[i],
                                     linewidth = linewidth_t)

                elif(chart_type == 'bar'):
                    # for multiple bars we need to allocate space properly
                    bar_pos = [k - (1 - bar_space) / 2. + bar_index * bar_width for k in range(0,len(bar_ind))]

                    ax_temp.bar(bar_pos, yd, bar_width, label = label, color = color_spec[i])

                    bar_index = bar_index + 1

                elif(chart_type == 'stacked'):
                    bar_pos = [k - (1 - bar_space) / 2. + bar_index * bar_width for k in range(0,len(bar_ind))]

                    yoff = np.where(yd > 0, yoff_pos, yoff_neg)

                    ax_temp.bar(bar_pos, yd, label = label, color = color_spec[i], bottom = yoff)

                    yoff_pos = yoff_pos + np.maximum(yd, zeros)
                    yoff_neg = yoff_neg + np.minimum(yd, zeros)

                    # bar_index = bar_index + 1

                elif(chart_type == 'scatter'):
                    ax_temp.scatter(xd, yd, label = label, color = color_spec[i])

                    if gp.line_of_best_fit is True:
                        self.trendline(ax_temp, xd.values, yd.values, order=1, color= color_spec[i], alpha=1,
                                           scale_factor = gp.scale_factor)

            # format X axis
            self.format_x_axis(ax, data_frame, gp, has_bar, bar_ind, has_matrix)

        except: pass

        if gp.display_source_label == True:
            ax.annotate('Source: ' + gp.source, xy = (1, 0), xycoords='axes fraction', fontsize=7 * gp.scale_factor,
                        xytext=(-5 * gp.scale_factor, 10 * gp.scale_factor), textcoords='offset points',
                        ha='right', va='top', color = gp.source_color)

        if gp.display_brand_label == True:
            self.create_brand_label(ax, anno = gp.brand_label, scale_factor = gp.scale_factor)

        leg = []
        leg2 = []

        loc = 'best'

        # if we have two y-axis then make sure legends are in opposite corners
        if ax2 != []: loc = 2

        try:
            leg = ax.legend(loc = loc, prop={'size':10 * gp.scale_factor})
            leg.get_frame().set_linewidth(0.0)
            leg.get_frame().set_alpha(0)

            if ax2 != []:
                leg2 = ax2.legend(loc = 1, prop={'size':10 * gp.scale_factor})
                leg2.get_frame().set_linewidth(0.0)
                leg2.get_frame().set_alpha(0)
        except: pass

        try:
            if gp.display_legend is False:
                if leg != []: leg.remove()
                if leg2 != []: leg.remove()
        except: pass

        try:
            plt.savefig(gp.file_output, transparent=False)
        except: pass


        ####### various matplotlib converters are unstable
        # convert to D3 format with mpld3
        try:
            # output matplotlib charts externally to D3 based libraries
            import mpld3

            if gp.display_mpld3 == True:
                mpld3.save_d3_html(fig, gp.html_file_output)
                mpld3.show(fig)
        except: pass

        # FRAGILE! convert to Bokeh format
        # better to use direct Bokeh renderer
        try:
            if (gp.convert_matplotlib_to_bokeh == True):
                from bokeh.plotting import output_file, show
                from bokeh import mpl

                output_file(gp.html_file_output)
                show(mpl.to_bokeh())
        except: pass

        # FRAGILE! convert matplotlib chart to Plotly format
        # recommend using AdapterCufflinks instead to directly plot to Plotly
        try:
            import plotly.plotly as py
            import plotly
            import plotly.tools as tls

            if gp.convert_matplotlib_to_plotly == True:
                plotly.tools.set_credentials_file(username = gp.plotly_username,
                                                  api_key = gp.plotly_api_key)

                py_fig = tls.mpl_to_plotly(fig, strip_style = True)
                plot_url = py.plot_mpl(py_fig, filename = gp.plotly_url)
        except:
            pass

        # display in matplotlib window
        try:
            if GraphicsConstants.plotfactory_silent_display == True:
                return fig
            elif gp.silent_display == False:
                plt.show()
            else:
                return fig

        except:
            pass
Exemple #55
0
def graph_data(stock):
    #fig = plt.figure()
    #fig.patch.set_facecolor('#DAD1CF')

    stockFile = stock + '.txt'
    string = str('%Y%m%d%H%M%S')

    if period == 'INTRADAY':
        date, closep, highp, lowp, openp, volume = np.loadtxt(
            stockFile,
            delimiter=',',
            unpack=True,
            converters={0: bytespdate2num('%Y%m%d%H%M%S')})
        pass
    else:
        date, closep, highp, lowp, openp, volume = np.loadtxt(
            stockFile,
            delimiter=',',
            unpack=True,
            converters={0: bytespdate2num('%Y%m%d')})

    ohlc = []
    ax1 = plt.subplot2grid((6, 4), (0, 0), rowspan=5, colspan=4)
    plt.ylabel('Price ($)')
    plt.title(stock + ' (' + time.strftime("%m/%d/%Y") + ')')

    ax2 = plt.subplot2grid((6, 4), (5, 0), rowspan=1, colspan=4, sharex=ax1)

    for x in range(len(date)):
        appendLine = date[x], openp[x], highp[x], lowp[x], closep[x], volume[x]
        ohlc.append(appendLine)

    if period == 'INTRADAY':
        wideness = 0.0005
    else:
        wideness = 0.2
    candlestick_ohlc(ax1,
                     ohlc,
                     width=wideness,
                     colorup='#77d879',
                     colordown='#db3f3f')

    if period == 'INTRADAY':
        string = str('%I:%m')
    elif period == '1y':
        string = str('%m/%d')
    else:
        string = str('%m/%d/%Y')
    ax1.xaxis.set_major_formatter(mdates.DateFormatter(string))
    ax2.xaxis.set_major_locator(mticker.MaxNLocator(12))
    ax2.yaxis.set_major_locator(mticker.MaxNLocator(nbins=2, prune='upper'))
    ax1.grid(True)
    ax2.grid(True)
    for label in ax2.xaxis.get_ticklabels():
        label.set_rotation(45)
    for label in ax1.xaxis.get_ticklabels():
        label.set_rotation(45)

    ax2.bar(date, (highp - lowp), width=0.0005)
    plt.xlabel('Date, period: ' + period)

    plt.subplots_adjust(left=0.09, bottom=0.20, right=0.94, top=0.90)
    plt.setp(ax1.get_xticklabels(), visible=False)

    print('graphing stock information ', end='')
    if outputType == 'WEBSITE':
        print('to website')
        mpld3.show()
    else:
        print('to terminal')
        plt.show()
Exemple #56
0
    x = np.linspace(-2, 2, 20)
    y = x[:, None]
    X = np.zeros((20, 20, 4))

    X[:, :, 0] = np.exp(- (x - 1) ** 2 - (y) ** 2)
    X[:, :, 1] = np.exp(- (x + 0.71) ** 2 - (y - 0.71) ** 2)
    X[:, :, 2] = np.exp(- (x + 0.71) ** 2 - (y + 0.71) ** 2)
    X[:, :, 3] = np.exp(-0.25 * (x ** 2 + y ** 2))

    im = ax.imshow(X, extent=(10, 20, 10, 20),
                   origin='lower', zorder=1, interpolation='nearest')
    fig.colorbar(im, ax=ax)

    ax.text(16, 16, "overlaid text")
    ax.text(16, 15, "covered text", zorder=0)

    ax.set_title('An Image', size=20)
    ax.set_xlim(9, 21)
    ax.set_ylim(9, 21)
    return fig


def test_imshow():
    fig = create_plot()
    html = mpld3.fig_to_html(fig)
    plt.close(fig)


if __name__ == "__main__":
    mpld3.show(create_plot())
Exemple #57
0
def PCA2d(sleppa, toflur):

    # prófa fyrst að taka bara costoflivingindex og veðrið í viku 30
    # töflurnar geta verið prices, costoflivingindex, qualityoflifeindex, weathereurope og weatherchanceseurope

    pca = PCA(n_components=2)
    fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'), figsize=(9, 7))
    if (len(toflur) == 1):
        if "WeatherEurope" in toflur or "WeatherChancesEurope" in toflur:
            query = cur.execute("SELECT * FROM " + toflur[0] +
                                " where week = 20")
            cols = [column[0] for column in query.description]

            results = pd.DataFrame.from_records(data=query.fetchall(),
                                                columns=cols)

            cities = results['city'].tolist()

            #results.drop(sleppa[0], axis=1)
            dropColumns = ['week', 'city']
            if toflur[0] == "WeatherEurope":
                dropColumns.append('cloud_cover')
            results.drop(columns=dropColumns, inplace=True)
            results = results.apply(pd.to_numeric)
            resultsScaled = preprocessing.StandardScaler().fit_transform(
                results)

            principalComponentsScaled = pca.fit_transform(resultsScaled)
            principalDfScaled = pd.DataFrame(
                data=principalComponentsScaled,
                columns=['principal component 1', 'principal component 2'])

            N = len(cities)
            col = np.random.rand(N)

            print(
                pd.DataFrame(
                    pca.components_, columns=results.columns).sort_values(
                        axis=1, by=0,
                        ascending=False))  #.to_csv(path_or_buf="PCA.csv")
            # aðal meginþættirnir hafa verið prentaðir
            mynd = ax.scatter(principalDfScaled['principal component 1'],
                              principalDfScaled['principal component 2'],
                              c=col,
                              alpha=0.5)
            ax.set_title("2-dimensional PCA of weather in Europe in late June")

        if "CostOfLivingIndex" in toflur or "QualityOfLifeIndex" in toflur:

            query2 = cur.execute("SELECT * from " + toflur[0])
            cols = [column[0] for column in query2.description]

            results2 = pd.DataFrame.from_records(data=query2.fetchall(),
                                                 columns=cols)

            cities = results2['City'].tolist()

            #results2.drop(sleppa[1], axis=1)
            results2.drop(columns=['City', 'Rank'], inplace=True)

            results2 = results2.apply(pd.to_numeric)

            results2Scaled = preprocessing.StandardScaler().fit_transform(
                results2)
            principalComponentsScaled2 = pca.fit_transform(results2Scaled)
            principalDfScaled2 = pd.DataFrame(
                data=principalComponentsScaled2,
                columns=['principal component 1', 'principal component 2'])

            col = np.random.rand(len(cities))

            mynd = ax.scatter(principalDfScaled2['principal component 1'],
                              principalDfScaled2['principal component 2'],
                              c=col,
                              alpha=0.5)
            print(
                pd.DataFrame(
                    pca.components_, columns=results2.columns).sort_values(
                        axis=1, by=0,
                        ascending=False))  #.to_csv(path_or_buf="PCA.csv")
            # aðal meginþættirnir hafa verið prentaðir
            if ("CostOfLivingIndex" in toflur):
                ax.set_title("2-dimensional PCA of cost of living indeces")
            else:
                ax.set_title("2-dimensional PCA of quality of life indeces")
        if "Prices" in toflur:
            query3 = cur.execute("SELECT * from " + toflur[0])
            cols = [column[0] for column in query2.description]
            print(cols)
            results3 = pd.DataFrame.from_records(data=query3.fetchall(),
                                                 columns=cols)

            cities3 = results3['City'].tolist()
            irrelevantAttributes = [
                "City", "Monthly Pass", "Volkswagen Golf", "Toyota Corolla",
                "Basic Utilities", "Internet", "Fitness Club",
                "Tennis Court Rent", "Preschool Month", "Primary School Year",
                "Rent 1 Bedroom Center"
                "Rent 1 Bedroom Outside Center"
                "Rent 3 Bedrooms Center", "Rent 3 Bedrooms Outside Center",
                "Price m2 Center", "Price m2 Outside Center",
                "Average Monthly Salary After Tax", "Mortgage Interest Rate"
            ]
            #results3.drop(sleppa[1], axis=1)
            results3.drop(columns=irrelevantAttributes, inplace=True)

            results3 = results3.apply(pd.to_numeric)

            results3Scaled = preprocessing.StandardScaler().fit_transform(
                results3)
            principalComponentsScaled3 = pca.fit_transform(results3Scaled)
            principalDfScaled3 = pd.DataFrame(
                data=principalComponentsScaled3,
                columns=['principal component 1', 'principal component 2'])

            col = np.random.rand(len(cities3))

            mynd = ax.scatter(principalDfScaled3['principal component 1'],
                              principalDfScaled3['principal component 2'],
                              c=col,
                              alpha=0.5)
            ax.set_title("2-dimensional PCA of prices of goods")
            print(
                pd.DataFrame(
                    pca.components_, columns=results3.columns).sort_values(
                        axis=1, by=0,
                        ascending=False))  #.to_csv(path_or_buf="PCA.csv")
    # aðal meginþættirnir hafa verið prentaðir
    else:
        # sameina
        toflulisti = []

        for tafla in toflur:

            if tafla == "WeatherEurope" or tafla == "WeatherChancesEurope":
                query = cur.execute("SELECT * FROM " + tafla +
                                    " where week = 20")
                cols = [column[0] for column in query.description]

                results = pd.DataFrame.from_records(data=query.fetchall(),
                                                    columns=cols)

                cities = results['city'].tolist()

                results.drop(sleppa[0], axis=1)

                toflulisti.append(results)

            if tafla == "CostOfLivingIndex" or tafla == "QualityOfLifeIndex":

                query2 = cur.execute("SELECT * from " + tafla)
                cols = [column[0] for column in query2.description]

                results2 = pd.DataFrame.from_records(data=query2.fetchall(),
                                                     columns=cols)

                cities = results2['City'].tolist()
                results2.drop(sleppa[1], axis=1)
                results2.drop("Rank", axis=1, inplace=True)
                results2.rename(index=str,
                                columns={"City": "city"},
                                inplace=True)

                toflulisti.append(results2)

            if tafla == "Prices":
                print("vantar atm")

        saman = reduce((lambda results, results2: pd.merge(results,
                                                           results2,
                                                           how='inner',
                                                           left_on=['city'],
                                                           right_on=['city'])),
                       toflulisti)

        cities = list(saman["city"])

        samandrop = ['city', 'week', 'cloud_cover']
        saman.drop(columns=samandrop, inplace=True)

        samanScaled = preprocessing.StandardScaler().fit_transform(saman)

        principalComponentsScaledSaman = pca.fit_transform(samanScaled)
        principalDfScaledSaman = pd.DataFrame(
            data=principalComponentsScaledSaman,
            columns=['principal component 1', 'principal component 2'])
        col = np.random.rand(len(saman))

        mynd = ax.scatter(principalDfScaledSaman['principal component 1'],
                          principalDfScaledSaman['principal component 2'],
                          c=col,
                          alpha=0.5)
        print(
            pd.DataFrame(pca.components_, columns=saman.columns).sort_values(
                axis=1, by=0,
                ascending=False))  #.to_csv(path_or_buf="PCA.csv")
    # aðal meginþættirnir hafa verið prentaðir


# frekar en að hafa results, results2 o.s.frv. þá get ég haft lista/dict  results["WeatherEurope"], results["CostOfLivingIndex"]
# Prices og QualityOfLifeIndex er keimlíkt CostOfLivingIndex
# WeatherChancesEurope er keimlíkt WeatherEurope
# nema það þarf að droppa öðrum dálkum

    ax.set_xlabel('Principal Component 1', fontsize=15)
    ax.set_ylabel('Principal Component 2', fontsize=15)
    if (len(toflur) > 1):
        title = "2-dimensional PCA of " + toflur[0]
        i = 1
        for tafla in toflur[1:]:
            if i != (len(toflur) - 1):
                title = title + ", " + tafla + " "
            else:
                title = title + " and " + tafla
            i = i + 1
        ax.set_title(title)
    # vantar að staðfesta að röðin sé eins?

    tooltip = mpld3.plugins.PointLabelTooltip(mynd, labels=cities)

    mpld3.plugins.connect(fig, tooltip)

    mpld3.show()
"""Plot to test line styles"""
import matplotlib.pyplot as plt
import numpy as np
import mpld3
from mpld3 import plugins


def create_plot():
    fig, ax = plt.subplots()
    points = ax.plot(range(10), 'o', ms=20)
    plugins.connect(fig,
                    plugins.PointLabelTooltip(points[0], location="top left"))
    return fig


def test_tooltips_basic():
    fig = create_plot()
    html = mpld3.fig_to_html(fig)
    plt.close(fig)


if __name__ == "__main__":
    mpld3.show(create_plot())
Exemple #59
0
    def create_d3_visualisation(self):

        vectors_matrix = np.vstack(
            [nlp(text).vector for text in tqdm(self.texts)])
        self.dist = 1 - cosine_similarity(vectors_matrix)

        km = KMeans(n_clusters=self.num_clusters)
        km.fit(vectors_matrix)
        clusters = km.labels_.tolist()

        mds = MDS(n_components=2, dissimilarity="precomputed", random_state=1)
        pos = mds.fit_transform(self.dist)  # shape (n_components, n_samples)

        xs, ys = pos[:, 0], pos[:, 1]

        cluster_names = {i: f'Cluster {i}' for i in range(self.num_clusters)}
        text_label_and_text = [
            ': '.join(t) for t in list(zip(self.text_labels, self.texts))
        ]

        # create data frame that has the result of the MDS plus the cluster numbers and titles
        df = pd.DataFrame(
            dict(x=xs, y=ys, clusters=clusters, title=text_label_and_text))

        # group by cluster
        groups_clusters = df.groupby('clusters')

        # set up plot
        fig, ax = plt.subplots(figsize=(25, 15))  # set size
        ax.margins(0.05)  # Optional, just adds 5% padding to the autoscaling

        # iterate through groups to layer the plot
        # note that I use the cluster_name and cluster_color dicts with the 'name' lookup to return
        # the appropriate color/label
        for name, group in groups_clusters:
            ax.plot(group.x,
                    group.y,
                    marker='o',
                    linestyle='',
                    ms=12,
                    label=cluster_names[name],
                    color=self.cluster_color[name],
                    mec='none')
            ax.set_aspect('auto')
            ax.tick_params(
                axis='x',  # changes apply to the x-axis
                which='both',  # both major and minor ticks are affected
                bottom='off',  # ticks along the bottom edge are off
                top='off',  # ticks along the top edge are off
                labelbottom='off')
            ax.tick_params(
                axis='y',  # changes apply to the y-axis
                which='both',  # both major and minor ticks are affected
                left='off',  # ticks along the bottom edge are off
                top='off',  # ticks along the top edge are off
                labelleft='off')

        ax.legend(numpoints=1)  # show legend with only 1 point

        # add label in x,y position with the label as the
        for i in range(len(df)):
            ax.text(df.ix[i]['x'], df.ix[i]['y'], df.ix[i]['title'], size=8)

        # uncomment the below to save the plot if need be
        plt.savefig(
            f'clusters_static-{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}.png',
            dpi=200)

        # Plot
        fig, ax = plt.subplots(figsize=(20, 15))  # set plot size
        ax.margins(0.03)  # Optional, just adds 5% padding to the autoscaling

        # iterate through groups to layer the plot
        for name, group in groups_clusters:
            points = ax.plot(group.x,
                             group.y,
                             marker='o',
                             linestyle='',
                             ms=18,
                             label=cluster_names[name],
                             mec='none',
                             color=self.cluster_color[name])
            ax.set_aspect('auto')
            labels = [i for i in group.title]

            # set tooltip using points, labels and the already defined 'css'
            tooltip = mpld3.plugins.PointHTMLTooltip(points[0],
                                                     labels,
                                                     voffset=10,
                                                     hoffset=10,
                                                     css=CSS)
            # connect tooltip to fig
            mpld3.plugins.connect(fig, tooltip, TopToolbar())

            # set tick marks as blank
            ax.axes.get_xaxis().set_ticks([])
            ax.axes.get_yaxis().set_ticks([])

            # set axis as blank
            ax.axes.get_xaxis().set_visible(False)
            ax.axes.get_yaxis().set_visible(False)

        ax.legend(numpoints=1)  # show legend with only one dot
        mpld3.show()  # show the plot