Exemplo n.º 1
0
    def doRender(self, handlerId):
        if self.canRenderChart(handlerId) == False:
            self._addHTML("Unable to find a numerical column in the dataframe")
            return

        mpld3.enable_notebook()
        fig, ax = plt.subplots()
        keyFields = self.getKeyFields(handlerId)
        keyFieldValues = self.getKeyFieldValues(handlerId, keyFields)
        keyFieldLabels = self.getKeyFieldLabels(handlerId, keyFields)
        valueFields = self.getValueFields(handlerId)
        valueFieldValues = self.getValueFieldValueLists(handlerId, keyFields, valueFields)
        context = self.getMpld3Context(handlerId)
        options = {"fieldNames":self.getFieldNames(),"aggregationSupported":self.supportsAggregation(handlerId),"aggregationOptions":["SUM","AVG","MIN","MAX","COUNT"]}
        if (context is not None):
            options.update(context[1])
            dialogBody = self.renderTemplate(context[0], **options)
        else:
            dialogBody = self.renderTemplate("baseChartOptionsDialogBody.html", **options)
        plugins.connect(fig, ChartPlugin(self, keyFieldLabels))
        plugins.connect(fig, DialogPlugin(self, handlerId, dialogBody))
        self.doRenderMpld3(handlerId, fig, ax, keyFields, keyFieldValues, keyFieldLabels, valueFields, valueFieldValues)
        self.setChartSize(handlerId, fig, ax)
        self.setChartGrid(handlerId, fig, ax)
        self.setChartLegend(handlerId, fig, ax)
Exemplo n.º 2
0
def mpld3_enable_notebook():
    """Change the default plugins, enable ipython notebook mode and return mpld3 module."""
    import mpld3
    from mpld3 import plugins as plugs
    plugs.DEFAULT_PLUGINS = [plugs.Reset(), plugs.Zoom(), plugs.BoxZoom(), plugs.MousePosition()]
    mpld3.enable_notebook()
    return mpld3
Exemplo n.º 3
0
def mpld3_enable_notebook():
    """Change the default plugins, enable ipython notebook mode and return mpld3 module."""
    import mpld3
    from mpld3 import plugins as plugs
    plugs.DEFAULT_PLUGINS = [plugs.Reset(), plugs.Zoom(), plugs.BoxZoom(), plugs.MousePosition()]
    mpld3.enable_notebook()
    return mpld3
Exemplo n.º 4
0
    def bar_plot(self, positive, negative, neutral, word_search,
                 number_of_terms):
        objects = ('positive', 'negative', 'neutral')
        performance = (positive, negative, neutral)
        y_pos = np.arange(len(objects))
        plt.bar(y_pos, performance, align='center', alpha=0.5)
        plt.xticks(y_pos, objects)
        plt.ylabel('Percentage')
        plt.xlabel('Sentiments')
        plt.show()

        names = 'POSITIVE', 'NEGATIVE', 'NEUTRAL'
        size = [positive, negative, neutral]

        # create a figure and set different background
        fig = plt.figure()
        fig.patch.set_facecolor('black')

        # Change color of text
        plt.rcParams['text.color'] = 'white'

        # Create a circle for the center of the plot
        my_circle = plt.Circle((0, 0), 0.7, color='black')

        # Pieplot + circle on it
        plt.pie(size, labels=names)
        p = plt.gcf()
        p.gca().add_artist(my_circle)
        plt.show()

        plt.plot([positive, negative, neutral], 'ks-', mec='y', mew=2, ms=15)
        plt.ylabel('Percentage')
        plt.xlabel('Sentiments')
        mpld3.enable_notebook()
        mpld3.show()
Exemplo n.º 5
0
    def renderFigure(self, fig):
        def genMarkup(chartFigure):
            return self.env.from_string("""
                    {0}
                    {{%for message in messages%}}
                        <div>{{{{message}}}}</div>
                    {{%endfor%}}
                """.format(chartFigure)).render(messages=self.messages)

        if not self.useMpld3:
            import base64
            try:
                from io import BytesIO as pngIO
            except ImportError:
                from StringIO import StringIO as pngIO
            png = pngIO()
            plt.savefig(png,
                        pad_inches=0.05,
                        bbox_inches='tight',
                        dpi=self.getDPI())
            try:
                return (genMarkup("""
                            <center><img style="max-width:initial !important" src="data:image/png;base64,{0}"  class="pd_save"></center>
                        """.format(
                    base64.b64encode(png.getvalue()).decode("ascii"))))
            finally:
                png.close()
        else:
            mpld3.enable_notebook()
            try:
                return genMarkup(mpld3.fig_to_html(fig))
            finally:
                mpld3.disable_notebook()
 def renderFigure(self, fig):
     def genMarkup(chartFigure):
         return self.env.from_string("""
                 {0}
                 {{%for message in messages%}}
                     <div>{{{{message}}}}</div>
                 {{%endfor%}}
             """.format(chartFigure)
         ).render(messages=self.messages)
         
     if not self.useMpld3:
         import base64
         try:
             from io import BytesIO as pngIO
         except ImportError:
             from StringIO import StringIO as pngIO
         png=pngIO()
         plt.savefig(png, pad_inches=0.05, bbox_inches='tight', dpi=self.getDPI())
         try:
             return( 
                 genMarkup("""
                         <center><img style="max-width:initial !important" src="data:image/png;base64,{0}"  class="pd_save"></center>
                     """.format(base64.b64encode(png.getvalue()).decode("ascii"))
                 )
             )
         finally:
             png.close()
     else:
         mpld3.enable_notebook()
         try:
             return genMarkup(mpld3.fig_to_html(fig))
         finally:
             mpld3.disable_notebook()
