Skip to content

Commit

Permalink
Trac #33133: Add pictures to scatter_plot documentation
Browse files Browse the repository at this point in the history
URL: https://trac.sagemath.org/33133
Reported by: jhonrubia6
Ticket author(s): Javier Honrubia González
Reviewer(s): Kwankyu Lee
  • Loading branch information
Release Manager committed Feb 14, 2022
2 parents 44fb492 + e4ab8e0 commit 83c2a60
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions src/sage/plot/scatter_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
Scatter Plots
"""

#*****************************************************************************
# *****************************************************************************
# Copyright (C) 2006 Alex Clemesha <clemesha@gmail.com>,
# William Stein <wstein@gmail.com>,
# 2008 Mike Hansen <mhansen@gmail.com>,
# 2009 Emily Kirkman
# 2022 Javier Honrubia <jhonrubia6@alumno.uned.es>
#
# Distributed under the terms of the GNU General Public License (GPL)
#
Expand All @@ -18,10 +19,11 @@
# The full text of the GPL is available at:
#
# http://www.gnu.org/licenses/
#*****************************************************************************
# *****************************************************************************
from sage.plot.primitive import GraphicPrimitive
from sage.misc.decorators import options


class ScatterPlot(GraphicPrimitive):
"""
Scatter plot graphics primitive.
Expand Down Expand Up @@ -91,12 +93,12 @@ def _allowed_options(self):
"""
return {'markersize': 'the size of the markers.',
'marker': 'What shape to plot the points. See the documentation of plot() for the full list of markers.',
'alpha':'How transparent the marker border is.',
'rgbcolor':'The color as an RGB tuple.',
'hue':'The color given as a hue.',
'facecolor':'The color of the marker face.',
'edgecolor':'The color of the marker border.',
'zorder':'The layer level in which to draw.',
'alpha': 'How transparent the marker border is.',
'rgbcolor': 'The color as an RGB tuple.',
'hue': 'The color given as a hue.',
'facecolor': 'The color of the marker face.',
'edgecolor': 'The color of the marker border.',
'zorder': 'The layer level in which to draw.',
'clip': 'Whether or not to clip.'}

def _repr_(self):
Expand All @@ -110,7 +112,7 @@ def _repr_(self):
sage: ScatterPlot(numpy.array([0,1,2]), numpy.array([3.5,2,5.1]), {})
Scatter plot graphics primitive on 3 data points
"""
return 'Scatter plot graphics primitive on %s data points'%len(self.xdata)
return 'Scatter plot graphics primitive on %s data points' % len(self.xdata)

def _render_on_subplot(self, subplot):
"""
Expand All @@ -130,11 +132,12 @@ def _render_on_subplot(self, subplot):
"""
options = self.options()
p = subplot.scatter(self.xdata, self.ydata, alpha=options['alpha'],
zorder=options['zorder'], marker=options['marker'],
s=options['markersize'], facecolors=options['facecolor'],
edgecolors=options['edgecolor'], clip_on=options['clip'])
zorder=options['zorder'], marker=options['marker'],
s=options['markersize'], facecolors=options['facecolor'],
edgecolors=options['edgecolor'], clip_on=options['clip'])
if not options['clip']:
self._bbox_extra_artists=[p]
self._bbox_extra_artists = [p]


@options(alpha=1, markersize=50, marker='o', zorder=5, facecolor='#fec7b8', edgecolor='black', clip=True, aspect_ratio='automatic')
def scatter_plot(datalist, **options):
Expand Down Expand Up @@ -165,19 +168,31 @@ def scatter_plot(datalist, **options):
sage: scatter_plot([[0,1],[2,2],[4.3,1.1]], marker='s')
Graphics object consisting of 1 graphics primitive
.. PLOT::
from sage.plot.scatter_plot import ScatterPlot
S = scatter_plot([[0,1],[2,2],[4.3,1.1]], marker='s')
sphinx_plot(S)
Extra options will get passed on to :meth:`~Graphics.show`, as long as they are valid::
sage: scatter_plot([(0, 0), (1, 1)], markersize=100, facecolor='green', ymax=100)
Graphics object consisting of 1 graphics primitive
sage: scatter_plot([(0, 0), (1, 1)], markersize=100, facecolor='green').show(ymax=100) # These are equivalent
.. PLOT::
from sage.plot.scatter_plot import ScatterPlot
S = scatter_plot([(0, 0), (1, 1)], markersize=100, facecolor='green', ymax=100)
sphinx_plot(S)
"""
import numpy
from sage.plot.all import Graphics
g = Graphics()
g._set_extra_kwds(Graphics._extract_kwds_for_show(options))
data = numpy.array(datalist, dtype='float')
if len(data) != 0:
xdata = data[:,0]
ydata = data[:,1]
xdata = data[:, 0]
ydata = data[:, 1]
g.add_primitive(ScatterPlot(xdata, ydata, options=options))
return g

0 comments on commit 83c2a60

Please sign in to comment.