Chart

class qf_lib.plotting.charts.chart.Chart(start_x: Optional[Any] = None, end_x: Optional[Any] = None, upper_y: Optional[Any] = None, lower_y: Optional[Any] = None)[source]

Bases: object

Abstract class for all the charts.

Parameters:
  • start_x (Any) – The upper bound of the x-axis.

  • end_x (Any) – The lower bound of the x-axis.

  • upper_y (Anny) – The upper bound of the y-axis.

  • lower_y (Anny) – The lower bound of the y-axis.

Methods:

add_decorator(decorator)

Adds the new decorator to the chart.

apply_data_element_decorators(...)

Plots all DataElementDecorators added to a chart.

close()

Closes the window containing the figure.

determine_end_x(start, series_list)

Implements a heuristic for determining the x-axis end date based on a start date and series list.

extract_series_data()

Extract data from data element decorators added to the chart.

get_axes_colors()

Returns the list of colors used for plotting on each Axes object.

plot([figsize])

Plots the chart.

render_as_base64_image([figsize, dpi, optimise])

Plots the chart and returns the base64 image.

setup_secondary_axes_if_necessary()

Creates a secondary axes if one has not already been created.

show()

Shows the chart.

Attributes:

axes

Axes is the object on which the chart is plotted ("the drawing area").

secondary_axes

A secondary axes on which data is plotted.

tick_color

Foreground color of all x and y ticks' labels.

tick_fontsize

x and y ticks' labels' font size.

tick_fontweight

x and y ticks' labels' font weight. Acceptable values: For more info, see: http://matplotlib.org/api/text_api.html#matplotlib.text.Text Acceptable values: [a numeric value in range 0-1000 | ‘ultralight’ | ‘light’ | ‘normal’ | ‘regular’ | ‘book’ | ‘medium’ | ‘roman’ |‘semibold’ | ‘demibold’ | ‘demi’ | ‘bold’ | ‘heavy’ | ‘extra bold’ | ‘black’ ].

add_decorator(decorator: ChartDecorator) None[source]

Adds the new decorator to the chart.

Each decorator must have a unique key that also doesn’t clash with any series keys because both are used for legend label data. If there is already a decorator registered under the specified key, the operation will raise the AssertionError.

Parameters:

decorator (ChartDecorator) – decorator to be added

apply_data_element_decorators(data_element_decorators: List[DataElementDecorator])[source]

Plots all DataElementDecorators added to a chart. This function should set legend_artist field in each plotted DataElementDecorator (if applicable).

Parameters:

data_element_decorators (List[DataElementDecorator]) – non-empty list of DataElementDecorators that should be plotted on the chart

property axes

Axes is the object on which the chart is plotted (“the drawing area”).

close()[source]

Closes the window containing the figure.

classmethod determine_end_x(start: datetime, series_list: List[Union[QFSeries, DataElementDecorator]]) datetime[source]

Implements a heuristic for determining the x-axis end date based on a start date and series list.

This is done by checking the year difference between the most recent date among the data points in all of the specified series and the specified start date. If the difference is greater than or equal to 10 years, the nearest date aligned to a 5 year boundary will be returned. Otherwise a datetime representing the nearest future January is returned.

extract_series_data()[source]

Extract data from data element decorators added to the chart.

classmethod get_axes_colors() List[str][source]

Returns the list of colors used for plotting on each Axes object. Colors are taken from the currently set plotting style.

plot(figsize: Optional[Tuple[float, float]] = None)[source]

Plots the chart. The underlying figure stays hidden until the show() method is called.

Parameters:

figsize (Tuple[float, float]) – The figure size to draw the chart at in inches. This is a tuple of (width, height) passed directly to matplotlib’s plot function. The values are expressed in inches.

render_as_base64_image(figsize: Optional[Tuple[float, float]] = None, dpi: int = 250, optimise: bool = False, **savefig_settings) str[source]

Plots the chart and returns the base64 image.

property secondary_axes

A secondary axes on which data is plotted. Created by calling setup_secondary_axes_if_necessary.

setup_secondary_axes_if_necessary()[source]

Creates a secondary axes if one has not already been created.

show()[source]

Shows the chart. It is necessary to call the plot function first.

tick_color

Foreground color of all x and y ticks’ labels. Acceptable values: Any matplotlib color, for example “black” or “#8ac72e”.

tick_fontsize

x and y ticks’ labels’ font size. Acceptable values: [size in points | ‘xx-small’ | ‘x-small’ | ‘small’ | ‘medium’ | ‘large’ | ‘x-large’ | ‘xx-large’ ]

tick_fontweight

x and y ticks’ labels’ font weight. Acceptable values: For more info, see: http://matplotlib.org/api/text_api.html#matplotlib.text.Text Acceptable values: [a numeric value in range 0-1000 | ‘ultralight’ | ‘light’ | ‘normal’ | ‘regular’ | ‘book’ | ‘medium’ | ‘roman’ |‘semibold’ | ‘demibold’ | ‘demi’ | ‘bold’ | ‘heavy’ | ‘extra bold’ | ‘black’ ]