Search code examples
pythonplotlyplotly-dashplotly-python

How to change parameters of a calplot when calplot is accessed through plotly-calplot?


I have two figures. One is a calplot and the other is a plotly-calplot. In my calplot I used the following parameters:

  • colorbar=True
  • fillcolor="w"
  • linecolor="w"
  • fig_kws=dict(facecolor="w")
  • subplot_kws=dict(facecolor="w")
  • edgecolor="grey"

I think it should also be possible to change these parameters in plotly-calplot, but I do not now how.


Solution

  • While there is not a 1:1 mapping between parameters, you can still customize the plotly_calplot.calplot figure just as nearly (and of course, interactively-wise, much more than) as with the static Python calplot library

    E.g.,

    calplot:

    To illustrate, the following code produces a static calplot using the Python package "calplot":

    import numpy as np; np.random.seed(sum(map(ord, 'calplot')))
    import pandas as pd
    import calplot
    
    all_days = pd.date_range('1/1/2022', periods=730, freq='D')
    days = np.random.choice(all_days, 500)
    events = pd.Series(np.random.randn(len(days)), index=days)
    
    calplot.calplot(events, colorbar=True,
                    fillcolor="w",
                    linecolor="w",
                    fig_kws=dict(facecolor="w"),
                    subplot_kws=dict(facecolor="w"),
                    edgecolor="grey")
    

    yielding: static calplot.calplot

    vs. plotly_calplot:

    As it happens, it seems to me, the default plotly_calplot.calplot settings produce a result very nearly identical, style-wise, to the static calplot shown above (using the specified parameter settings you provided):

    import plotly_calplot
    
    df = pd.DataFrame(events, columns=["value"])
    df["date"] = df.index
    
    fig = plotly_calplot.calplot(df, x="date", y="value")
    fig.show()
    

    plotly_calplot Note: The years are swapped vs. the calplot.caplot figure above. If you wish to add axis labels showing the years, see the docs by printing them with the help(fig.layout)* command (which provides a comprehensive manual of all the possible parameters for layout [very extensive]).

    To change the style parameters of the plotly figure ("fig"), checkout the layout attribute of the fig object:

    >>> fig.layout
    
    Layout({
        'font': {'color': '#9e9e9e', 'size': 10},
        'height': 300,
        'margin': {'b': 20, 't': 20},
        'plot_bgcolor': '#fff',
        'showlegend': False,
        'template': '...',
        'title': {'text': ''},
        'xaxis': {'anchor': 'y',
                  'domain': [0.0, 1.0],
                  'showgrid': False,
                  'showline': False,
                  'tickmode': 'array',
                  'ticktext': [January, February, March, April, May, June, July,
                               August, September, October, November, December],
                  'tickvals': array([ 1.5       ,  5.90909091, 10.31818182, 14.72727273, 19.13636364,
                                     23.54545455, 27.95454545, 32.36363636, 36.77272727, 41.18181818,
                                     45.59090909, 50.        ]),
                  'zeroline': False},
        'xaxis2': {'anchor': 'y2',
                   'domain': [0.0, 1.0],
                   'showgrid': False,
                   'showline': False,
                   'tickmode': 'array',
                   'ticktext': [January, February, March, April, May, June, July,
                                August, September, October, November, December],
                   'tickvals': array([ 1.5       ,  5.90909091, 10.31818182, 14.72727273, 19.13636364,
                                      23.54545455, 27.95454545, 32.36363636, 36.77272727, 41.18181818,
                                      45.59090909, 50.        ]),
                   'zeroline': False},
        'yaxis': {'anchor': 'x',
                  'autorange': 'reversed',
                  'domain': [0.54, 1.0],
                  'showgrid': False,
                  'showline': False,
                  'tickmode': 'array',
                  'ticktext': [Mon, Tue, Wed, Thu, Fri, Sat, Sun],
                  'tickvals': [0, 1, 2, 3, 4, 5, 6],
                  'zeroline': False},
        'yaxis2': {'anchor': 'x2',
                   'autorange': 'reversed',
                   'domain': [0.0, 0.46],
                   'showgrid': False,
                   'showline': False,
                   'tickmode': 'array',
                   'ticktext': [Mon, Tue, Wed, Thu, Fri, Sat, Sun],
                   'tickvals': [0, 1, 2, 3, 4, 5, 6],
                   'zeroline': False}
    })
    

    which can be explicitly updated/overwritten using fig.update_layout, for example (before executing fig.show()).

    If it is the heatmap-like colormap legend you are especially wanting to recreate, that doesn't appear to be as easily built-in, but I imagine there is likely a way of achieving creating one. (Again, I would recommend looking through help(fig.layout).)

    Change style parameters of Plotly figure via fig.update_layout and fig.update_traces:

    *For example, the following line will properly show the years:

    fig.update_layout({"yaxis": {"title": "2023"}, "yaxis2": {"title": "2022"}})
    fig.show()
    

    plotly_calplot layout update on yaxis titles

    To show a colormap legend (and also change the cmap to also match the default of calplot.calplot [which is the classic 'Viridis' colormap]), fig.update_traces can be used:

    import plotly.express as px
    
    fig.update_traces(
        showscale = True, 
        selector=dict(type='heatmap'),
        zmax=df.value.max(),
        zmin=df.value.min(),
        colorscale=px.colors.sequential.Viridis
    )
    fig.show()
    

    update traces demo