[ANN] clj-xchart – A charting/plotting library for Clojure

929 views
Skip to first unread message

Jean Niklas L'orange

unread,
Oct 15, 2016, 8:31:17 AM10/15/16
to Clojure
Hi Clojurians,

I am happy to announce clj-xchart! XChart is a lightweight charting
library for Java. clj-xchart wraps this library and tries to be a
succinct yet evident charting library for Clojure. The library can
emit the following chart types:

- Line charts
- Scatter charts
- Area charts
- Bar charts
- Histogram charts
- Pie charts
- Donut charts
- Bubble charts
- Stick charts

It also provides the following useful features:

- Easy to compare, view and make charts from a REPL
- Logarithmic axes
- Number, Date and Category X-Axis
- Export to png, gif, jpg, svg, pdf and eps
- Extensive customisation

Note that clj-xchart is a Clojure only library; if you need
interactive or animated charts in a web browser, then this library
will not help you with that. However, if you need png/jpg/svg/pdfs of
charts, then this may be a viable option.

To see a couple of example charts, along with the code required to
generate them, head over to the examples page.

The tutorial for the current release should give you a good
introduction in how to use the library, and the render options page
page has additional information about how to style the charts. The
majority of all commits and work has been related to examples and
documentation, and I hope this will make the library easy to use.

The source code is over at https://github.com/hyPiRion/clj-xchart

Suggestions and contributions are welcome!

-- Jean Niklas

Alan Thompson

unread,
Oct 16, 2016, 7:55:36 PM10/16/16
to clo...@googlegroups.com
Looks nice - I'll be keeping it in mind.
Alan

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mars0i

unread,
Oct 17, 2016, 1:54:41 AM10/17/16
to Clojure
Looks very nice! Thanks Jean Niklas.  I've been using Incanter for charts, which has been fine so far for my needs, but clj-xchart looks like it will make it easier to make nicer charts, and it would avoid loading much of Incanter when you don't need some of the other things Incanter provides.  (I also use nvd3 to generate charts in a browser with Clojurescript.)

Since you've developed a charting library, maybe I'll mention a feature that I have wanted (I think!):

I've been making plots with a large number of points--100K, sometimes even 1M or 2M per data sequence.  Sometimes I will sample a larger sequence every 10 or 100 steps to reduce the burden on the Incanter or nvd3 plotting function, but sometimes I want to see what the data looks like with all of the points.

I generate the data in a lazy sequence, using iterate, where, let's say, each element of the sequence is a map containing several pieces  of y values for the x value corresponding to that element of the sequence, e.g.

data = ({:a y-a-1, :b y-b-1, :c y-c-1}, {:a y-a-2, :b y-b-2, :c y-c-2}, ...)

In order to plot all three sequences of y values in Incanter or nvd3 (and clj-xchart?), I have to extract a new sequence of values for each data series, e.g. like this:

(map :a data)
(map :b data)
(map :c data)

and I have to generate several sequences of x values by calling (range) repeatedly.  I pass these six lazy sequences to the chart function, but at least in Incanter and nvd3, I don't believe Incanter does anything until it realizes all of the sequences.  That means that it realizes six distinct sequences, I think, and my initial sequence of maps will have been realized as well. 

But if I'm plotting several sequences of y values that are embedded in a sequence of maps or vectors, each with several y values for the same x, I wonder if it could be more to efficient pass the entire complex sequence to the plotting function at once, and only provide one set of x values if all of the y values will share the same x's.  If the plotting function extracts the y values as it reads through the sequence of maps/vectors, and needs only one sequence of x's, then only two sequences are realized.

Maybe this is an unusual need, at present, but as Clojure is used for more scientific applications, it might become more common.


Jean Niklas L'orange

unread,
Oct 17, 2016, 4:37:51 PM10/17/16
to clo...@googlegroups.com
Hi Marshall,

On 17 October 2016 at 07:54, Mars0i <mars...@logical.net> wrote:
Looks very nice! Thanks Jean Niklas.  I've been using Incanter for charts, which has been fine so far for my needs, but clj-xchart looks like it will make it easier to make nicer charts, and it would avoid loading much of Incanter when you don't need some of the other things Incanter provides.  (I also use nvd3 to generate charts in a browser with Clojurescript.)

Thanks! This was also parts of the rationale for clj-xchart as well: Incanter is great, but it feels a bit strange to drag in both json and csv dependencies if you only need to plot some charts.
 
Since you've developed a charting library, maybe I'll mention a feature that I have wanted (I think!):

I've been making plots with a large number of points--100K, sometimes even 1M or 2M per data sequence.  Sometimes I will sample a larger sequence every 10 or 100 steps to reduce the burden on the Incanter or nvd3 plotting function, but sometimes I want to see what the data looks like with all of the points.

I generate the data in a lazy sequence, using iterate, where, let's say, each element of the sequence is a map containing several pieces  of y values for the x value corresponding to that element of the sequence, e.g.

data = ({:a y-a-1, :b y-b-1, :c y-c-1}, {:a y-a-2, :b y-b-2, :c y-c-2}, ...)

In order to plot all three sequences of y values in Incanter or nvd3 (and clj-xchart?), I have to extract a new sequence of values for each data series, e.g. like this:

(map :a data)
(map :b data)
(map :c data)

and I have to generate several sequences of x values by calling (range) repeatedly.  I pass these six lazy sequences to the chart function, but at least in Incanter and nvd3, I don't believe Incanter does anything until it realizes all of the sequences.  That means that it realizes six distinct sequences, I think, and my initial sequence of maps will have been realized as well. 

