GithubHelp home page GithubHelp logo

matplotlib / mpl-altair Goto Github PK

View Code? Open in Web Editor NEW
13.0 9.0 8.0 1.59 MB

Matplotlib Backend for Altair Visualization Library

Home Page: https://matplotlib.org/mpl-altair/

License: Other

Python 100.00%

mpl-altair's Introduction

Join the chat at https://gitter.im/matplotlib/mpl-altair Powered by http://www.numfocus.org

Matplotlib Backend for Altair Visualization Library

Altair is a declarative statistical visualization library for Python, based on the Vega and Vega-Lite visualization grammars. This project converts Altair charts to Matplotlib objects that can be modified using the Matplotlib API, rendered as publication-qualuty figures, and integrated with Matplotlib based tools, libraries, and dashboards.

Contributing

Want to add docs? Features? Found a bug? Awesome!

Our style guide is matplotlib's and we use pytest for testing. Chat with us on gitter

Acknowledgments

Initial work on this project was supported by the John Hunter Matplotlib Summer Fellowship.

mpl-altair's People

Contributors

gitter-badger avatar kdorr avatar palnabarun avatar story645 avatar tacaswell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mpl-altair's Issues

Add binning support

mpl-altair currently doesn't support conversion for Altair charts that use binning.

Code for reproduction

from vega_datasets import data
seattle = data.seattle_weather()
seattle.head()

Binning color in a scatter plot:

chart = alt.Chart(seattle).mark_point().encode(
    alt.X('wind'), alt.Y('temp_max'), alt.Color('temp_min', bin=alt.Bin(step=10))
)
mplaltair.convert(chart)

Binning in a bar chart:

chart = alt.Chart(seattle).mark_bar().encode(
    alt.X('wind', bin=True), alt.Y('count()')
)
mplaltair.convert(chart)

Actual outcome

In a chart that would work without binning, binning fails silently and the chart is converted as if binning is always false.

Binning color in a scatter plot:

bin_mplaltair

Binning in a bar chart:

raises NotImplementedError until bar charts and aggregates are working.

Expected outcome
Converting a scatter plot with binned color should produce a chart that looks something like this:

bin-altair

Converting a binned bar chart should produce a chart that looks something like this:

bin_altair_bar

Possible solution and notes

  • Manipulate the data to be binned in parse_chart.ChannelMetadata().
  • Binning is probably better to add when bar charts and aggregate functions are functional in mplaltair.
  • Be aware that binning can happen in the encoding definition or in a transform.

For more info

Distinguish between mark_point, mark_circle, and mark_square

mpl-altair does not distinguish between mark_point(), mark_circle(), and mark_square(). All three marks produce a scatter plot.

Implementation Notes

  • Marks should be handled in _marks.py
  • mark_point() should draw open circles by default
  • mark_circle() should draw closed circles by default
  • mark_square() should draw squares by default

For more information

Categorical scatter plot

mpl-altair currently doesn't convert Altair charts that have categorical color, stroke, shape, or size encodings.

Code for reproduction

df = pd.DataFrame({
    'a': [1, 2, 3, 4], 'b': [7, 6, 5, 4], 'c': ['a', 'b', 'b', 'c']
})

chart = alt.Chart(df).mark_point().encode(
    alt.X('a'), alt.Y('b'), alt.Color('c:N')
)
mplaltair.convert(chart)

Actual outcome

A NotImplementedError is raised.

Expected outcome

Something that looks kind of like this:

scatter_altair

Possible solutions

  • Fix this on the Matplotlib side of things
  • Incorporate a workaround similar to how mplaltair._marks._handle_line() deals with colors in line plots

Add timeUnit support

mpl-altair currently doesn't support conversion for Altair charts that use timeUnit aggregations.

Code for reproduction

This and other timeUnit transformations don't work:

from vega_datasets import data
seattle = data.seattle_weather()

chart = alt.Chart(seattle).mark_point().encode(
    alt.X('month(date)'), alt.Y('temp_max')
)
mplaltair.convert(chart)

Actual outcome

A NotImplementedError is raised for anything that has a timeUnit.

Expected outcome

Any plot with a timeUnit transformation should work.

This plot should look something like this:

timeunit_altair

Possible solution and notes

  • Manipulate the data in parse_chart.ChannelMetadata() so that the data reflects the timeUnit aggregation. (see parse_chart.ChannelMetadata._handle_timeUnit().)
  • Also note that timeUnit transformations can also be applied separately from timeUnit transformations in the encoding channel.

Build is failing

While working on the other PR, I saw that the master's Travis build is failing. On further investigations, I found that they were failing due to change in how Altair interpreted inline data. The earlier behavior was to keep Inline Data as it is, but now they convert it into Named Data. This was breaking our tests as we did not have support for that.

Ref:
Altair Changelog
Failing Travis Builds

Add aggregate support

mpl-altair currently doesn't support conversion for Altair charts that use aggregations.

Code for reproduction

data:

df = pd.DataFrame({
    'a': [1, 1, 2, 3],
    'b': [1.1, 2.2, 3.3, 4.4]
})

Example 1:

chart = alt.Chart(df).mark_point().encode(
    alt.X('a'), alt.Y('average(b)')
)
mplaltair.convert(chart)

Example 2:

chart = alt.Chart(df).mark_point().encode(
    alt.X('a'), alt.Y('count()')
)
mplaltair.convert(chart)

Actual outcome

A NotImplementedError is raised for anything that has aggregated data.

Expected outcome

Any plot with aggregation should work.

The first chart should look something like this:

aggregates_altair

The second chart should look something like this:

aggregates_altair_count

Possible solution

During the initial altair chart parsing, manipulate the data to include the aggregations. See parse_chart.ChannelMetadata() (specifically _aggregate_channel()).

Add bar charts

Bar charts aren't supported yet in mpl-altair.

Implementation Notes

  • Marks should be handled in _marks.py
  • Plotting different data types in a bar plot gives different types of bar charts in Altair (vertical, horizontal, skinny bars, fat bars).
  • Plotting a categorical y against a categorical x gives a heatmap-like chart in Altair.
  • Adding a color encoding does different things depending on the data type. Sometimes the chart gets stacked, sometimes it doesn't.

For more information

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.