Exemplo n.º 7
0
 def plotPieChart(self, positive, wpositive, spositive, negative, wnegative, snegative, neutral, searchTerm, noOfSearchTerms): 
     labels = ['Positive [' + str(positive) + '%]', 'Weakly Positive [' + str(wpositive) + '%]','Strongly Positive [' + str(spositive) + '%]', 'Neutral [' + str(neutral) + '%]', 
                'Negative [' + str(negative) + '%]', 'Weakly Negative [' + str(wnegative) + '%]', 'Strongly Negative [' + str(snegative) + '%]'] 
     sizes = [positive, wpositive, spositive, neutral, negative, wnegative, snegative] 
     colors = ['yellowgreen','lightgreen','darkgreen', 'gold', 'red','lightsalmon','darkred'] 
     patches, texts = plt.pie(sizes, colors=colors, startangle=90) 
     plt.legend(patches, labels, loc="best") 
     plt.title('How people are reacting on ' + searchTerm + ' by analyzing ' + str(noOfSearchTerms) + ' Tweets.') 
     plt.axis('equal') 
     plt.tight_layout() 
     plt.show() 
     mpld3.enable_notebook() 
     mpld3.show() 
Exemplo n.º 8
0
 def renderFigure(self, fig, dialogBody):
     if self.options.get("staticFigure","false") is "true":
         import StringIO
         png=StringIO.StringIO()
         plt.savefig(png)
         self._addHTMLTemplate("mpld3Chart.html", 
             mpld3Figure="""<img src="data:image/png;base64,{0}">""".format(png.getvalue().encode('base64')), 
             optionsDialogBody=dialogBody)
         plt.close(fig)
     else:
         mpld3.enable_notebook()
         self._addHTMLTemplate("mpld3Chart.html", mpld3Figure=mpld3.fig_to_html(fig), optionsDialogBody=dialogBody)
         plt.close(fig)
         mpld3.disable_notebook()
Exemplo n.º 9
0
    def plot_cumulative_returns_by_intervals(self, lower_bound_list,
                                             upper_bound_list, freq):
        factor = self.factor

        #想要查看的因子区间策略的数量
        num_strategies = len(upper_bound_list)
        #基准策略
        baseline = self.baseline_generator()

        color = [
            'green', 'skyblue', 'purple', 'black', 'pink', 'yellow', 'orange',
            'grey', 'brown', 'blue', 'coral'
        ]
        y0 = baseline['累计收益率'].values
        time = baseline['交易日期'].values
        time_start = time[0]
        time_end = time[-1]

        import matplotlib as mpl
        import matplotlib.pyplot as plt
        import mpld3
        mpld3.enable_notebook()
        plt.rcParams['figure.figsize'] = [9, 6]
        from pylab import mpl
        mpl.rcParams['font.sans-serif'] = ['SimHei']

        plt.figure()
        plt.title('%s - %s 累计收益率(不同%s)' % (time_start, time_end, factor))
        plt.plot(time, y0, color='red', label='baseline')

        for i in range(num_strategies):
            upper_bound = upper_bound_list[i]
            lower_bound = lower_bound_list[i]
            strategy = self.strategy_factor_interval(lower_bound, upper_bound,
                                                     freq)
            y = strategy['累计收益率'].values
            print('第%s个策略的因子区间为:%s' % (i + 1, strategy['票池因子区间'].values))
            print('第%s个策略的参与的股票数目为:%s' % (i + 1, strategy['参与股票数目'].values))
            plt.plot(time,
                     y,
                     color=color[i],
                     label='因子为%s、因子区间为[%s,%s)的策略' %
                     (factor, lower_bound, upper_bound))

        plt.ylim((0, 6))
        plt.legend()  # 显示图例
        plt.xlabel('time')
        plt.ylabel('累计收益率')

        plt.show()
Exemplo n.º 10
0
def _init_ipython(backend):
    """Tries `backend`.  Returns ``inline`` backend if default requested or
    unable to load requested backend.
    """
    if backend == uti_plot.DEFAULT_BACKEND:
        backend = 'inline'
    is_mpld3 = backend == 'mpld3'
    b = 'inline' if is_mpld3 else backend
    get_ipython().magic('matplotlib ' + b)
    if is_mpld3:
        try:
            import mpld3
            mpld3.enable_notebook()
        except:
            print('mpld3: backend unavailable; plots will be inline')
            backend = 'inline'
    return backend
Exemplo n.º 11
0
 def _init_ipython(self, backend):
     """Tries `backend`.  Returns ``inline`` backend if default requested or
     unable to load requested backend.
     """
     if backend == uti_plot.DEFAULT_BACKEND:
         backend = 'inline'
     is_mpld3 = backend == 'mpld3'
     b = 'inline' if is_mpld3 else backend
     get_ipython().magic('matplotlib ' + b)
     if is_mpld3:
         try:
             import mpld3
             mpld3.enable_notebook()
         except:
             print('mpld3: backend unavailable; plots will be inline')
             backend = 'inline'
     return backend
    def doRender(self, handlerId):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            import mpld3
            mpld3.enable_notebook()

            fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'))
            ax.grid(color='white', linestyle='solid')

            N = 50
            scatter = ax.scatter(np.random.normal(size=N),
                                 np.random.normal(size=N),
                                 c=np.random.random(size=N),
                                 s=1000 * np.random.random(size=N),
                                 alpha=0.3,
                                 cmap=plt.cm.jet)

            ax.set_title("D3 Scatter Plot", size=18)