But if I'm plotting several sequences of y values that are embedded in a sequence of maps or vectors, each with several y values for the same x, I wonder if it could be more to efficient pass the entire complex sequence to the plotting function at once, and only provide one set of x values if all of the y values will share the same x's.  If the plotting function extracts the y values as it reads through the sequence of maps/vectors, and needs only one sequence of x's, then only two sequences are realized.

Maybe this is an unusual need, at present, but as Clojure is used for more scientific applications, it might become more common.

XChart (and consequently clj-xchart) can take the same x-axis as input. So you can reuse the same x values instead of creating 3 distinct but identical ones:

(let [x [1 2 3]
      y1 [1 2 3]
      y2 [2 4 6]
      y3 [3 6 9]]
  (c/xy-chart {"y1" {:x x :y y1}
               "y2" {:x x :y y2}
               "y3" {:x x :y y3}}))

I don't think this is unique to clj-xchart though, the same should apply to Incanter/JFreeChart.

Unfortunately, clj-xchart will indirectly realise almost all lists it is given, as it calls .size() on them to ensure that the X/Y/error lists are identical in size. It will also walk the lists to find the size/scale of the chart to plot. I'm not sure if there's a way around that, except perhaps if one pin the boundaries of the plot axes.

That being said, it doesn't seem like a bad idea to provide some sort of efficient view over data to avoid creating a new list that will be realised with the exact same data in another list. I made an issue on this, it shouldn't be too hard to implement either.



For what it's worth, I've had the same "issue" with large datasets as well (10-20M elements). In my case there isn't that much interesting to look at except the occational outlier, or if you have values which differ extremely from one datapoint to another.

What I tend to do is rebin/shrink the data set, typically by computing the average/max value of partitions (after filtering away outliers), depending on what I need to plot. I have a small section in the "Gotchas" section named Many Datapoints (bottom of the page) which has a couple of lines on how one can do that. I haven't found a good generic interface for it yet, so it's not provided by clj-xchart as of now.

--
Regards,
Jean Niklas L'orange

Mars0i

unread,
Oct 17, 2016, 10:33:34 PM10/17/16
to Clojure


On Monday, October 17, 2016 at 3:37:51 PM UTC-5, Jean Niklas L'orange wrote:
Hi Marshall,

On 17 October 2016 at 07:54, Mars0i <mars...@logical.net> wrote:
Looks very nice! Thanks Jean Niklas.  I've been using Incanter for charts, which has been fine so far for my needs, but clj-xchart looks like it will make it easier to make nicer charts, and it would avoid loading much of Incanter when you don't need some of the other things Incanter provides.  (I also use nvd3 to generate charts in a browser with Clojurescript.)

Thanks! This was also parts of the rationale for clj-xchart as well: Incanter is great, but it feels a bit strange to drag in both json and csv dependencies if you only need to plot some charts.
 
But if I'm plotting several sequences of y values that are embedded in a sequence of maps or vectors, each with several y values for the same x, I wonder if it could be more to efficient pass the entire complex sequence to the plotting function at once, and only provide one set of x values if all of the y values will share the same x's.  If the plotting function extracts the y values as it reads through the sequence of maps/vectors, and needs only one sequence of x's, then only two sequences are realized.


Maybe this is an unusual need, at present, but as Clojure is used for more scientific applications, it might become more common.

XChart (and consequently clj-xchart) can take the same x-axis as input. So you can reuse the same x values instead of creating 3 distinct but identical ones:

(let [x [1 2 3]
      y1 [1 2 3]
      y2 [2 4 6]
      y3 [3 6 9]]
  (c/xy-chart {"y1" {:x x :y y1}
               "y2" {:x x :y y2}
               "y3" {:x x :y y3}}))

I don't think this is unique to clj-xchart though, the same should apply to Incanter/JFreeChart.

Right.  I don't know why I didn't think of that.  Good idea.

Unfortunately, clj-xchart will indirectly realise almost all lists it is given, as it calls .size() on them to ensure that the X/Y/error lists are identical in size. It will also walk the lists to find the size/scale of the chart to plot. I'm not sure if there's a way around that, except perhaps if one pin the boundaries of the plot axes.

That being said, it doesn't seem like a bad idea to provide some sort of efficient view over data to avoid creating a new list that will be realised with the exact same data in another list. I made an issue on this, it shouldn't be too hard to implement either.

Great.
For what it's worth, I've had the same "issue" with large datasets as well (10-20M elements). In my case there isn't that much interesting to look at except the occational outlier, or if you have values which differ extremely from one datapoint to another.

What I tend to do is rebin/shrink the data set, typically by computing the average/max value of partitions (after filtering away outliers), depending on what I need to plot. I have a small section in the "Gotchas" section named Many Datapoints (bottom of the page) which has a couple of lines on how one can do that. I haven't found a good generic interface for it yet, so it's not provided by clj-xchart as of now.

Yes, good idea for some situations.  For my present project, it's OK to simply use every 10th or 100th point, even when the data is wildly random, but sometimes I want to see them all.   Other data elements are roughly continuous, so it's no problem to sample every nth point.

Joachim De Beule

unread,
Oct 18, 2016, 3:10:59 AM10/18/16
to Clojure
Hi Marshall,

You might want to have a look at https://github.com/aphyr/gnuplot

From the repo: "Datasets are streamed as sequences directly to gnuplot's stdin, so there's no temporary files to worry about. Yep, lazy sequences are streamed lazily. My laptop can plot about 10 million points in about a minute, and most of the CPU time is spent inside gnuplot in that test, so I'm reasonably happy."

Cheers,
Joachim.

Op maandag 17 oktober 2016 07:54:41 UTC+2 schreef Mars0i:
Reply all
Reply to author
Forward
0 new messages