无法使用 Pandas 和 Cuflinks 将色标应用于 Plotly
Trouble applying color scale to Plotly with Pandas and Cuflinks
我正在尝试将色标应用到我使用 pandas、plotly 和 cuflinks 从数据帧生成的图表。
如果我尝试使用特定的色标,它会崩溃:
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()
这是我的色标,我可以像这样显示它们:
bupu = cl.scales['9']['seq']['BuPu']
cs12 = cl.scales['12']['qual']['Paired']
HTML(cl.to_html(cs12))
如果我使用 'bupu' 创建绘图,效果很好,但是如果我尝试使用 'cs12' 相同,我会收到错误消息:
这个有效:
df.iplot(kind='bar',colorscale='bupu')
这不是:
df.iplot(kind='bar',colorscale='cs12')
KeyError: 'cs12'
我刚刚创建了自己的自定义托盘
mycolors = ['#F81106','#FA726C','#F8C1BE',
'#137503','#54B644','#B2F5A7',
'#051E9B','#4358C0','#A6B3F9',
'#9C06A0','#C34BC6','#F3A1F6',
'#A07709','#CDA742','#F4DC9D',
'#08A59E','#4DD5CE','#AAF7F3']
colors=mycolors
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
为了使用 colorlover 模块,您可以创建颜色数组,然后将变量分配给 colorscale
,例如
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'
}])
我正在尝试将色标应用到我使用 pandas、plotly 和 cuflinks 从数据帧生成的图表。
如果我尝试使用特定的色标,它会崩溃:
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()
这是我的色标,我可以像这样显示它们:
bupu = cl.scales['9']['seq']['BuPu']
cs12 = cl.scales['12']['qual']['Paired']
HTML(cl.to_html(cs12))
如果我使用 'bupu' 创建绘图,效果很好,但是如果我尝试使用 'cs12' 相同,我会收到错误消息:
这个有效:
df.iplot(kind='bar',colorscale='bupu')
这不是:
df.iplot(kind='bar',colorscale='cs12')
KeyError: 'cs12'
我刚刚创建了自己的自定义托盘
mycolors = ['#F81106','#FA726C','#F8C1BE',
'#137503','#54B644','#B2F5A7',
'#051E9B','#4358C0','#A6B3F9',
'#9C06A0','#C34BC6','#F3A1F6',
'#A07709','#CDA742','#F4DC9D',
'#08A59E','#4DD5CE','#AAF7F3']
colors=mycolors
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
为了使用 colorlover 模块,您可以创建颜色数组,然后将变量分配给 colorscale
,例如
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'
}])