Skip to main content

ggshow

Python package

Produce ggplot2 graphs from Python

Install

# from pypi
pip install ggshow

# or from github
git clone --depth 1 https://github.com/kota7/ggshow.git
pip install -U ./ggshow

Requirements

  • Python 3.7+ (but would work in some older versions)
  • Rscript command and the R environment with ggplot2 package installed

Functionalities

  • ggshow: Draw graphs using ggplot2 on R and return the Image object that you can show on Jupyter notebook. From v0.1.10, we can also save the image to the file.
  • ggwrite: Draw graphs using ggplot2 on R and save it to a file (but not show on the notebook).
  • %gg, %%gg: ipython magic for drawing ggplot2 graph.
from ggshow import ggshow, ggwrite
# Minimal example
# pass R code to produce ggplot graph to ggshow function
# note: ggplot2 is automatically imported
ggshow("""
  x <- c(1,2,3)
  y <- c(4,5,6)
  ggplot(mapping=aes(x, y)) + geom_line()
""", savesize=(3, 2))
None
# Example with data frames
# pass pandas data frames as keyword arguments
# then you can use them in the R code
import numpy as np
import pandas as pd
x = np.linspace(-20, 20, 500)
y = np.sin(x)
y2 = 0.4*np.cos(x)

df1 = pd.DataFrame({"x":x, "y":y})
df2 = pd.DataFrame({"x":x, "y":y2})
ggshow("""
  ggplot(a, aes(x, y)) + 
    geom_line(color="blue") +
    geom_line(data=b, linetype="dashed", color="red") +
    theme_bw()
""", dispwidth=500, savesize=(4, 2), a=df1, b=df2)
None
# Example to save the graph to a file
ggshow("""
  x <- c(1,2,3)
  y <- c(4,5,6)
  ggplot(mapping=aes(x, y)) + geom_line()
""", outfile="foo.jpg", savesize=(3, 2))
# Example to "only" save the graph to a file, not to show
ggshow("""
  x <- c(1,2,3)
  y <- c(4,5,6)
  ggplot(mapping=aes(x, y)) + geom_line()
""", outfile="bar.jpg", savesize=(3, 2))

# Confirm the file has been created
from IPython.display import Image
Image("bar.jpg", width=400)
# Example to specify the image file format
# But there may not be significant differences to human eyes
# Default: png
ggshow("""
  x <- c(1,2,3)
  y <- c(4,5,6)
  ggplot(mapping=aes(x, y)) + geom_line() + ggtitle("PNG")
""", savesize=(3, 2), imageformat="png")

ggshow("""
  x <- c(1,2,3)
  y <- c(4,5,6)
  ggplot(mapping=aes(x, y)) + geom_line() + ggtitle("JPEG")
""", savesize=(3, 2), imageformat="jpeg")

ggshow("""
  x <- c(1,2,3)
  y <- c(4,5,6)
  ggplot(mapping=aes(x, y)) + geom_line() + ggtitle("SVG")
""", savesize=(3, 2), imageformat="svg")
None
# Example using %gg magic
%load_ext ggshow
## line magic
%gg ggplot(mapping=aes(1, 2)) + geom_point() -s 4 3 --dispwidth 300 --outfile buz.jpeg
## line magic with the code as a string variable
x = np.linspace(-20, 20, 500)
y = np.sin(x)
y2 = 0.4*np.cos(x)

df1 = pd.DataFrame({"x":x, "y":y})
df2 = pd.DataFrame({"x":x, "y":y2})
code = """
ggplot(a, aes(x, y)) + 
  geom_line(color="blue") +
  geom_line(data=b, linetype="dashed", color="red") +
  theme_bw() +
  ggtitle("Sine and Cosine Waves")
"""

%gg {code} -s 4 2 --dispwidth 400 --data a=df1 b=df2
%%gg -s 4 2 --dispwidth 400 --data a=df1 b=df2

## cell magic
ggplot(a, aes(x, y)) + 
  geom_line(color="blue") +
  geom_line(data=b, linetype="dashed", color="red") +
  theme_bw() +
  ggtitle("Sine and Cosine Waves")
# show the full command options
%gg --help
Help on method gg in module ggshow.ggshow:

gg(line, cell=None) method of ggshow.ggshow.GGMagic instance
    ::

      %gg [--help] [--outfile OUTFILE] [-s SAVESIZE SAVESIZE] [--scale SCALE]
              [--units UNITS] [--dpi DPI] [--message_encoding MESSAGE_ENCODING]
              [--rscriptcommand RSCRIPTCOMMAND] [-w DISPWIDTH] [-h DISPHEIGHT]
              [--imageformat {png,jpeg,svg}] [--libs [LIBS ...]]
              [--data [DATA ...]]
              [plotcode ...]

    positional arguments:
      plotcode              R code

    options:
      --help
      --outfile OUTFILE     File path to save the graph
      -s <SAVESIZE SAVESIZE>, --savesize <SAVESIZE SAVESIZE>
                            height, width
      --scale SCALE         ggsave option scale
      --units UNITS         ggsave option units
      --dpi DPI             ggsave option dpi
      --message_encoding MESSAGE_ENCODING
                            Encoding of the subprocess outputs
      --rscriptcommand RSCRIPTCOMMAND
                            Rscript command name
      -w DISPWIDTH, --dispwidth DISPWIDTH
                            display width
      -h DISPHEIGHT, --dispheight DISPHEIGHT
                            display width
      --imageformat <{png,jpeg,svg}>
                            imagefile format
      --libs <[LIBS ...]>   R libraries to use
      --data <[DATA ...]>   data frames mapping as {name in r}={name in python}
# By default, we use 'Rscript' as the command to run R code.
# If this is not a valid command on the environment,
# we can specify the command by `rscriptcommand` option 
ggshow("""
  x <- c(1,2,3)
  y <- c(4,5,6)
  ggplot(mapping=aes(x, y)) + geom_line()
""", savesize=(3, 2), rscriptcommand="/usr/local/bin/Rscript")


# Or set the new command path as the default using set_rscript function.
from ggshow import config, set_rscript

set_rscript("/usr/local/bin/Rscript")  
print(config.rscript)
# this is just the full path of the command on this environment
# so the command will work in the same way
/usr/local/bin/Rscript

Release files for ggshow 0.1.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ggshow 0.1.10
File Size Uploaded
ggshow-0.1.10.tar.gz 8.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ggshow 0.1.10
File Interpreter ABI Platform
ggshow-0.1.10-py3-none-any.whl Python 3 none any Details

Total release size: 15.6 kB

Release files / ggshow-0.1.10.tar.gz

Download URL ggshow-0.1.10.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
30275cec3619cca2e12d0b1cd32b69d5c26cda606528684697df72d321107413
BLAKE2b-256 checksum
How to use checksums
ff27faf527f48f12f09cc7a3afe3fbc9063f70b31b5cb7fb4087f490bbf0a348
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release files / ggshow-0.1.10-py3-none-any.whl

Download URL ggshow-0.1.10-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
db7133dfb005651e4c1fc6befa6b9c6bb4a41a003c47b34f222e085f0b29746f
BLAKE2b-256 checksum
How to use checksums
dafc8e26c564d2dc37f520a2cdcefaa728b2906aff0a9bcda3a3def095fb41c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release history Release notifications | RSS feed

This release

0.1.10 This release

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page