def prepare_image_data(image_data):
	im = np.array(image_data, dtype=np.uint8)

	# Create figure and axes
	fig,ax = plt.subplots(1)
	
  # Adjust the 
	width = 15
	height = width * image_data.size[1] / image_data.size[0]
	fig.set_size_inches(width, int(height), forward=True)
	
	plugins.connect(fig, plugins.MousePosition(fontsize=14))
	mpld3.enable_notebook()
  
	# Display the image
	ax.imshow(im)

	return ax
Exemplo n.º 14
0
	def mplPlotLine(self):

		mpld3.enable_notebook()
		plt.errorbar(self.data[0],self.data[1],self.data[2],
		aa=self.antialiasing,
		marker=self.marker,
		ls=self.linestyle,
		c=self.color,
		lw=self.linewidth,
		mfc=self.markerfacecolor,
		mec=self.markeredgecolor,
		ms=self.markersize,
		mew=self.markeredgesize)

		plt.xlim(self.xlim[0],self.xlim[1])
		plt.xlabel(self.xlabel)
		plt.ylabel(self.ylabel)
		plt.grid(self.grid)
Exemplo n.º 15
0
    def doRender(self, handlerId):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            import mpld3
            mpld3.enable_notebook()        
        
            fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'))
            ax.grid(color='white', linestyle='solid')

            N = 50
            scatter = ax.scatter(np.random.normal(size=N),
                        np.random.normal(size=N),
                        c=np.random.random(size=N),
                        s = 1000 * np.random.random(size=N),
                        alpha=0.3,
                        cmap=plt.cm.jet)

            ax.set_title("D3 Scatter Plot", size=18);
Exemplo n.º 16
0
    def doRender(self, handlerId):
        displayColName = self.getNumericalColsInfo()
        if len(displayColName) < 2:
            self._addHTML("Unable to find two numerical columns in the dataframe")
            return
        
        mpld3.enable_notebook()        
        x = self.entity.select(displayColName[0]).toPandas()[displayColName[0]].dropna().tolist()
        y = self.entity.select(displayColName[1]).toPandas()[displayColName[1]].dropna().tolist()

        plt.rcParams['font.size']=11
        plt.rcParams['figure.figsize']=[6.0, 5.0]
        fig, ax=plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'))          
        ax.grid(color='white', linestyle='solid')
        scatter = ax.scatter(x,y,c=y,marker='o',alpha=0.7,s=124,cmap=plt.cm.ocean)
        ax.set_title("D3 Scatter Plot", size=18);
        ax.set_xlabel(displayColName[0], size=14)
        ax.set_ylabel(displayColName[1], size=14)
Exemplo n.º 17
0
    def plot_cumulative_returns_by_quantiles(self, freq, fraction):
        factor = self.factor

        all_strategies = self.strategy_comparison(freq, fraction)
        baseline = self.baseline_generator()

        color = [
            'green', 'skyblue', 'purple', 'black', 'pink', 'yellow', 'orange',
            'grey', 'brown', 'blue'
        ]
        y0 = baseline['累计收益率'].values
        time = baseline['交易日期'].values
        time_start = time[0]
        time_end = time[-1]

        import matplotlib as mpl
        import matplotlib.pyplot as plt
        import mpld3
        mpld3.enable_notebook()
        plt.rcParams['figure.figsize'] = [9, 6]
        from pylab import mpl
        mpl.rcParams['font.sans-serif'] = ['SimHei']

        plt.figure()
        plt.title('%s - %s 累计收益率(不同%s)' % (time_start, time_end, factor))
        plt.plot(time, y0, color='red', label='baseline')

        for i in range(1, fraction + 1):
            y = all_strategies['累计收益率%s' % (i)].values
            print('第%s部分的因子区间为:%s' % (i, all_strategies['票池因子区间%s' %
                                                        (i)].values))
            print('第%s部分的参与的股票数目为:%s' % (i, all_strategies['参与股票数目%s' %
                                                           (i)].values))
            plt.plot(time,
                     y,
                     color=color[i - 1],
                     label='strategy with %s smallest %s' % (i, factor))
        plt.ylim((0, 6))
        plt.legend()  # 显示图例
        plt.xlabel('time')
        plt.ylabel('累计收益率')

        plt.show()
    def mplPlotLine(self):

        mpld3.enable_notebook()
        plt.errorbar(self.data[0],
                     self.data[1],
                     self.data[2],
                     aa=self.antialiasing,
                     marker=self.marker,
                     ls=self.linestyle,
                     c=self.color,
                     lw=self.linewidth,
                     mfc=self.markerfacecolor,
                     mec=self.markeredgecolor,
                     ms=self.markersize,
                     mew=self.markeredgesize)

        plt.xlim(self.xlim[0], self.xlim[1])
        plt.xlabel(self.xlabel)
        plt.ylabel(self.ylabel)
        plt.grid(self.grid)
Exemplo n.º 19
0
def plot_decomposed_embeddings(ce_object):
    """
    @param ce_object ContextEmbeddings object

    Plots the embedding representation (decomposed to 2 dimensions)
    """
    if ce_object.decomp_dims == 2:
        fig, ax = plt.subplots(figsize=(10, 8))

        points = ax.plot(
            ce_object.decomposed_embedding_representation[:, 0],
            ce_object.decomposed_embedding_representation[:, 1],
            "o",
            color="b",
            mec="k",
            ms=5,
            mew=1,
            alpha=0.6,
        )

        ax.set_title(
            f"{ce_object.decomp_dims}D {ce_object.decomp_method} for contexts of '{ce_object.word}'",
            size=16,
        )

        labels = [
            ce_object.contexts[i] for i in range(len(ce_object.contexts))
        ]
        tooltip = mpld3.plugins.PointHTMLTooltip(points[0],
                                                 labels,
                                                 voffset=10,
                                                 hoffset=10)
        mpld3.plugins.connect(fig, tooltip)

        mpld3.enable_notebook()

    else:
        print("Can't plot with dimension > 2.")
