Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: plotting with x axis being vector of strings #40

Closed
cooldome opened this issue Feb 18, 2020 · 2 comments
Closed

Feature request: plotting with x axis being vector of strings #40

cooldome opened this issue Feb 18, 2020 · 2 comments

Comments

@cooldome
Copy link
Contributor

I variation of previous example with one change: tenors column (the X axis) is vector of strings.
If axis X is strings I expect graph to be equally spaced in X axis.

import ggplotnim
import random
import tables
import math

const paths = 20
const dates = 80

proc gaussian*(rnd: var Rand, mu = 0.0, sigma = 1.0): float =
  var
    s = 0.0
    u = 0.0
    v = 0.0
  while s >= 1.0 or s <= 0.0:
    u = 2.0 * rnd.rand(0.0..1.0) - 1.0
    v = 2.0 * rnd.rand(0.0..1.0) - 1.0
    s = (u * u) + (v * v)

  let x = u * sqrt(-2.0 * ln(s) / s)
  return (mu + (sigma * x))

proc createDataFrame(): DataFrame = 
  const sigma = 0.10

  var rnd = initRand(124325)
  var data: OrderedTable[string, seq[Value]]
  var simPath = newSeq[Value](dates)
  for j in 0..<paths:
    simPath[0] = %~  100.0
    for i in 1..<dates:
      simPath[i] = %~(simPath[i-1].toFloat() * exp(-0.5 * sigma^2 + sigma * gaussian(rnd)))
    data["path" & $(j + 1)] = simPath

  var tenors = newSeq[Value](dates)
  for i in 0..<dates:
    tenors[i] = %~ ("t" & $i)
  data["tenors"] = tenors
  toDf(data)

let df = createDataFrame()
var g = ggplot(df)
for i in 1..paths:
  g = g + geom_line(aes(y = "path" & $i, x = "tenors"))
g + ggsave("test1.pdf")
@Vindaar
Copy link
Owner

Vindaar commented Feb 18, 2020

Hm, having a discrete X axis should actually work just fine. Discrete y axes are actually a missing feature indeed.

In this case you may be stumbling over the second issue I mention here: #38

As far as I can tell this is a regression. I was working on the above yesterday already. I'll see if that fixes your example too.

@Vindaar
Copy link
Owner

Vindaar commented Feb 22, 2020

Once #45 is merged, let me know if it works as you expect!

edit: I'll close this issue. Feel free to reopen!

edit2: for an example now, see: https://github.com/Vindaar/ggplotnim/blob/master/recipes.org#using-a-discrete-x-scale-with-lines

@Vindaar Vindaar closed this as completed Feb 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants