Plot¶
-
class
visualife.core.Plot(viewport, min_screen_x, max_screen_x, min_screen_y, max_screen_y, min_data_x=0, max_data_x=1, min_data_y=0, max_data_y=1, axes_definition='BL')¶ Bases:
objectCreates a plot object with axes
Parameters: - viewport (
core.HtmlViewportorcore.SvgViewport) – selected viewport object to draw a plot - min_screen_x (
float) – starting x value to draw a plot in screen coordinates - max_screen_x (
float) – ending point x value to draw a plot in screen coordinate - min_screen_y (
float) – starting point x value to draw a plot in screen coordinate - max_screen_y (
float) – ending point x value to draw a plot in screen coordinate - min_data_x (
float) – minimum x data value - max_data_x (
float) – maximum x data value - min_data_y (
float) – minimum y data value - max_data_y (
float) – maximum y data value - axes_definition (
string) – string containg letters represents axis to create in a plot <B - bottom, U - upper, R - right, L - left> (“BL” set by default)
The example below creates a plot in a SVG viewport:
drawing = SvgViewport("scatterplot.svg", 0, 0, 800, 400) plot = Plot(drawing,50,750,50,350,0.0,0.5,0.0,0.5, axes_definition="UBLR")
Viewport is 800x400, the plot takes 700x300 of it (from 50 to 750 and from 50 to 350 along X and Y asis, respectively) All four axis will be shown because of “UBLR” argument. Output plot will be stored in
scatterplot.svgfile (SVG format). If you want to make a plot on a web-page, usecore.HtmlViewport:from random import random from browser import document from visualife.core import HtmlViewport from visualife.core.Plot import Plot xy_data = [ (random(), random()) for i in range(500)] drawing = HtmlViewport(document['svg'],600,400) pl = Plot(drawing,50,550,50,350,0.0,1.0,0.0,1.0, axes_definition="UBLR") pl.scatter(xy_data) pl.draw(grid=True) drawing.close()
Attributes Summary
axesDictionary of axes in a plot - key is a letter defines axis (U,B,R,L) and Axis object is a value axes_svgReturns axes clip_path_nameDefines clip_path_name of this plot clip_path_ticsDefines clip_path_name of this plot converterdata_idsReturns data_ids extra_labelsProvides access to the list of extra labels of this plot plot_labelDefines title (label) of this plot plot_label_font_sizeDefines the font size used to draw title (label) of this plot viewportReturns viewport Methods Summary
add_extra_label(label_text, data_x, data_y, …)Adds a new label that will be drawn in the plot. axes_svg_x()Returns axes elements as an svg object axes_svg_y()Returns axes elements as an svg object bars(*args, **kwargs)Creates a bar plot boxes(*args, **kwargs)bubbles(*args, **kwargs)Creates a bubble chart clear()draw(**kwargs)Draws this plot on its viewport draw_axes()Draws axes as an <svg> element draw_grid()Draws grid as an <svg> element draw_legend([position])Calculates legend position and size and draws it draw_plot_label()Draws a plot label - its title and all extra labels that has been added to this plot line(*args, **kwargs)Creates a line plot prepare_data_colors(kwargs_dict)Returns list of colors used to draw points scale(scale)Scales plot :param scale: provides a scale :type scale: floatscatter(*args, **kwargs)Creates a scatterplot Attributes Documentation
-
axes¶ Dictionary of axes in a plot - key is a letter defines axis (U,B,R,L) and Axis object is a value
Getter: returns a dictionary holding axes objects; dictionary keys are B,U,RandLfor bottom, top, right and left axis, respectivelyType: dictionary(Axis)
-
axes_svg¶ Returns axes
-
clip_path_name¶ Defines clip_path_name of this plot
Getter: returns the clip path name Setter: sets the new clip path name Type: string
-
clip_path_tics¶ Defines clip_path_name of this plot
Getter: returns the clip path name Setter: sets the new clip path name Type: string
-
converter¶
-
data_ids¶ Returns data_ids
-
extra_labels¶ Provides access to the list of extra labels of this plot
Returns: a list of labels, each label as a tuple (x, y, "text", dict). Thedictholds kwargs with plotting style
-
plot_label¶ Defines title (label) of this plot
Getter: returns the label as text Setter: sets the new label text Type: string
-
plot_label_font_size¶ Defines the font size used to draw title (label) of this plot
Getter: returns the label font size Setter: sets the new label font size Type: float
-
viewport¶ Returns viewport
Methods Documentation
-
add_extra_label(label_text, data_x, data_y, **kwargs)¶ Adds a new label that will be drawn in the plot.
Besides tics labels, axis labels, etc a plot may also have extra labels, placed in arbitrary locations given in data coordinates. Plot object will convert these X,Y coordinates do screen coordinates using main X and Y axes
Parameters: - label_text – text to be written in this plot
- data_x –
- data X coordinate of a label
- data_y –
- data y coordinate of a label
- kwargs –
- style parameters
Returns: None
-
axes_svg_x()¶ Returns axes elements as an svg object
-
axes_svg_y()¶ Returns axes elements as an svg object
-
bars(*args, **kwargs)¶ Creates a bar plot
Keyword Arguments (**kwargs) (**kwargs):- colors (
list[string],list[float] orlist[ color in HEX format ]) – define fill color for points; the colors are cycled over for all points, so if the list contains a single elements, all points will have the same fill color - stroke (
floator color in HEX format ) - stroke color - width (
float) - width of each bar (in data units!) - title (
string) - title of this data series; if not provided, the word “bars” will be used - converter_type (
string) - converter type to calculate axis range - may be “primary”
(bottom and left axis) or “secondary” (upper and rigth axis)
- adjust_range - False if you want to keep values from Plot constructor and True (default) if you want to have automatically adjust scale
- colors (
-
boxes(*args, **kwargs)¶
-
bubbles(*args, **kwargs)¶ Creates a bubble chart
Bubble chart displays three dimensions of data. Radius of each bubble is proportional to the square root of Z value of each point
Parameters: *args – data to be plotted; see
DataSet.__init__()documentationKeyword Arguments (**kwargs) (**kwargs):- markersize (
float) – a value used to scale radius of each bubbles - colors (
list[string],list[float] orlist[ color in HEX format ]) – define fill color for points; the colors are cycled over for all points, so if the list contains a single elements, all points will have the same fill color - stroke (
floator color in HEX format ) - stroke color - stroke_width (
float) - stroke width - title (
string) - title of this data series; if not provided, the word “bubble chart” will be used - converter_type (
string) - converter type to calculate axis range - may be “primary”
(bottom and left axis) or “secondary” (upper and rigth axis
- adjust_range - False if you want to keep values from Plot constructor and True (default) if you want to have automatically adjust scale
- markersize (
-
clear()¶
-
draw(**kwargs)¶ Draws this plot on its viewport
Its necessary to call this method at the end of plotting; only then this plot will be actually drawn. Also, any adjustments (e.g. extra labels, axis, etc.) added after this call will not be included in this plot.
Keyword Arguments (**kwargs) (**kwargs):- axes (
bool) – draws axes ifTrue(as default) - grid (
bool) – draws grid ifTrue(default isFalse) - plot_label (
bool) – draws plot_label ifTrue(as default) - legend (
string``or ``bool) – legend position; default isFalse; ifTruethen default location is set (seePlotLegend.__init__())
- axes (
-
draw_axes()¶ Draws axes as an <svg> element
-
draw_grid()¶ Draws grid as an <svg> element
-
draw_legend(position='RU')¶ Calculates legend position and size and draws it
-
draw_plot_label()¶ Draws a plot label - its title and all extra labels that has been added to this plot
-
line(*args, **kwargs)¶ Creates a line plot
Line plot displays data conected with lines.
Keyword Arguments (**kwargs) (**kwargs):- colors (
list[string],list[float] orlist[ color in HEX format ]) – define fill color for points; the colors are cycled over for all points, so if the list contains a single elements, all points will have the same fill color - stroke (
floator color in HEX format ) - stroke color - width (
float) - stroke width - title (
string) - title of this data series; if not provided, the word “line” will be used - converter_type (
string) - converter type to calculate axis range - may be “primary”
(bottom and left axis) or “secondary” (upper and rigth axis)
- adjust_range - False if you want to keep values from Plot constructor and True (default) if you want to have automatically adjust scale
- colors (
-
prepare_data_colors(kwargs_dict)¶ Returns list of colors used to draw points
This method makes the following choice:
- if
kwargs_dictdoes not contain"colors"key, a single color will be returned to plot all points the returned color will be selected from the current palette assigned to differentiate between data series - if
"colors"provides astringthat is a valid color palette name (i.e. registered instyles.known_color_scales) the requested pallete is returned - if
"colors"provides aint, the number is interpreted as an index of a color in the data series palette; a single color is returned - if
"colors"provides alistof values, a color is evaluated separately for every value using a color map Ifkwargs_dictprovides"cmap"key, the requested color map is used for that purpose; otherwise a default"blues"map is used. In addition, when"cmap_reversed"key is set toTrue, the cmap will be reversed, e.g."red_blue"color map will become"blue_red"
The colors are defined based on what user requested by kwargs parameters passed to plotting methods as``bubbles()``,
scatter()orline()- if
-
scale(scale)¶ Scales plot :param scale: provides a scale :type scale:
float
-
scatter(*args, **kwargs)¶ Creates a scatterplot
Keyword Arguments (**kwargs) (**kwargs):- markersize (
float) – size of points of this scatterplot - markerstyle (
char) – point type; available types are:- ‘o’ – circle
- ‘c’ – circle (same as ‘o’)
- ‘s’ – square
- ‘t’ – triangle
- ‘r’ – rhomb
- colors (
list[string],list[float] orlist[ color in HEX format ]) – define fill color for points; the colors are cycled over for all points, so if the list contains a single elements, all points will have the same fill color - stroke (
floator color in HEX format ) - stroke color - stroke_width (
float) - stroke width - title (
string) - title of this data series; if not provided, the word “scatter” will be used - flush (
bool) – ifTrue, the plot will be automatically send to HTML viewport have no efefct on SVG viewport; defalit isTrue - converter_type (
string) - converter type to calculate axis range - may be “primary”
(bottom and left axis) or “secondary” (upper and rigth axis)
- adjust_range - False if you want to keep values from Plot constructor and True (default) if you want to have automatically adjust scale
- markersize (
- viewport (