sparrow
sparrow

Reputation: 11460

Trouble applying color scale to Plotly with Pandas and Cuflinks

I'm trying to apply a color scale to a graph that I'm generating from a dataframe using pandas, plotly and cuflinks.

It's breaking if I try to use a certain colorscale:

import pandas as pd
import cufflinks as cf
import colorlover as cl
from plotly.offline import download_plotlyjs, init_notebook_mode,plot,iplot
from IPython.display import HTML

init_notebook_mode(connected=True)
cf.go_offline()

Here are my colorscales, both of which I can display fine like this:

bupu = cl.scales['9']['seq']['BuPu']
cs12 = cl.scales['12']['qual']['Paired']
HTML(cl.to_html(cs12))

If I create a plot using 'bupu' it works great, however if I attempt he same with 'cs12' I get an error:

This works:

df.iplot(kind='bar',colorscale='bupu')

This does not:

df.iplot(kind='bar',colorscale='cs12')

KeyError: 'cs12'

Upvotes: 0

Views: 1437

Answers (2)

Maximilian Peters
Maximilian Peters

Reputation: 31709

From the documentation:

colorscale must be an array containing arrays mapping a normalized value to an rgb, rgba, hex, hsl, hsv, or named color string. [...]

Alternatively, colorscale may be a palette name string of the following list: Greys, YlGnBu, Greens, YlOrRd, Bluered, RdBu, Reds, Blues, Picnic, Rainbow, Portland, Jet, Hot, Blackbody, Earth, Electric, Viridis

In order to use the colorlover module you could create the color array and then assign the variable to colorscale, e.g.

import colorlover, plotly

cs12 = colorlover.scales['12']['qual']['Paired']

plotly.plotly.plot(data=[{
  'colorscale': cs12
  'z': [[10, 100.625, 1200.5, 150.625, 2000],
       [5000.625, 60.25, 8.125, 100000, 150.625],
       [2000.5, 300.125, 50., 8.125, 12.5],
       [10.625, 1.25, 3.125, 6000.25, 100.625],
       [0, 0.625, 2.5, 50000.625, 10]],
  'type': 'heatmap'
}])

Upvotes: 2

sparrow
sparrow

Reputation: 11460

I just created my own custom pallet

mycolors = ['#F81106','#FA726C','#F8C1BE',
            '#137503','#54B644','#B2F5A7',
            '#051E9B','#4358C0','#A6B3F9',
           '#9C06A0','#C34BC6','#F3A1F6',
           '#A07709','#CDA742','#F4DC9D',
           '#08A59E','#4DD5CE','#AAF7F3']

colors=mycolors

Upvotes: 2

Related Questions