Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 314
Trexion Kameha Avatar asked Sep 05 '17 04:09

Trexion Kameha


People also ask

How to change the background color of a ggplot2 plot?

You can use the following syntax to change the background color of various elements in a ggplot2 plot: p + theme (panel.background = element_rect (fill = 'lightblue', color = 'purple'), panel.grid.major = element_line (color = 'red', linetype = 'dotted'), panel.grid.minor = element_line (color = 'green', size = 2))

How do I use ggplot in Python?

Using ggplot in Python allows you to build data visualizations in a very concise and consistent way. As you’ve seen, even complex and beautiful plots can be made with a few lines of code using plotnine. This tutorial uses the example datasets included in plotnine, but you can use everything you learned to create visualizations from any other data.

How to reshape the size of a ggplot2 plot?

The current output is attached. Use the most recent ggplot2 for Python: plotnine. In order to reshape the plot size, use this theme parameter: figure_size (width, height). Width and height are in inches.

How to display more than two variables in a ggplot in Python?

In this section, you learned another way to display more than two variables in a graphic using ggplot in Python. When you have three variables, you should choose between using facets and colors depending on which approach makes the data visualization easier to understand.


2 Answers

Use the most recent ggplot2 for Python: plotnine.

In order to reshape the plot size, use this theme parameter: figure_size(width, height). Width and height are in inches.

Refer to this library documentation: https://plotnine.readthedocs.io/en/stable/generated/plotnine.themes.themeable.figure_size.html#plotnine.themes.themeable.figure_size

See the following example:

from plotnine import *

(ggplot(df) 
 + aes(x='column_X', y='column_Y', color = 'column_for_collor')    
 + geom_line()
 + theme(axis_text_x = element_text(angle = 45, hjust = 1))
 + facet_wrap('~column_to_facet', ncol = 3)   # ncol to define 3 facets per line
 + theme(figure_size=(16, 8))  # here you define the plot size
)
like image 172
Rodolfo Bugarin Avatar answered Oct 16 '22 12:10

Rodolfo Bugarin


This is the top answer that comes up in Google when searching how to increase the size of plotnine plot. None of the answers worked for me but increasing the figure size theme did:

from plotnine import *
from plotnine.data import diamonds
ggplot(diamonds, aes(x='price', color='cut')) + geom_density(alpha=0.25) + facet_wrap("clarity") + theme(figure_size = (10, 10))
like image 39
User2321 Avatar answered Oct 16 '22 12:10

User2321