2

Been loving the new ggplot module in Python but I haven't been able to format my y labels as percent instead of decimals. The below code produces the following image. Note that the labels = 'percent' code does not produce the intended format.

plot = ggplot(aes(x='Date', y='return', color='Stocks'),data=rx) +\
geom_line() +\
scale_x_date(breaks=date_breaks('1 day'), labels='%b %d %Y') +\
scale_y_continuous(labels= 'percent') +\
ylab('Cumulative Return') + ggtitle('S&P Sector Performance') 

enter image description here

6
  • Two thoughts: 1) the percent function is in the scales package; has that been loaded/attached/whatever the nomenclature for the equivalent to library("scales") from within Python is? 2) Try without quotes around percent; they are not necessary, but they may be allowed (don't recall offhand). Commented Apr 22, 2014 at 20:31
  • I knew it came from the 'scales' library but thought I had seen ex with % but it might have been R's ggplot2. I checked yhat's examples and realized they didn't show it so you're probably right in that the 'scales' library hasn't been ported yet. Python requires the the quotation marks. However, it is strange that most of the other scales formatting works including 'comma' and 'millions' etc.. Commented Apr 22, 2014 at 22:11
  • What happens if you replace 'percent' with percent_format()? Commented Apr 23, 2014 at 0:18
  • percent_format() is undefined and produces an error. Anything you pass through to scale_y_continuous(labels= 'percent') that is unrecognized appears to default to standard decimals. That includes misspelled 'commas', '%' etc. Commented Apr 23, 2014 at 22:12
  • Oh, I just realized that you are dealing with a port of ggplot2 into python, not a bridge between python and R. I'm afraid that I don't know enough (well, anything) about that to be of any real help. Sorry to send you down dead ends. Commented Apr 23, 2014 at 22:34

1 Answer 1

3

This is now available in version 0.5.3 (I just pushed this).

>>> from ggplot import *
>>> import numpy as np
>>> import pandas as pd
>>> df = pd.DataFrame({ "x": np.arange(0, 10), "y": np.arange(0, 1, 0.1) })
>>> ggplot(df, aes(x='x', y='y')) +\
... geom_point() +\
... scale_y_continuous(labels='percent')

ggplot scale_x_continuous percent

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.