pythonggplot2python-ggplot

ggplot in python: plot size and color


Folks,

I'm trying to use ggplot in python.

from ggplot import *
ggplot(diamonds, aes(x='price', fill='cut')) + geom_density(alpha=0.25) + facet_wrap("clarity")

Couple things I am trying to do:

1) I expected the color to be both filled and for the lines, but as you can see the color is all grey

2) I am trying to adjust the size of the plot. In R I would run this before the plot:

options(repr.plot.width=12, repr.plot.height=4)

However, that doesn't work here.

Does anyone know how I color in the distribution and also change the plot size?

Thank you. The current output is attached.

enter image description here


Solution

  • Color

    Use color instead of fill. e.g.;

    from ggplot import *
    ggplot(diamonds, aes(x='price', color='cut')) + geom_density(alpha=0.25) + facet_wrap("clarity")
    

    Size

    A couple of ways to do this.

    Easiest is with ggsave - look up the documentation.

    Alternatively, use theme with plot_margin argument:

    ggplot(...) ... + theme(plot_margin = dict(right = 12, top=8))
    

    Or, use matplotlib settings:

    import matplotlib as mpl
    mpl.rcParams["figure.figsize"] = "11, 8"
    ggplot(...) + ...
    

    Hope that helped!