Beispiel #1
0
def img_from_tex(tex, font_size=20):

    figure = matplotlib.figure.Figure(None, facecolor='white')
    #canvas = matplotlib.backends.backend_wxagg.FigureCanvasWxAgg(None, -1, figure)
    canvas = matplotlib.backends.backend_wxagg.FigureCanvasAgg(figure)

    font_size = font_size

    tex = "${0}$".format(tex)

    figure.clear()
    figure.text(0.05, 0.5, tex, size=font_size)
    canvas.draw()

    filename = 'equ.png'

    figure.savefig(filename, dpi=600)

    img = Image.open(filename)

    imginv = ImageChops.invert(img.convert('L'))

    box = imginv.getbbox()
    img = img.crop(box)

    img = ImageOps.expand(img, border=10, fill=(255, 255, 255))

    img.save(filename)

    return img
Beispiel #2
0
def tex2img(tex, filename="equ.png", font_size=20):

    font_size = 20

    figure = matplotlib.figure.Figure(None, facecolor=(0, 0, 0, 0))
    canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(figure)

    font_size = font_size

    tex = "${0}$".format(tex)

    figure.clear()
    figure.text(0.05, 0.5, tex, size=font_size)

    canvas.draw()

    png = "equ.png"

    figure.savefig(png, transparent=True, dpi=300, format="png")

    img = Image.open(png)

    imginv = ImageChops.invert(img.convert('L'))

    box = imginv.getbbox()

    img = img.crop(box)

    img = ImageOps.expand(img, border=10, fill=(255, 255, 255, 0))

    img.save(png, format="png")

    return img
def graphs():
    file_name = request.form["csv_file"]
    jess_adjustment = request.form["jess_adjustment"]
    pete_adjustment = request.form["pete_adjustment"]
    return render_template('underconstruction.html', do_not_display=123, csv_file= file_name, 
    jess_adjustment= jess_adjustment, pete_adjustment= pete_adjustment) #page under construction
    file_name = request.form["csv_file"]
    jess_adjustment = request.form["jess_adjustment"]
    pete_adjustment = request.form["pete_adjustment"]
    con = connect_to_db()
    cur = con.cursor()
    cur.execute("SELECT DISTINCT Category FROM {0};".format("May2019"))
    categories = cur.fetchall()
    categories2 = []
    for category in categories:
        categories2.append(category[0])
    totals = []
    category3 = []
    for category in categories2:
        cur.execute("SELECT SUM(Debit) FROM {0} WHERE Category = '{1}';".format("May2019", category))
        totals.append(cur.fetchall()[0][0])
        category3.append(category)

    figure = plt.figure(figsize=(12, 10))
    plt.plot(category3,totals)
    figure.savefig('static/images/myfig.png')
    return render_template('graphs.html', do_not_display=123, csv_file= file_name, 
    jess_adjustment= jess_adjustment, pete_adjustment= pete_adjustment)
    def _upload_plot(self, figure, name='plot.png'):
        buffer = io.BytesIO()

        figure.savefig(buffer, format="png")

        data = (name, buffer.getvalue(), 'image/png')

        self._upload(data)
Beispiel #5
0
def _figure_to_svg(figure):
    """Return the SVG representation of a figure."""

    old_canvas = figure.canvas
    matplotlib.backends.backend_svg.FigureCanvas(figure)
    output = io.BytesIO()
    figure.savefig(output, format='svg')
    figure.set_canvas(old_canvas)
    data = output.getvalue()
    return data.decode('utf-8')
Beispiel #6
0
def convert_plot_to_image(figure: figure.Figure) -> Image.Image:
    """
    Converts the specified matplotlib Figure into a PIL Image
    :param figure: Figure to convert
    :return: PIL Image
    """
    buf = io.BytesIO()
    figure.savefig(buf, format="png", facecolor="None")
    buf.seek(0)
    im = Image.open(buf)
    return im
Beispiel #7
0
def figure_to_image(figure, *args, **kwargs):
    """Convert a figure to a numpy array"""
    #
    # Save the figure as a .PNG and then load it using imageio.imread
    #
    fd = io.BytesIO()
    kwargs = kwargs.copy()
    kwargs["format"] = "png"
    figure.savefig(fd, *args, **kwargs)
    image = imageio.imread(fd.getvalue())
    return image[:, :, :3]
Beispiel #8
0
def figure_to_image(figure, *args, **kwargs):
    """Convert a figure to a numpy array"""
    #
    # Save the figure as a .PNG and then load it using imageio.imread
    #
    fd = six.moves.StringIO()
    kwargs = kwargs.copy()
    kwargs["format"] = "png"
    figure.savefig(fd, *args, **kwargs)
    fd.seek(0)
    image = imageio.imread(fd)
    return image[:, :, :3]
Beispiel #9
0
def draw_plot_to_file(file_name, *args, **kwds):
    """
  For offline plotting, which turns out to be necessary when running the
  status plot through pyana.
  """
    import matplotlib
    import matplotlib.figure
    from matplotlib.backends.backend_agg import FigureCanvasAgg
    figure = matplotlib.figure.Figure((16, 10))
    canvas = FigureCanvasAgg(figure)
    draw_plot(figure, *args, **kwds)
    canvas.draw()
    figure.savefig(file_name, format="png")
Beispiel #10
0
def draw_plot_to_file(file_name, *args, **kwds):
    """
  For offline plotting, which turns out to be necessary when running the
  status plot through pyana.
  """
    import matplotlib
    import matplotlib.figure
    from matplotlib.backends.backend_agg import FigureCanvasAgg

    figure = matplotlib.figure.Figure((16, 10))
    canvas = FigureCanvasAgg(figure)
    draw_plot(figure, *args, **kwds)
    canvas.draw()
    figure.savefig(file_name, format="png")
Beispiel #11
0
def save_pdf(
    figure: matplotlib.figure.Figure,
    path: Union[str, Path],
    crop: bool = True,
    optimize: bool = True,
):
    path = make_path(path)

    figure.savefig(path)

    if crop:
        crop_pdf(path)

    if optimize:
        optimize_pdf(path)
Beispiel #12
0
def save(
    figure: matplotlib.figure.Figure,
    path: Union[str, Path],
    crop: bool = False,
    optimize: bool = False,
):
    path = make_path(path)

    if path.suffix == ".pdf":
        save_pdf(figure, path, crop, optimize)
        return

    if crop:
        figure.savefig(path, bbox_inches="tight")
    else:
        figure.savefig(path)
Beispiel #13
0
def _figure_to_svg(figure):
    """Return the SVG representation of a figure."""

    old_canvas = figure.canvas
    matplotlib.backends.backend_svg.FigureCanvas(figure)
    output = io.BytesIO()
    figure.savefig(output, format='svg')
    figure.set_canvas(old_canvas)
    data = output.getvalue()
    decoded_data = data.decode('utf-8')

    new_data = svg_width_regex.sub(svg_width_replacement,
                                   decoded_data,
                                   count=1)
    new_data = svg_height_regex.sub(svg_height_replacement, new_data, count=1)

    return new_data
Beispiel #14
0
def SavePlot(figure: matplotlib.figure.Figure, directory: str, name: str, filetype: str):
    """
    Save matplotlib figure to a file

    :param figure: matplotlib figure
    :param directory: directory to the file
    :param name: name of the file
    :param filetype: file type of saved figure (only support png and svg)
    :return:
    """
    fileName = directory + name
    print("Plot " + name + " is save to file: " + fileName + ".")
    if filetype == 'svg':
        figure.savefig(fileName + '.svg', format='svg')
    elif filetype == 'png':
        figure.savefig(fileName + '.png', format='png')
    else:
        print("File type " + filetype + " is not supported by PlotHelper.")
Beispiel #15
0
    def render(self, directory):
        try:
            import matplotlib.figure
            import matplotlib.cm
        except ImportError as ex:
            sys.stderr.write("not writing out image: {}".format(ex))
            return

        squashed = self.squashed
        if squashed is None:
            return

        name = 'clusters.png'
        labels = set(self.clusters.keys())
        colors = [
            matplotlib.cm.Spectral(each)
            for each in numpy.linspace(0, 1, len(labels))
        ]

        figure = matplotlib.figure.Figure(figsize=(16, 12))
        axes = figure.gca()

        def plot(cluster, color, marker):
            first = True
            for point in cluster.points:
                (x, y) = squashed[point]
                if first and cluster.label:  # The first point gets a label
                    axes.annotate(str(cluster.label), (x + 0.01, y))
                first = False
                axes.scatter(x, y, color=color, marker=marker, alpha=0.4)

        # Plot all the clusters with colors and labels
        for label, cluster in self.clusters.items():
            plot(cluster, color=colors[label], marker='o')

        # Plot the background noise as grey
        plot(self.noise, color=(0.6, 0.6, 0.6), marker='x')

        figure.tight_layout()
        figure.savefig(os.path.join(directory, "clusters.png"), dpi=200)