import openpyxl as opx
import json
import matplotlib.pyplot as plt
import operator
import numpy as np

from openpyxl import load_workbook
from pandas import DataFrame, read_csv
from IPython.display import HTML
import folium

try:
    import mpld3
    from mpld3 import enable_notebook
    from mpld3 import plugins
    enable_notebook()
except Exception as e:
    print "Attempt to import and enable mpld3 failed", e

class FarmersMarket:
	URL ='http://www.ams.usda.gov/AMSv1.0/getfile?dDocName=STELPRDC5087258'
	DEBUG = False
	
	def __init__(self):
		#Parsing local CSV.	
		location = r'/Desktop/WorkingWithOpenData/FarmersMarkets.csv'
		df = pd.read_csv(location)
		df = pd.DataFrame.from_csv(location)
		df.columns
	def sort_top_states(df):
		#df -> DataFrame
Exemplo n.º 21
0
  def plot_defaultdicts(self, default_dicts, show_max=True, bucket='huanan', figsize_wh=(15, 8), legend_size=12):
    import matplotlib.pyplot as plt
    % matplotlib inline
    import numpy as np
    import mpld3
    mpld3.enable_notebook()
    import os
    import moxing as mox
    import tempfile
    if not isinstance(default_dicts, list):
      default_dicts = [default_dicts]
    if not isinstance(show_max, list):
      show_max = [show_max]
    assert len(show_max) == len(default_dicts)

    fig, axes = self.get_fig_axes(rows=len(default_dicts), cols=1, figsize_wh=figsize_wh)

    bucket = self.root_obs_dict[bucket]
    root_dir = os.path.expanduser('~/results')

    label2datas_list = []
    for idx, default_dict in enumerate(default_dicts):
      data_xlim = None
      axes_prop = default_dict.get('properties')
      if axes_prop is not None:
        if 'xlim' in axes_prop:
          data_xlim = axes_prop['xlim'][-1]

      label2datas = {}
      # for each result dir
      for (result_dir, label2file) in default_dict.items():
        if result_dir == 'properties':
          continue
        # for each texlog file
        for label, file in label2file.items():
          filepath = os.path.join(root_dir, result_dir, file)
          filepath_obs = os.path.join(bucket, result_dir, file)
          if not mox.file.exists(filepath_obs):
            print("=> Not exist: '%s'" % filepath_obs)
            continue
          mox.file.copy(filepath_obs, filepath)
          # get modified time
          modi_minutes = self.get_last_md_inter_time(filepath_obs)

          data = np.loadtxt(filepath, delimiter=':')
          data = data.reshape(-1, 2)
          # limit x in a range
          if data_xlim:
              data = data[data[:, 0] <= data_xlim]

          itr_val_str = self.get_itr_val_str(data, show_max[idx])
          label_str = f'{itr_val_str}' + f'-{modi_minutes:03d}m---' + label

          axes[idx].plot(data[:, 0], data[:, 1], label=label_str, marker='.', linewidth='5', markersize='15', alpha=0.5)
          label2datas[label] = data
      axes[idx].legend(prop={'size': legend_size})
      axes[idx].set(**default_dict['properties'])
      axes[idx].grid(b=True, which='major', color='#666666', linestyle='--', alpha=0.2)

      label2datas_list.append(label2datas)

    return label2datas_list
#     0 32.0
#     5 41.0
#     10 50.0
#     15 59.0
#     20 68.0
#     25 77.0
#     30 86.0
#     35 95.0
#     40 104.0
#

# In[4]:

from mpld3 import enable_notebook
enable_notebook()

rad = np.arange(-100, 100)
plot(sin(rad / 10.))
show()

# Out[4]:

# image file:

# ## Text manipulation

# ### Edit replace

# In[35]:
Exemplo n.º 23
0
def points(
        im='http://msutoday.msu.edu/_/img/assets/2013/beaumont-spring-1.jpg',
        point_color='black',
        point_radius=2):
    # The user can input a url of an image (there is a default image), a point color (default is black), and point radius (default is 2).

    # Here are some libraries you will need to use.
    import scipy.misc as misc
    from urllib.request import urlopen
    from scipy.misc import imread, imsave
    import matplotlib.pyplot as plt
    import matplotlib.image as img
    import sys
    sys.path.append('./packages')
    import mpld3
    from mpld3 import plugins
    mpld3.enable_notebook()

    # This checks is the input point radius is a valid number. If not, it sets the point radius to 2.
    try:
        point_radius = float(point_radius)
    except ValueError:
        point_radius = 2

    # Plots the image in the notebook
    def plot(imgname):
        fig, ax = plt.subplots()
        im = img.imread(imgname)
        plt.imshow(im, origin='lower')
        return fig

    # Function called in the notebook
    def pickpoints(fig='', radius=4, color="white", x='x', y='y'):
        if not fig:
            fig = plt.gcf()
        plugins.connect(fig, Annotate(radius, color, x,
                                      y))  # color='htmlcolorname', radius=int
        plugins.connect(fig, plugins.MousePosition())

    # Main class that contains Javascript code to create and drag circles
    class Annotate(plugins.PluginBase):
        """A plugin that creates points in a figure by clicking the mouse"""

        JAVASCRIPT = r"""
        mpld3.register_plugin("annotate", Annotate);
        Annotate.prototype = Object.create(mpld3.Plugin.prototype);
        Annotate.prototype.constructor = Annotate;
        Annotate.prototype.requiredProps = [];
        Annotate.prototype.defaultProps = {radius: 4, color: "white", x: 'x', y: 'y'};
        function Annotate(fig, props){
            mpld3.Plugin.call(this, fig, props);
        };

        Annotate.prototype.draw = function(){

            /// NECESSARY STARTUP VARIABLES ///

            var fig = this.fig;
            var ax = fig.axes;
            var dataset = [];
            var svg = d3.select(".mpld3-figure");   // existing svg element
            var radius = this.props.radius;
            var color = this.props.color;
            var x = this.props.x;
            var y = this.props.y;
            var ax = fig.axes[0];


            /// INDEXES HTML DOC TO PULL VALUES FOR x,y CALIBRATION ///
            var xcal = this.parent.axes[0].position[0];
            var ycal = this.parent.axes[0].position[1];
            console.log('x calibration: ' + xcal);
            console.log('y calibration: ' + ycal);

            var xcommand = x+" = []";
            IPython.notebook.kernel.execute(xcommand);
            var ycommand = y+" = []";
            IPython.notebook.kernel.execute(ycommand);


            ////////// CREATE POINT COMPONENT //////////

            var update_coords = function() {

                return function() {
                    var pos = d3.mouse(this),
                        xpos = ax.x.invert(pos[0]),
                        ypos = ax.y.invert(pos[1]);

                    var newpoint = {
                        cx: pos[0] + xcal,
                        cy: pos[1] + ycal,
                        r: radius,
                        fill: color
                    };
                    dataset.push(newpoint);

                    var circles = svg.selectAll("circle")
                        .data(dataset)
                        .enter()
                        .append("circle")
                        .attr(newpoint)
                        .call(drag);

                    var xcommand = x+".append("+xpos+")";
                    IPython.notebook.kernel.execute(xcommand);
                    console.log(xcommand);
                    var ycommand = y+".append("+ypos+")";
                    IPython.notebook.kernel.execute(ycommand);
                    console.log(ycommand);

                };
            }();
            ax.baseaxes
                .on("mousedown", update_coords);



            ////////// DRAG POINT COMPONENT //////////

            var drag = d3.behavior.drag()
                .on("dragstart", dragstarted)
                .on("drag", dragged)
                .on("dragend", dragended);

            function dragstarted(d) {
                 d3.event.sourceEvent.stopPropagation();
                 d3.select(this).classed("dragging", true);
            }

            function dragged(d) {
                 d3.select(this).attr("cx", d3.event.x)
                                .attr("cy", d3.event.y);             
            }

            function dragended(d, i) {
                 d3.event.sourceEvent.stopPropagation();
                 d3.select(this).classed("dragging", false);
                 var calib_cx = d3.select(this)[0][0].cx.animVal.value - xcal;
                 var calib_cy = d3.select(this)[0][0].cy.animVal.value - ycal;
                 var xcommand = x+"["+i+"] = "+ax.x.invert(calib_cx);
                 var ycommand = y+"["+i+"] = "+ax.y.invert(calib_cy);
                 IPython.notebook.kernel.execute(xcommand);
                 IPython.notebook.kernel.execute(ycommand);
                 console.log(xcommand);
                 console.log(ycommand);
            }


        };"""

        def __init__(self, radius=4, color="white", x='x', y='y'):
            self.dict_ = {
                "type": "annotate",
                "radius": radius,
                "color": color,
                "x": x,
                "y": y
            }

    # Opens and displays the image
    if type(im) == str:
        with urlopen(im) as file:
            im2 = imread(file, mode='RGB')
        im = im2
    fig = plt.figure(figsize=(9, 6))
    plt.imshow(im)

    # Runs the pickpoints function to allow the user to pick points on the image
    pickpoints(color=point_color,
               radius=point_radius,
               x='xcoords',
               y='ycoords')
Exemplo n.º 24
0
def make_interactive_graph(graph,
                           pos=None,
                           cmap=plt.cm.viridis,
                           edge_cmap=plt.cm.Reds,
                           node_size_factor=5):
    fig = plt.figure(1, figsize=(8, 6), dpi=100)
    ax = plt.Axes(fig, [0., 0., 1., 1.])
    ax.set_axis_off()
    fig.add_axes(ax)

    weights = [d["weight"] for (u, v, d) in graph.edges(data=True)]
    degrees = nx.degree(graph)
    sizes = [degrees[name] * node_size_factor for name in graph.nodes()]
    urls = [d["url"] for u, d in graph.nodes(data=True)]

    if pos is None:
        pos = nx.spring_layout(graph)

    nodes = nx.draw_networkx_nodes(graph,
                                   pos=pos,
                                   node_size=sizes,
                                   node_color=sizes,
                                   cmap=cmap,
                                   alpha=0.3)
    edges = nx.draw_networkx_edges(graph,
                                   pos=pos,
                                   edge_color=weights,
                                   edge_cmap=edge_cmap,
                                   arrows=False,
                                   alpha=0.3)

    tip_div = u"<div style='background: #FFF; border-style: solid; border-width: 1px; padding: 2px;'>{}</div>"

    html_labels = list()
    for node in graph.nodes():
        neighbors = graph[node]
        neighbor_list = sorted([(neighbor, graph[node][neighbor]["weight"])
                                for neighbor in graph[node]],
                               key=lambda tup: tup[1],
                               reverse=True)[:3]
        list_label = u"<br />".join([
            u"{} ({})".format(neighbor[0], neighbor[1])
            for neighbor in neighbor_list
        ])
        html_labels.append(
            tip_div.format(u"<strong>{}</strong><br/><i>{}</i>".format(
                node, list_label)))

    line_labels = [
        tip_div.format(
            u"<strong>{}</strong> and <strong>{}</strong> <i>({})</i>".format(
                u, v, d["weight"])) for (u, v, d) in graph.edges(data=True)
    ]

    tooltips = mpld3.plugins.PointHTMLTooltip(nodes,
                                              labels=html_labels,
                                              voffset=-10,
                                              hoffset=10)
    linetips = mpld3.plugins.PointHTMLTooltip(edges,
                                              labels=line_labels,
                                              voffset=-5,
                                              hoffset=10)
    mpld3.plugins.connect(fig, ClickInfo(nodes, urls))
    mpld3.plugins.connect(fig, Highlight(nodes))
    mpld3.plugins.connect(fig, Highlight(edges))
    mpld3.plugins.connect(fig, tooltips)
    mpld3.plugins.connect(fig, linetips)
    mpld3.enable_notebook()
    return mpld3.fig_to_html(fig)
