Code Monkey home page Code Monkey logo

less's Introduction

less

The beautiful graphing library

why?

Matplotlib is an amazing library. It enables almost any kind of graphing you could possibly want. Lately, though, I've found myself wanting two things:

  • Easy minimal plots without a lot of code required to turn various default chartjunk elements off
  • A slightly more intuitive / uniform interface to the various elements. I've only got so much time, and I'd rather not spend it in the docs seeing if I should use .set_xlim() or .set_bounds(), .tick_params or .set_ticks_position().

less is my attempt to address both of those personal pain points. Let's look at an example of how things work out of the box, Matplotlib vs less. First, we'll create some data points:

np.random.seed(42)

x = np.linspace(0, 2*np.pi, 50)
y = np.sin(x)

y2 = y + 0.1 * np.random.normal(size=x.shape)
mask = np.abs(y-y2) > 0.18

i = [i for i,e in enumerate(mask) if e][-1]
y2[i] += 2*(y[i]-y2[i])

typical_x,typical_y = x[mask],y2[mask]
outlier_x,outlier_y = x[mask == False],y2[mask == False]

Now we'll plot it with Matplotlib's default settings:

plt.figure(figsize=(9,6))

plt.plot(x, y)
plt.scatter(typical_x, typical_y)
plt.scatter(outlier_x, outlier_y)

plt.show()

img

And plotting it with less's default settings:

chart = less.Chart(9,6)

chart.plot(x, y)

chart.scatter(typical_x, typical_y)
chart.scatter(outlier_x, outlier_y)

chart.render()

img

Now obviously the second graph isn't very informative as is. No axes, no labels, no color. But the philosophy of less is that every element in a graph should be there for a specific reason. To that end, its design requires you to add every single element by hand. Let's see a usable graph made with less:

# create chart
chart = less.Chart(9,6) # args set size

# draw data elements
chart.plot(x, y) # defaults to dashed line
chart.scatter(typical_x, typical_y, style='highlight') # color
chart.scatter(outlier_x, outlier_y, style='background') # grey

# creating and styling left axis
chart.spine.left.visible(True)
chart.spine.left.ticks.major([-1,0,1])
chart.spine.left.ticks.minor(outlier_y)

# creating and styling right axis
chart.spine.right.visible(True)
chart.spine.right.ticks.major([-1,0,1])
chart.spine.right.ticks.minor(outlier_y)

# creating ticks on bottom axis
chart.spine.bottom.ticks.major([0, np.pi, 2*np.pi],
                               labels=['0', '$\pi$', '2$\pi$'])
chart.spine.bottom.ticks.minor(outlier_x)

# setting extent of chart area
chart.xlim(-0.1, 2*np.pi+0.1)
chart.ylim(-1.25,1.25)

# similar to plt.show()
chart.render()

img

  • We specify some of the points to be highlighted, and less uses its default color schemes to draw attention to only those points.
  • Scatter plot data points are by default drawn smaller than Matplotlib's defaults, for a cleaner style.
  • We manually make the left and right axes visible. Everything else stays undrawn by default.
  • Setting our tick locations automatically limits the axis to being drawn only between those locations.
  • When we're drawing the bottom ticks, we specify an optional labels argument.

Every visible element is there as a conscious choice, and I would argue the end result is both more beautiful and more informative as a result.

todo

  • Large pretty text
  • Slopegraph
  • Waterfall
  • Vertical bar
  • Horizontal bar
  • Stacked vertical bar
  • Stacked horizontal bar
  • Square area
  • Heatmap
  • Table?
  • Have easy settings (themes?) for scaling for projectors, Jupyter, etc
  • Support drawing text in the middle of lines

less's People

Contributors

coreygirard avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.