Plots

Requires: - matplotlib >= 3.3.4 - seaborn >= 0.11.1

class defSim.tools.Plots.DynamicsPlot(colors=None, palette='deep', linewidth=3, ylim=None, xlim=None, fast: bool = False)

Bases: defSim.tools.Plots.dsPlot

Facilitates plotting the dynamics of opinion changes for a feature. Uses seaborn relplot behind the scenes. :param colors: List of colors, used to color the lines. If None, colors are set based on the palette. Usually, the number of hues should equal the number of agents (showing the opinions for each agent at each step). However, it is possible to specify another variable (see ‘hue’ in DynamicsPlot.plot()) on which to color. Then, the list of colors should be equal in length to the number of unique values of that variable. :param palette: Seaborn palette or matplotlib colormap to use. :param float linewidth=3: Width of lines drawn. :param ylim: Iterable with 2 values, which gives (ymin, ymax) :param xlim: Iterable with 2 values, which gives (xmin, xmax) :param bool fast=False: If True, show plain (but fast) plot. If False, show plot with customizable markup (slow).

__init__(colors=None, palette='deep', linewidth=3, ylim=None, xlim=None, fast: bool = False)

Initialize self. See help(type(self)) for accurate signature.

plot(data, y: str, hue=None, xlab: Optional[str] = None, ylab: Optional[str] = None, ylim=None, xlim=None)

Creates a plot showing one line for each agent, indicating their value on a chosen feature at a specific step. :param values: Pandas dataframe containing all variables to use in the plot. :param str y: The feature to plot, by name of the column in dataframe. If the pandas DataFrame in which results of the simulation are stored is named ‘results’, and the tickwise feature is named ‘f01’, the column name is ‘Tickwise_f01’ :param hue: Variable on which to group by color. To show all agents separately, leave this set to None. One line wil be drawn for each group. To color agents based on a variable but show lines for all agents individually, use a list of colors in DynamicsPlot.colors instead (equal in length to nr of agents). :param str xlab: Label for x-axis :param str ylab: Label for y-axis :param ylim: Iterable with 2 values, which gives (ymin, ymax). Overrides self.ylim if present :param xlim: Iterable with 2 values, which gives (xmin, xmax). Overrides self.xlim if present

class defSim.tools.Plots.HeatMap(colors=None, palette='rocket', vmin=None, vmax=None, annot=False, fmt=None, annot_kws=None, linewidths=None, linecolor=None, square=False, cbar=True, cbar_kws=None)

Bases: defSim.tools.Plots.dsPlot

Facilitates creation of a heatmap. X and Y are variables which determine the coordinates on the heatmap, while hue determines the ‘heat’ value. :param colors: List of colors to use (if ‘hue’ is set on calling plot()).If None, colors are set based on the palette. :param palette: Seaborn palette or matplotlib colormap to use. :param float vmin: Minimum value for hue. :param float vmax: Maximum value for hue. :param annot: True, False or dataset. If true, writes data value in each cell. If array with same shape as data then this is used to annotate the heatmap. :param str fmt: String formatting code to use when adding annotations. :param annot_kws: Dictionary of keyword arguments for drawing annotation text :param float linewidths=3: Width of lines drawn. :param linecolor: Color to use for lines between cells. :param bool square: If True, axes are adjusted so that each cell is square. :param bool cbar: Whether to draw a color bar. :param cbar_kws: Dictionary of keyword arguments to draw colorbar.

__init__(colors=None, palette='rocket', vmin=None, vmax=None, annot=False, fmt=None, annot_kws=None, linewidths=None, linecolor=None, square=False, cbar=True, cbar_kws=None)

Initialize self. See help(type(self)) for accurate signature.

plot(data, x, y, hue, sort_y_ascending=False, summary='mean', center=None, robust=False, xticklabels='auto', yticklabels='auto', mask=None, ax=None, cbar_ax=None, **kwargs)

Creates a heatmap showing the relationship between X, Y and some outcome represented by hue. :param data: Pandas dataframe containing all variables to use in the plot. :param str x: Name of the X-variable :param str y: Name of the Y-variable :param str hue: Name of the variable on which to color the cells (‘heat’) :param bool sort_y_ascending: Whether to sort Y ascending (True) or descending (False) :param str summary: Type of summary to prsent in the heatmap. Default is mean. The hue presented in each cell is the result of the summary function applied to all cases with a particular combination of X and Y :param float center: Value at which to center the colormap :param bool robust: If True and vmin, vmax are None, colormap range is computed with robust quantiles instead of extreme values. :param xticklabels: Labels for ticks on X-axis :param yticklabels: Labels for tick on Y-axis :param mask: If set, data is not shown in cells where mask is True. :param ax: Matplotlib axes in which to draw the plot (currently-active axes used if not specified) :param cbar_ax: Matplotlib axes in which to draw the colorbar (takes space from main axes if not specified)

class defSim.tools.Plots.LinePlot(colors=None, palette='rocket', palette_as_cmap=False, ylim=None, xlim=None, linewidth=3)

Bases: defSim.tools.Plots.RelPlot

