1

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.

Xtiaan
  • 252
  • 1
  • 11
  • Which aspects specifically, that are not already present in the plotly default, are you trying to get (e.g., the colormap legend?, the gray perimeter edge line? - those are about the only differences I see)? – John Collins Jul 26 '23 at 12:45
  • How would one change any of these values to anything in plotly-calplot. Calplot has these parameters, so I expect plotly-calplot to also have them. However, I do not know the correct syntax to access them. – Xtiaan Jul 26 '23 at 12:50
  • It would be nice if you could explain how to find out how the mapping works in general so that I can solve problems like this myself in the future :) – Xtiaan Jul 26 '23 at 13:43
  • Your post is very helpful, but I noticed many of the options like 'fillcolor', 'linecolor', 'facecolor' and 'edgecolor' for example are not available. I assume it is still possible to change those. Or is that not possible using plotly-calplot? – Xtiaan Jul 26 '23 at 15:01
  • I've updated the answer adding demonstration of how to change some of the parameters, including adding the colormap legend bar and also changing the colormap. The fill, line, and face colors yes all can be explicitly changed (but by default are already white - which is what your question's code specifies). I do not yet see a way to create the grey perimeter outline "edge" - is this a critical detail for you? – John Collins Jul 26 '23 at 16:26
  • Another thing I'd point out though, is that if you run calplot.calplot without any of the parameter options you specify, other than `colorbar`, the output produced is identical. So none of those options are actually changing anything. It also actually does seem to be the case that there is an issue with `calplot.calplot` incorrectly plotting the data. The brightest (most yellow) square (if you run the code I provide in answer), in the `calplot.calplot` does not correspond to the highest data value. The plotly calplot on the other hand seems to be precisely accurate... – John Collins Jul 26 '23 at 18:12

1 Answers1

1

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

John Collins
  • 2,067
  • 9
  • 17