Exemplo n.º 25
0
def d3():
    import mpld3
    mpld3.enable_notebook()
    yield
    mpld3.disable_notebook()
Exemplo n.º 26
0
 def plot(self,
          plot_pred_price: bool = True,
          plot_buy_sell_point: bool = True,
          in_range: list = [0, None],
          save: bool = False):
     if len(in_range) != 2:
         raise Exception("in_range must be a list size of 2.")
     in_range[1] = len(
         self.real_hist_price) if in_range[1] == None else in_range[1]
     if in_range[0] == None or in_range[1] > len(
             self.real_hist_price
     ) or in_range[0] < 0 or in_range[0] > in_range[1]:
         raise Exception("invalid values in in_range parameter.")
     b_x, b_points, b_units = [], [], []
     s_x, s_points, s_units = [], [], []
     reduce_units_b, reduce_units_s = {}, {}
     for rec in self.trade_record:
         if rec[0] == 'b':
             if rec[1] >= in_range[0] and rec[1] < in_range[1]:
                 b_x.append(rec[1])
                 b_points.append(rec[2])
                 b_units.append(rec[3])
         elif rec[0] == 's':
             if rec[1] >= in_range[0] and rec[1] < in_range[1]:
                 s_x.append(rec[1])
                 s_points.append(rec[2])
                 s_units.append(rec[3])
     for idx, z in enumerate(
         [zip(b_x, b_points, b_units),
          zip(s_x, s_points, s_units)]):
         for val in z:
             if idx == 0:
                 if val[0] in list(reduce_units_b):
                     reduce_units_b[val[0]][1] += val[2]
                 else:
                     reduce_units_b[val[0]] = [val[1], val[2]]
             else:
                 if val[0] in list(reduce_units_s):
                     reduce_units_s[val[0]][1] += val[2]
                 else:
                     reduce_units_s[val[0]] = [val[1], val[2]]
     pluged = False
     mpld3.enable_notebook()
     real_days = np.arange(
         in_range[0],
         len(self.real_hist_price[in_range[0]:in_range[1]]) + in_range[0])
     pred_days = np.arange(
         in_range[0],
         len(self.pred_ema[in_range[0]:in_range[1]]) + in_range[0])
     fig, ax = plt.subplots()
     ax.set_title(self.model_type)
     ax.plot(real_days,
             self.real_hist_price[in_range[0]:in_range[1]],
             'black',
             label='Actual')
     if plot_pred_price:
         ax.plot(real_days,
                 (self.pred.reshape(-1, 1)[:, 0])[in_range[0]:in_range[1]],
                 c='orange',
                 ls='--',
                 label='Predict')
     ax.plot(pred_days,
             self.pred_ema[in_range[0]:in_range[1]],
             'blue',
             label='Predict-EMA')
     ax.set_xlabel('Days')
     ax.set_ylabel('Close Price')
     ax.legend()
     if plot_buy_sell_point:
         if len(reduce_units_b) != 0:
             b_scat = ax.scatter(
                 list(reduce_units_b),
                 np.array(list(reduce_units_b.values()))[:,
                                                         0].astype('float'),
                 c='g',
                 linewidths=1)
             b_tooltip = mpld3.plugins.PointLabelTooltip(
                 b_scat,
                 labels=np.array(list(
                     reduce_units_b.values()))[:, 1].astype('float'))
             mpld3.plugins.connect(fig, b_tooltip)
             pluged = True
         if len(reduce_units_s) != 0:
             s_scat = ax.scatter(
                 list(reduce_units_s),
                 np.array(list(reduce_units_s.values()))[:,
                                                         0].astype('float'),
                 c='r',
                 linewidths=1)
             s_tooltip = mpld3.plugins.PointLabelTooltip(
                 s_scat,
                 labels=np.array(list(
                     reduce_units_s.values()))[:, 1].astype('float'))
             mpld3.plugins.connect(fig, s_tooltip)
             pluged = True
         # if show_buy_sell_units:
         # pluged = True
         # for redu in [reduce_units_b, reduce_units_s]:
         # for idx in range(len(redu)):
         # if len(redu) != 0:
         # ax.annotate(np.array(list(redu.values()))[idx,1].astype('float'), (list(redu)[idx], np.array(list(redu.values()))[idx,0].astype('float')))
     if save:
         if pluged:
             mpld3.save_html(
                 fig,
                 f'plot_{self.model_type}_{datetime.datetime.now().strftime("%Y%m%d%H%M%S")}.html'
             )
         else:
             plt.savefig(
                 f'plot_{self.model_type}_{datetime.datetime.now().strftime("%Y%m%d%H%M%S")}.png'
             )
Exemplo n.º 27
0
# coding: utf-8

# In[33]:

# Only needed in a Jupyter Notebook
get_ipython().magic(u'matplotlib inline')

# In[34]:

