Themes¶
This section showcases the existing themes found in the datachart.themes module.
Let's start by importing the necessary functions to help us work with the datachart.themes
module.
import random
import numpy as np
from datachart.charts import LineChart, BarChart, Histogram, Heatmap
from datachart.constants import VALFMT, FIG_SIZE, LINE_MARKER, LINE_STYLE
from datachart.config import config
To get the supported themes, you have to load them from the datachart.themes
module.
from datachart.constants import THEME
The datachart.constants.THEME module contains all the predefined themes.
Default Theme¶
The THEME.DEFAULT is the theme, that the user will by default see when using the datachart
package. To use it, no additional configuration is required.
In case if the user uses a different theme, or if they change it by hand using the config
module, the default theme will be reset in the following way:
# reset the config to the default theme
config.reset_config()
# explicity set the default theme
config.set_theme(THEME.DEFAULT)
What follows is a showcase of charts in the default theme.
Line Chart
_ = LineChart(
{
"charts": [
{
"data": [{"x": x / 10, "y": np.cos(x / 2)} for x in range(21)],
"style": {
"plot_line_marker": LINE_MARKER.POINT,
"plot_line_style": LINE_STYLE.DOTTED,
},
"subtitle": "cosine", # add a subtitle to the line
},
{
"data": [{"x": x / 10, "y": np.sin(x / 2)} for x in range(21)],
"subtitle": "sine", # add a subtitle to the line
},
],
"title": "Title",
"xlabel": "the global x-axis label",
"ylabel": "the global y-axis label",
"figsize": FIG_SIZE.A4_NARROW,
"show_grid": "both",
"show_legend": True, # show the legend
}
)
Bar Chart
_ = BarChart(
{
"charts": {
"data": [
{"label": f"xx{id}", "y": 100 * (id + 1) * random.random()}
for id in range(10)
],
"vlines": [{"x": 2 * i} for i in range(1, 4)],
"hlines": {"y": 400},
},
"title": "Title",
"xlabel": "the global x-axis label",
"ylabel": "the global y-axis label",
"figsize": FIG_SIZE.A4_NARROW,
"show_grid": "both",
"xmin": -0.5,
"xmax": 9.5,
}
)
Histogram
_ = Histogram(
{
"charts": [
{
"data": {"x": [100 * idx * random.random() for idx in range(100)]},
"subtitle": f"Histogram {id}",
}
for id in range(2)
],
"title": "Title",
"xlabel": "the global x-axis label",
"ylabel": "the global y-axis label",
"figsize": FIG_SIZE.A4_NARROW, # must be a tuple (width, height), values are in inches
"show_grid": "y", # options: "both", "x", "y"
"show_legend": True,
"num_bins": 40,
}
)
Heatmap
_ = Heatmap(
{
"charts": {
"data": [[x + y for x in range(8)] for y in range(8)],
"valfmt": VALFMT.DECIMAL,
"xticks": [idx for idx in range(8)],
"xticklabels": [f"xxx{idx}" for idx in range(8)],
"xtickrotate": 45,
"yticks": [idx for idx in range(8)],
"yticklabels": [f"xxx{idx}" for idx in range(8)],
"ytickrotate": 0,
},
"title": "Title",
"xlabel": "the global x-axis label",
"ylabel": "the global y-axis label",
"figsize": FIG_SIZE.SQUARE_SMALL,
"show_heatmap_values": True,
"show_colorbars": True,
}
)
Greyscale Theme¶
Under development
This theme is still under development. If you are interested in improving it, please let us know.
The THEME.GREYSCALE is designed to be monochrome and is suitable for printing. It is also used for some journals, which require a monochrome theme.
To use the theme, simply run the following code:
config.set_theme(THEME.GREYSCALE)
What follows is a showcase of charts in the greyscale theme.
Line Chart
_ = LineChart(
{
"charts": [
{
"data": [{"x": x / 10, "y": np.cos(x / 2)} for x in range(21)],
"style": {
"plot_line_marker": LINE_MARKER.POINT,
"plot_line_style": LINE_STYLE.DOTTED,
},
"subtitle": "cosine", # add a subtitle to the line
},
{
"data": [{"x": x / 10, "y": np.sin(x / 2)} for x in range(21)],
"subtitle": "sine", # add a subtitle to the line
},
],
"title": "Title",
"xlabel": "the global x-axis label",
"ylabel": "the global y-axis label",
"figsize": FIG_SIZE.A4_NARROW,
"show_grid": "both",
"show_legend": True, # show the legend
}
)
Bar Chart
_ = BarChart(
{
"charts": {
"data": [
{"label": f"xx{id}", "y": 100 * (id + 1) * random.random()}
for id in range(10)
],
"vlines": [{"x": 2 * i} for i in range(1, 4)],
"hlines": {"y": 400},
},
"title": "Title",
"xlabel": "the global x-axis label",
"ylabel": "the global y-axis label",
"figsize": FIG_SIZE.A4_NARROW,
"show_grid": "both",
"xmin": -0.5,
"xmax": 9.5,
}
)
Histogram
_ = Histogram(
{
"charts": [
{
"data": {"x": [100 * idx * random.random() for idx in range(100)]},
"subtitle": f"Histogram {id}",
}
for id in range(3)
],
"title": "Title",
"xlabel": "the global x-axis label",
"ylabel": "the global y-axis label",
"figsize": FIG_SIZE.A4_NARROW, # must be a tuple (width, height), values are in inches
"show_grid": "y", # options: "both", "x", "y"
"num_bins": 40,
}
)
Heatmap
_ = Heatmap(
{
"charts": {
"data": [[x + y for x in range(8)] for y in range(8)],
"valfmt": VALFMT.DECIMAL,
"xticks": [idx for idx in range(8)],
"xticklabels": [f"xxx{idx}" for idx in range(8)],
"xtickrotate": 45,
"yticks": [idx for idx in range(8)],
"yticklabels": [f"xxx{idx}" for idx in range(8)],
"ytickrotate": 0,
},
"title": "Title",
"xlabel": "the global x-axis label",
"ylabel": "the global y-axis label",
"figsize": FIG_SIZE.SQUARE_SMALL,
"show_heatmap_values": True,
"show_colorbars": True,
}
)
Creating Your Own Theme¶
Adding the theme to the datachart
package
If you think the theme would be useful and would like it to be added to the datachart
package, please create a pull request to add it.
The user can create their own theme by defining a new dictionary that has the same structure as the StyleAttrs type.
For instance, one can copy the bellow definition of the default theme and modify the values to customize the theme.
from datachart.typings import StyleAttrs
from datachart.constants import COLORS, FONT_STYLE, FONT_WEIGHT, LINE_DRAW_STYLE
CUSTOM_THEME: StyleAttrs = {
"color_general_singular": COLORS.Blue,
"color_general_multiple": COLORS.Spectral,
"font_general_family": "sans-serif",
"font_general_sansserif": ["Helvetica", "Arial"],
"font_general_color": "#000000",
"font_general_size": 11,
"font_general_style": FONT_STYLE.NORMAL,
"font_general_weight": FONT_WEIGHT.NORMAL,
"font_title_size": 12,
"font_title_color": "#000000",
"font_title_style": FONT_STYLE.NORMAL,
"font_title_weight": FONT_WEIGHT.NORMAL,
"font_subtitle_size": 11,
"font_subtitle_color": "#000000",
"font_subtitle_style": FONT_STYLE.NORMAL,
"font_subtitle_weight": FONT_WEIGHT.NORMAL,
"font_xlabel_size": 10,
"font_xlabel_color": "#000000",
"font_xlabel_style": FONT_STYLE.NORMAL,
"font_xlabel_weight": FONT_WEIGHT.NORMAL,
"font_ylabel_size": 10,
"font_ylabel_color": "#000000",
"font_ylabel_style": FONT_STYLE.NORMAL,
"font_ylabel_weight": FONT_WEIGHT.NORMAL,
"axes_spines_top_visible": True,
"axes_spines_right_visible": True,
"axes_spines_bottom_visible": True,
"axes_spines_left_visible": True,
"axes_spines_width": 0.5,
"axes_spines_zorder": 100,
"axes_ticks_length": 2,
"axes_ticks_label_size": 9,
"plot_legend_shadow": False,
"plot_legend_frameon": True,
"plot_legend_alignment": "left",
"plot_legend_font_size": 9,
"plot_legend_title_size": 10,
"plot_legend_label_color": "#000000",
"plot_area_alpha": 0.3,
"plot_area_color": None,
"plot_area_linewidth": 0,
"plot_area_hatch": None,
"plot_area_zorder": 3,
"plot_grid_alpha": 1,
"plot_grid_color": "#E6E6E6",
"plot_grid_linewidth": 0.5,
"plot_grid_linestyle": LINE_STYLE.SOLID,
"plot_grid_zorder": 0,
"plot_line_color": None,
"plot_line_style": LINE_STYLE.SOLID,
"plot_line_marker": None,
"plot_line_width": 1,
"plot_line_alpha": 1.0,
"plot_line_drawstyle": LINE_DRAW_STYLE.DEFAULT,
"plot_line_zorder": 3,
"plot_bar_color": None,
"plot_bar_alpha": 1.0,
"plot_bar_width": 0.8,
"plot_bar_zorder": 3,
"plot_bar_hatch": None,
"plot_bar_edge_width": 0.5,
"plot_bar_edge_color": "#000000",
"plot_bar_error_color": "#000000",
"plot_hist_color": None,
"plot_hist_alpha": 1.0,
"plot_hist_zorder": 3,
"plot_hist_fill": None,
"plot_hist_hatch": None,
"plot_hist_type": "bar",
"plot_hist_align": "mid",
"plot_hist_edge_width": 0.5,
"plot_hist_edge_color": "#000000",
"plot_vline_color": None,
"plot_vline_style": LINE_STYLE.SOLID,
"plot_vline_width": 1,
"plot_vline_alpha": 1.0,
"plot_hline_color": None,
"plot_hline_style": LINE_STYLE.SOLID,
"plot_hline_width": 1,
"plot_hline_alpha": 1.0,
"plot_heatmap_cmap": COLORS.Blue,
"plot_heatmap_alpha": 1.0,
"plot_heatmap_font_size": 9,
"plot_heatmap_font_color": "#000000",
"plot_heatmap_font_style": FONT_STYLE.NORMAL,
"plot_heatmap_font_weight": FONT_WEIGHT.NORMAL,
}
Once you define the theme, you can use it by updating the config
module in the following way:
from datachart.config import config
config.update_config(CUSTOM_THEME)
Once you do this, all the plots will use the custom theme.