Richard
Richard

Reputation: 61429

Problems using `scale_size_manual` in plotnine

I'm trying to use plotnine to build a plot with points sized based on their a classification 1-4.

In the MWE below, I've constructed a test dataset that matches mine.

from plotnine import *
import pandas as pd
import numpy as np

a = pd.DataFrame(data={'a':4*list(range(4)), 'x':np.random.random(16), 'y':np.random.random(16)})

ggplot(a, aes(x='x', y='y', size='a')) + geom_point() + scale_size_manual(values=(3,3,3,3))

However, when I run the code, I get the following message:

TypeError: Continuous value supplied to discrete scale

Thinking that the problem was that the a column was non-categorical, I tried the following MWE:

from plotnine import *
import pandas as pd
import numpy as np

a = pd.DataFrame(data={'a':4*list(range(4)), 'x':np.random.random(16), 'y':np.random.random(16)})

a['a'] = a['a'].astype('category')

ggplot(a, aes(x='x', y='y', size='a')) + geom_point() + scale_size_manual(values=(3,3,3,3))

However, this gives the error message:

ValueError: cannot convert float NaN to integer

In R's ggplot (which plotnine attempts to mirror), the following

library(ggplot2)
a = data.frame(a=rep(1:4,4), x=runif(16), y=runif(16))
ggplot(a, aes(x=x, y=y, size=a)) + geom_point() + scale_size_manual(values=c(3,3,3,3))

gives the familiar message

Error: Continuous value supplied to discrete scale

Introducing a factor solves the issue:

library(ggplot2)
a   = data.frame(a=rep(1:4,4), x=runif(16), y=runif(16))
a$a = factor(a$a)
ggplot(a, aes(x=x, y=y, size=a)) + geom_point() + scale_size_manual(values=c(3,3,3,3))

So plotnine seems to be treating categorical information slightly differently from ggplot.

How can I get plotnine to do what I want?

Upvotes: 1

Views: 1080

Answers (1)

Blizzard
Blizzard

Reputation: 358

This question was recently answered on github and turns out to be a bug stemming from how plotnine interfaces with numpy.

You can avoid the problem with:

... + scale_size_manual(values=(3,3,3,3), na_value=-1)

Upvotes: 1

Related Questions