#plots will get a toolbar menu at the bottom left
import mpld3
mpld3.enable_notebook(
)  #http://stackoverflow.com/questions/10655217/ipython-notebook-pylab-inline-zooming-of-a-plot

# In[35]:

__author__ = 'Ali'
# rev 5.1 (2015-10-08 13:43)
# update 2016-03-31

import numpy as np
import itertools as itr
import subprocess
import os
import pprint as pp
import sys
import fileinput
import matplotlib.pyplot as plt
from scipy.interpolate import UnivariateSpline
from scipy.optimize import curve_fit
""" unsolved issue & TODO tasks indicated by !!!!
Exemplo n.º 28
0
# In[]
print('start')
for x in range(0, 3):
    print(x)
print('end')

#%%
plt.plot([1, 2, 3, 2, 3, 2, 2, 1])
plt.show()

#%%
import matplotlib.pyplot as plt
import numpy as np
import mpld3

mpld3.enable_notebook()

fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'))
ax.grid(color='white', linestyle='solid')

N = 50
scatter = ax.scatter(np.random.normal(size=N),
                     np.random.normal(size=N),
                     c=np.random.random(size=N),
                     s=1000 * np.random.random(size=N),
                     alpha=0.3,
                     cmap=plt.cm.jet)

ax.set_title("D3 Scatter Plot", size=18)

#%%
Exemplo n.º 29
0
def plot_optimization_evolution_2d(evolution_data,
                                   *args,
                                   obj_func=None,
                                   **kwargs):
    """For a given population, plot their positions in solution space in 2d over time.

    Arguments
    ---------
        evolution_data: list over iterations where each entry is a dict of population members
                        holding their 2d positions as a list
        xlims (optional): list of lower and upper x lim for plot
        ylims (optional): list of lower and upper y lim for plot
        obj_func: name of the objective function to contour plot in the background
    """
    mpld3.enable_notebook()
    fig, ax = viz_utils.setup_figure_1ax(x_label='x position',
                                         y_label='y position',
                                         size=(8, 8),
                                         shrink_ax=False)

    if obj_func:
        if obj_func not in OBJ_FUNCS:
            raise NotImplementedError(
                f'{obj_func} is not implemented for plotting. '
                f'Only {list(OBJ_FUNCS.keys())} can be plotted.\n'
                f'Feel free to implement them in python :)')

        mpld3.disable_notebook(
        )  # contour plots cannot be json serialized so we have to switch of d3 mode
        min_x, max_x, min_y, max_y = viz_utils.get_min_max_of_evolution_data(
            evolution_data)
        x_list = np.linspace(min_x, max_x, 100)
        y_list = np.linspace(min_y, max_y, 100)
        x_mesh, y_mesh = np.meshgrid(x_list, y_list)

        z_mesh = OBJ_FUNCS[obj_func](np.vstack(
            [x_mesh.ravel(), y_mesh.ravel()])).reshape((100, 100))

        ax.contourf(
            x_mesh,
            y_mesh,
            z_mesh,
            [rosenbrock(np.array([k, k])) for k in np.linspace(1, 20, 50)],
            cmap='jet',
            locator=ticker.LogLocator(),
            alpha=0.1)

    for iter_idx, step_dict in enumerate(evolution_data):

        if iter_idx == 0:
            color = 'black'
        else:
            color = viz_utils.get_color_from_cm(cm.hot, 1, len(evolution_data),
                                                iter_idx + 1)

        x = [pos[0] for pos in step_dict.values()]
        y = [pos[1] for pos in step_dict.values()]

        ax.plot(x, y, '.', color=color, alpha=0.7, markeredgewidth=0.0)

    if 'xlims' in kwargs:
        ax.set_xlim(kwargs['xlims'])
    if 'ylims' in kwargs:
        ax.set_ylim(kwargs['ylims'])
    if 'title' in kwargs:
        ax.set_title(kwargs['title'])

    return fig, ax
Exemplo n.º 30
0
    def doRenderMpld3(self, handlerId, figure, axes, keyFields, keyFieldValues, keyFieldLabels, valueFields, valueFieldValues):
        allNumericCols = self.getNumericalFieldNames()
        if len(allNumericCols) == 0:
            self._addHTML("Unable to find a numerical column in the dataframe")
            return
        
                 
        keyFields = self.options.get("keyFields")
        valueField = self.options.get("valueFields")

        if(keyFields==None and valueField==None):
            keyFields=self.getFirstStringColInfo()
            valueField=self.getFirstNumericalColInfo() 
        else:
            keyFields = keyFields.split(',') 
            valueField = valueField.split(',') 
            if(len(valueField) > 1):
                self._addHTML("You can enter only have one value field for Bar Charts (2-D)"+str(len(valueField)))
                return
            keyFields = keyFields[0]
            valueField=valueField[0]
        
                
        #if(len(valueFields>)):


    
        #init
        fig=figure
        ax=axes
        
        #fig, ax = plt.subplots()   
        #fig = plt.figure()
        

        params = plt.gcf()
        plSize = params.get_size_inches()
        params.set_size_inches( (plSize[0]*2, plSize[1]*2) )


        agg=self.options.get("aggregation")
        groupByCol=self.options.get("groupByCol")
        
        if (agg=="None" or agg==None):
            colLabel = keyFields
            y = self.entity.select(valueField).toPandas()[valueField].dropna().tolist()
            x_intv = np.arange(len(y))
            labels =  self.entity.select(keyFields).toPandas()[keyFields].dropna().tolist()
            plt.xticks(x_intv,labels)
            plt.xlabel(keyFields, fontsize=18)
            plt.ylabel(valueField, fontsize=18)
        elif(agg=='AVG'):
            y1=self.entity.groupBy(keyFields).agg(F.avg(valueField).alias("avg")).toPandas().sort_values(by=keyFields)
            y=y1["avg"].dropna().tolist()
            x_intv = np.arange(len(y))
            labels=y1[keyFields].dropna().tolist()
            plt.xticks(x_intv,labels)
            plt.xlabel(keyFields, fontsize=18)
            plt.ylabel("Average "+valueField, fontsize=18)
        elif(agg=='SUM'):
            y1=self.entity.groupBy(keyFields).agg(F.sum(valueField).alias("sum")).toPandas().sort_values(by=keyFields)
            y=y1["sum"].dropna().tolist()
            x_intv = np.arange(len(y))
            labels=y1[keyFields].dropna().tolist()
            plt.xticks(x_intv,labels)
            plt.xlabel(keyFields, fontsize=18)
            plt.ylabel("sum "+valueField, fontsize=18)
        elif(agg=='MAX'):
            y1=self.entity.groupBy(keyFields).agg(F.max(valueField).alias("max")).toPandas().sort_values(by=keyFields)
            y=y1["max"].dropna().tolist()
            x_intv = np.arange(len(y))
            labels=y1[keyFields].dropna().tolist()
            plt.xticks(x_intv,labels)
            plt.xlabel(keyFields, fontsize=18)
            plt.ylabel("max "+valueField, fontsize=18)
        elif(agg=='MIN'):
            y1=self.entity.groupBy(keyFields).agg(F.min(valueField).alias("min")).toPandas().sort_values(by=keyFields)
            y=y1["min"].dropna().tolist()
            x_intv = np.arange(len(y))
            labels=y1[keyFields].dropna().tolist()
            plt.xticks(x_intv,labels)
            plt.xlabel(keyFields, fontsize=18)
            plt.ylabel("min "+valueField, fontsize=18)
        elif(agg=='COUNT'):
            y1=self.entity.groupBy(keyFields).agg(F.count(valueField).alias("count")).toPandas().sort_values(by=keyFields)
            y=y1["count"].dropna().tolist()
            x_intv = np.arange(len(y))
            labels=y1[keyFields].dropna().tolist()
            plt.xticks(x_intv,labels)
            plt.xlabel(keyFields, fontsize=18)
            plt.ylabel("count "+valueField, fontsize=18)

        mpld3.enable_notebook()      
        plt.bar(x_intv,y,color="blue",alpha=0.5)
        ax_fmt = BarChart(labels)
        mpld3.plugins.connect(fig, ax_fmt)
Exemplo n.º 31
0
vsx, vsx_x, vsx_y = catalog_search(
    ccd.wcs, ccd.shape, 'B/vsx/vsx', 'RAJ2000', 'DEJ2000')
vsx_names = vsx['Name']
print(ccd.shape)
print(type(ccd))


# In[5]:

disp = scale_and_downsample(ccd.data, downsample=1)


# In[6]:

import mpld3
mpld3.enable_notebook()
# mpld3.disable_notebook()


# In[7]:

plt.figure(figsize=(12, 7))
plt.imshow(disp, cmap='gray', origin='lower')
plt.scatter(vsx_x, vsx_y, c='none', s=100, edgecolor='cyan')
plt.title('Blue: VSX, Yellow: APASS', fontsize=20)

for x, y, m in zip(vsx_x, vsx_y, vsx_names):
    plt.text(x, y, str(m), fontsize=18, color='cyan')

plt.scatter(in_apass_x, in_apass_y, c='none', s=50,
            edgecolor='yellow', alpha=0.5, marker='o')
Exemplo n.º 32
0
def plot(  # plot all the data
    x,
    xconf,
    date,
    yconfirmados,
    outputconf,
    erfoutputconf,
    cumconf,
    erfcumconf,
    ysuspeitos,
    Diamaxconf,
    amplitudeconf,
):
    mpld3.enable_notebook()
    fig = plt.figure(1)
    fig.suptitle("Casos de COVID-19 em portugal", fontsize=14, fontweight="bold")

    ax1 = fig.add_subplot(411)
    ax2 = fig.add_subplot(412)
    ax3 = fig.add_subplot(413)
    ax4 = fig.add_subplot(414)

    fig.subplots_adjust(top=0.80)

    ax1.set_title(
        "Ajustes logístico e de Gompertz"
        + "\n"
        + "Confirmados: máximo a "
        + str(Diamaxconf.strftime("%d/%m/%Y"))
        + " com "
        + str(amplitudeconf)
        + " casos."
    )

    # ax1.set_ylabel("Casos")
    ax1.plot(x, yconfirmados, "ro", label="Casos confirmados")
    ax1.plot(xconf, outputconf, label="Fit Logístico")
    ax1.xaxis.set_visible(False)
    #ax1.set_ylim(-5, 15000)
    ax1.legend()

    ax2.set_ylabel("Casos")
    ax2.bar(date, cumconf, width=0.8, label="Casos novos", color="g")
    ax2.xaxis.set_visible(False)
    ax2.legend()

    ax3.plot(x, yconfirmados, "mo", label="Casos confirmados")
    ax3.plot(xconf, erfoutputconf, label="Fit Gompertz")
    ax3.xaxis.set_visible(False)
    # ax3.set_ylim(-5, 10000)
    ax3.legend()

    ax4.set_ylabel("Casos")
    bars = ax4.bar(date, erfcumconf, width=0.8, label="Gompertz Casos novos", color="g")
    ax4.legend()

    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d"))
    plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=7))
    plt.gcf().autofmt_xdate()
    plt.savefig("prediction.pdf")
    plt.savefig("prediction.png")
    plt.show()