Shorthand for using RelPlot with kind=’line’. :param colors: List of colors to use (if ‘hue’ is set on calling plot()).If None, colors are set based on the palette. :param palette: Seaborn palette or matplotlib colormap to use. :param bool palette_as_cmap=False: If True, the palette is used as a matplotlib colormap, suitable for continuous variables. If False, the palette is used to generate distinct hues, equal in number to the number of unique values in the hue parameter when calling plot(). This is suitable for categorical variables. :param float linewidth=3: Width of lines drawn.

__init__(colors=None, palette='rocket', palette_as_cmap=False, ylim=None, xlim=None, linewidth=3)

Initialize self. See help(type(self)) for accurate signature.

class defSim.tools.Plots.NetworkPlot(node_size: float = 100, cmap=<matplotlib.colors.LinearSegmentedColormap object>, edge_alpha: float = 0.2)

Bases: defSim.tools.Plots.dsPlot

Facilitates creating network plots, using networkx plot functions. These functions use matplotlib behind the scenes.

Parameters
  • node_size=100 (float) – Sets the size of nodes in the plot.

  • cmap – Matplotlib color map to apply to the network nodes.

  • edge_alpha=0.2 (float) – Sets opacity of edges.

__init__(node_size: float = 100, cmap=<matplotlib.colors.LinearSegmentedColormap object>, edge_alpha: float = 0.2)

Initialize self. See help(type(self)) for accurate signature.

plot(network, feature: Optional[str] = None, colors=None, title=None, pos=None, layout=<function fruchterman_reingold_layout>)

Creates the network plot. :param network: Network to draw :param str feature: Feature (by name) on which colors should be based. If none is given all nodes are colored the same. :param colors: List of colors equal in length to the number of nodes, used to color the nodes. If None, colors are set based on the colormap. :param str title: Title for the plot :param pos: (Optional) representation of network position for each node, can be used to keep the network layout exactly the same between plots. If pos is not given, network positions are generated using layout function. :param layout=networkx.spring_layout: Function to use to generate the network layout. Can also be set to

‘grid’ to plot grid networks accurately, as there is no standard networkx layout for this.

class defSim.tools.Plots.RelPlot(colors=None, palette='rocket', palette_as_cmap: bool = False, ylim=None, xlim=None, linewidth=3, kind='line')

Bases: defSim.tools.Plots.dsPlot

Facilitates plotting the relationship between two variables, most commonly from an experiment. Has two variants (LinePlot and ScatterPlot) which can both be accessed through RelPlot by specifying kind. Alternatively, use dsPlots.LinePlot or dsPlots.ScatterPlot. :param colors: List of colors to use (if ‘hue’ is set on calling plot()).If None, colors are set based on the palette. :param palette: Seaborn palette or matplotlib colormap to use. :param bool palette_as_cmap=False: If True, the palette is used as a matplotlib colormap, suitable for continuous variables. If False, the palette is used to generate distinct hues, equal in number to the number of unique values in the hue parameter when calling plot(). This is suitable for categorical variables. :param ylim: Iterable with 2 values, which gives (ymin, ymax) :param xlim: Iterable with 2 values, which gives (xmin, xmax) :param float linewidth=3: Width of lines drawn. :param kind: Determines type of plot. Select from ‘line’ or ‘scatter’.

__init__(colors=None, palette='rocket', palette_as_cmap: bool = False, ylim=None, xlim=None, linewidth=3, kind='line')

Initialize self. See help(type(self)) for accurate signature.

plot(data, x: str, y: str, hue: Optional[str] = None, xlab: Optional[str] = None, ylab: Optional[str] = None)

Creates a plot showing the relationship between X and Y, separately for all values of hue. Hue is used to create multiple lines with distinct colors. :param data: Pandas dataframe containing all variables to use in the plot. :param str x: Name of the X-variable :param str y: Name of the Y-variable :param str hue: Name of the variable on which to split into lines with different colors (optional) :param str xlab: Label for X-axis. If None, name of the X-variable is used :param str ylab: Label for Y-axis. If None, name of the Y-variable is used

class defSim.tools.Plots.ScatterPlot(colors=None, palette='rocket', palette_as_cmap=False, ylim=None, xlim=None, linewidth=3)

Bases: defSim.tools.Plots.RelPlot

Shorthand for using RelPlot with kind=’scatter’. :param colors: List of colors to use (if ‘hue’ is set on calling plot()).If None, colors are set based on the palette. :param palette: Seaborn palette or matplotlib colormap to use. :param bool palette_as_cmap=False: If True, the palette is used as a matplotlib colormap, suitable for continuous variables. If False, the palette is used to generate distinct hues, equal in number to the number of unique values in the hue parameter when calling plot(). This is suitable for categorical variables. :param float linewidth=3: Width of lines drawn.

__init__(colors=None, palette='rocket', palette_as_cmap=False, ylim=None, xlim=None, linewidth=3)

Initialize self. See help(type(self)) for accurate signature.

class defSim.tools.Plots.dsPlot

Bases: object

This class is responsible for generating plots. Inherit from this class and implement at least the plot method for each type of plot.

__init__()

Initialize self. See help(type(self)) for accurate signature.

plot()
show()