Beispiel #16
0
def plotData(NQuery, input_table, FigureStrBase, variables, xMin, xMax,
             yMin, yMax, zMin, zMax, interactive=False, show_log=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
    xMin
    xMax
    yMin
    yMax
    zMin
    zMax
    """

    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[variables[0]]
    y_ax = d[variables[1]]
    z_ax = d[variables[2]]
    if d['IsSimulated'].dtype == 'bool':
        IsSim = d['IsSimulated']
    else:
        IsSim = d['IsSimulated'] == 'True'

    label_dict = \
        {'SurfaceDensity': '$\Sigma$ [M$_{\odot}$ pc$^{-2}$]',
         'VelocityDispersion': '$\sigma$ [km s$^{-1}$]',
         'Radius': '$R$ [pc]'}

    # selects surface density points wthin 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

    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()

    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=(np.log(np.array(z_ax[ObsPlot]))-np.log(zMin.value)+0.5)**3.,
                           color=color, alpha=0.5, edgecolors='k')

            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=(np.log(np.array(z_ax[SimPlot]))-np.log(zMin.value)+0.5)**3.,
                           color=color, alpha=0.5, edgecolors='k')

            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[variables[0]], fontsize=16)
    ax.set_ylabel(label_dict[variables[1]], fontsize=16)

    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,
                                                        alpha_sel=0.5))

    # adding fake points to show the size
    axes_limits = ax.axis()
    xax_limits = axes_limits[:2]
    yax_limits = axes_limits[2:]

    # TODO: write a function with this section
    # TODO: change position based on user input
    xfake = [0.1, 0.1, 0.1]
    yfake = [0.85, 0.9, 0.95]
    radius = np.array([1e-1, 1e0, 1e1])  # *u.pc #(zMin + zMax)*0.5

    # 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=(np.log(np.array(radius))-np.log(zMin.value)+0.5)**3.,
               transform=ax.transAxes,
               facecolors='g')
    for xf, yf, rad in zip(xfake, yfake, radius):
        ax.text(xf + 0.05, yf-0.01, str(rad) + ' ' + str(zMin.unit),
                transform=ax.transAxes)

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

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

    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+'.html'
Beispiel #17
0
def plotData(NQuery, 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()
    canvas = FigureCanvasAgg(figure)
    ax = figure.gca()

    # d = table.Table.read("merged_table.ipac", format='ascii.ipac')
    d = 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 = set(Author[Use])
    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)
    
    ax.loglog()
    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):
            ax.scatter(SurfDens[ObsPlot], VDisp[ObsPlot], marker=markers[0],
                        s=(np.log(np.array(Rad[ObsPlot]))-np.log(np.array(RadMin))+0.5)**3.,
                        color=color, alpha=0.5)
        if any(SimPlot):
            ax.scatter(SurfDens[SimPlot], VDisp[SimPlot], marker=markers[1],
                        s=(np.log(np.array(Rad[SimPlot]))-np.log(np.array(RadMin))+0.5)**3.,
                        color=color, alpha=0.5)
    if any(Obs):
        ax.scatter(SurfDens[Obs], VDisp[Obs], marker=markers[0],
                    s=(np.log(np.array(Rad[Obs]))-np.log(np.array(RadMin))+0.5)**3.,
                    facecolors='none', edgecolors='black',
                    alpha=0.5)
    if any(Sim):
        ax.scatter(SurfDens[Sim], VDisp[Sim], marker=markers[1],
                    s=(np.log(np.array(Rad[Sim]))-np.log(np.array(RadMin))+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])

    #html_bokeh = bokeh.mpl.to_bokeh(fig=figure, name="bokeh_"+FigureStrBase+NQuery)
    #html = mpld3.fig_to_html(figure)
    #with open("mpld3_"+FigureStrBase+NQuery+'.html','w') as f:
    #    f.write(html)

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

    # Put a legend to the right of the current axis
    ax.legend(UniqueAuthor, loc='center left', bbox_to_anchor=(1.0, 0.5), prop={'size':12}, markerscale = .7, scatterpoints = 1)

    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()

    return FigureStrBase+NQuery+'.png'
def drawSVMParaFirgure(xList, yList):
	'''
	#here's our data to plot, all normal Python lists
	x = [1, 2, 3, 4, 5]
	y = [0.1, 0.2, 0.3, 0.4, 0.5]

	intensity = [5, 10, 15, 20, 25,30, 35, 40, 45, 50,55, 60, 65, 70, 75,80, 85, 90, 95, 100,105, .01, 115, 120, 125]

	#setup the 2D grid with Numpy
	x, y = np.meshgrid(x, y)

	#convert intensity (list of lists) to a numpy array for plotting
	intensity = np.array(intensity).reshape((5, 5))
	print(intensity)

	#now just plug the data into pcolormesh, it's that easy!
	plt.pcolormesh(x, y, intensity)
	plt.colorbar() #need a colorbar to show the intensity scale
	plt.show() #boom	
	'''
	gamma = 1e-9*4.5
	gammaList = [ gamma*(10**powE) for powE in range(10)]
	
	C = 0.00001
	CList = [ C*(10**powE) for powE in range(10)]
	
	print(gammaList)
	print(CList)
	
	resultList = []
	scoreList = []
	decision_function = 'ovr'
	for C in CList:
		for gamma in gammaList:
			clf_rbf = svm.SVC(decision_function_shape=decision_function, 	C=C, kernel='rbf',  			gamma=gamma)

			#cross validation 
			#print("rbf cross validation")
			score_rbf = cross_validation.cross_val_score(clf_rbf, xList, yList, cv=3)
			mean = score_rbf.mean()	
			resultList.append({'C':C, 'gamme':gamma, 'score':mean})
			scoreList.append(mean)
	scoreList = np.array(scoreList)
	
	maxScore = 0
	maxResult = []
	for result in resultList:
		score = result['score']
		if maxScore < score:
			maxScore = score
			maxResult = result
	print (maxScore, maxResult)
	
	x = gammaList
	y = CList
	
	x = range(len(gammaList)+1)
	y = range(len(CList)+1)
	intensity = scoreList.reshape((len(gammaList), len(CList)))
	
	#print(x)
	#print(y)
	#print(intensity)
	
	#f1 = scoreList.reshape((6, 6))
	#setup the 2D grid with Numpy
	x, y = np.meshgrid(x, y)

	plt.pcolormesh(x, y, intensity)
	plt.colorbar() #need a colorbar to show the intensity scale
	#plt.show() #boom	
	
	fig = matplotlib.pyplot.gcf()
	fig.set_size_inches(10.5, 8.5)
	fig.savefig("SVM_C_gamma.png", dpi=100)
Beispiel #19
0
def _get_figure_as_image(figure):
    with io.BytesIO() as image_buffer:
        figure.savefig(image_buffer, format='png', bbox_inches="tight")
        return image_buffer.getvalue()
def drawSVMParaFirgure(xList, yList):
    '''
	#here's our data to plot, all normal Python lists
	x = [1, 2, 3, 4, 5]
	y = [0.1, 0.2, 0.3, 0.4, 0.5]

	intensity = [5, 10, 15, 20, 25,30, 35, 40, 45, 50,55, 60, 65, 70, 75,80, 85, 90, 95, 100,105, .01, 115, 120, 125]

	#setup the 2D grid with Numpy
	x, y = np.meshgrid(x, y)

	#convert intensity (list of lists) to a numpy array for plotting
	intensity = np.array(intensity).reshape((5, 5))
	print(intensity)

	#now just plug the data into pcolormesh, it's that easy!
	plt.pcolormesh(x, y, intensity)
	plt.colorbar() #need a colorbar to show the intensity scale
	plt.show() #boom	
	'''
    gamma = 1e-9 * 4.5
    gammaList = [gamma * (10**powE) for powE in range(10)]

    C = 0.00001
    CList = [C * (10**powE) for powE in range(10)]

    print(gammaList)
    print(CList)

    resultList = []
    scoreList = []
    decision_function = 'ovr'
    for C in CList:
        for gamma in gammaList:
            clf_rbf = svm.SVC(decision_function_shape=decision_function,
                              C=C,
                              kernel='rbf',
                              gamma=gamma)

            #cross validation
            #print("rbf cross validation")
            score_rbf = cross_validation.cross_val_score(clf_rbf,
                                                         xList,
                                                         yList,
                                                         cv=3)
            mean = score_rbf.mean()
            resultList.append({'C': C, 'gamme': gamma, 'score': mean})
            scoreList.append(mean)
    scoreList = np.array(scoreList)

    maxScore = 0
    maxResult = []
    for result in resultList:
        score = result['score']
        if maxScore < score:
            maxScore = score
            maxResult = result
    print(maxScore, maxResult)

    x = gammaList
    y = CList

    x = range(len(gammaList) + 1)
    y = range(len(CList) + 1)
    intensity = scoreList.reshape((len(gammaList), len(CList)))

    #print(x)
    #print(y)
    #print(intensity)

    #f1 = scoreList.reshape((6, 6))
    #setup the 2D grid with Numpy
    x, y = np.meshgrid(x, y)

    plt.pcolormesh(x, y, intensity)
    plt.colorbar()  #need a colorbar to show the intensity scale
    #plt.show() #boom

    fig = matplotlib.pyplot.gcf()
    fig.set_size_inches(10.5, 8.5)
    fig.savefig("SVM_C_gamma.png", dpi=100)
Beispiel #21
0
        xlabel = scaleLabel(xlabel, plot.args.xscale)
    if plot.args.yscale is not None:
        scaleAxis(axes1.yaxis, plot.args.yscale)
        ylabel = scaleLabel(ylabel, plot.args.yscale)
    if plot.sig:
        axes1.tick_params(axis = "x", bottom = False, labelbottom = False)
        axes2.set_xlabel(xlabel)
        axes2.set_xlim(plot.args.xmin, plot.args.xmax)
        if plot.args.xstep is not None:
            axes2.xaxis.set_major_locator(matplotlib.ticker.MultipleLocator(plot.args.xstep))
        if plot.args.xtick is not None:
            axes2.set_xticks(numpy.append(axes2.get_xticks(), plot.args.xtick))
        if plot.args.xscale is not None:
            scaleAxis(axes2.xaxis, plot.args.xscale)
        axes2.set_ylabel("Significance")
        axes2.set_ylim(0.75, 1.05)
        ticks = axes2.set_yticks((0.8, 0.95, 1.0))
        if plot.args.size < 4.0:
            nudge(ticks[1].label, 0.0, -1.0)
            nudge(ticks[2].label, 0.0, 1.0)
    else:
        axes1.set_xlabel(xlabel)
    if plot.args.regress or plot.args.passive:
        axes1.legend(loc = plot.args.legend)
    axes1.set_ylabel(ylabel)
    if plot.args.diag:
        xlim = axes1.get_xlim()
        axes1.plot(xlim, xlim, **getGridKwargs())
    figure.set_tight_layout(PAD)
    figure.savefig("{0}-vs-{1}".format(plot.yMetric.getKey(), plot.xMetric.getKey()))
Beispiel #22
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
Beispiel #23
0
# table_data = pd.read_csv (file_name+'.csv')
#     table_data2 = table_data[['Posted Date','Card No.','Description','Category','Debit','Credit']]
#     table_data1 = table_data[['Card No.','Category','Debit']]
#     groupby_sum1 = table_data1.groupby(['Card No.','Category']).sum()

con = sqlite3.connect("capitalone.db")
cur = con.cursor()
cur.execute("SELECT DISTINCT Category FROM {0};".format("May2019"))
categories = cur.fetchall()
categories2 = []
for category in categories:
    categories2.append(category[0])
totals = []
category3 = []
for category in categories2:
    cur.execute("SELECT SUM(Debit) FROM {0} WHERE Category = '{1}';".format(
        "May2019", category))
    totals.append(cur.fetchall()[0][0])
    category3.append(category)

figure = plt.figure(figsize=(12, 10))
plt.plot(category3, totals)
figure.savefig('static/images/myfig.png')


@app.route("/")
def main_page():
    return render_template('test.html')


app.run()