Skip to content

Conversation

MichaelSchuldes
Copy link

I created a theme for the plotting library Vega-Altair based on your matplotlib theme an included it in the python package. The modification doesnt require anyone to install altair but if its installed, it add the themes on the import of RWTHColors. You use it as follows:

import altair as alt
import RWTHColors

alt.theme.enable("rwth") # or "rwth-full" or "rwth-dark"

You can see the default altair theme and the rwth themes in the pictures below.

fig-default
fig-rwth
fig-rwth-full
fig-rwth-dark

The images are created with the following code (install the following dependencies: pip install altair vega_datasets vl-convert-python

import altair as alt
import numpy as np
from vega_datasets import data
import pandas as pd
import RWTHColors

def plot_graphs(theme=''):
    source = pd.DataFrame(
    {
        "a": ["A", "B", "C", "D", "E", "F", "G", "H", "I"],
        "b": [28, 55, 43, 91, 81, 53, 19, 87, 52],
    }
    )

    bar = alt.Chart(source).mark_bar(tooltip=True).encode(x="a:N", y="b:Q").properties(title='Bar Chart')

    source_stock = data.stocks()

    stacked = alt.Chart(source_stock).mark_line().encode(
        x="date:T",
        y="price:Q",
        color="symbol:N",
        strokeDash="symbol:N",
    ).properties(title='Line Chart')

    source_area = data.iowa_electricity()

    area = alt.Chart(source_area).mark_area().encode(x="year:T", y="net_generation:Q", color="source:N").properties(title='Area Chart')

    x, y = np.meshgrid(range(-5, 5), range(-5, 5))
    z = x**2 + y**2

    source_heat = pd.DataFrame({"x": x.ravel(), "y": y.ravel(), "z": z.ravel()})

    heat = alt.Chart(source_heat).mark_rect().encode(x="x:O", y="y:O", color=alt.Color("z:Q")).properties(title='Heat Map')

    heat_diverging = alt.Chart(source_heat).mark_rect().encode(x="x:O", y="y:O", color=alt.Color("z:Q", scale=alt.Scale(domainMid=30))).properties(title='Heat Map - Diverging')

    return (bar | stacked | area | heat | heat_diverging).properties(title=f'Altair Theme: {theme}')

for x in ['default','rwth','rwth-full','rwth-dark']:
    alt.theme.enable(x)
    f = plot_graphs(x)
    f.save(f"fig-{x}.svg")

I could include the code above as a test, but that would require the above packages as a dev dependency. Is this wanted?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant