Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

📣 Announcing Plotly.py 4.12: Horizontal and Vertical Lines and Rectangles

Update: version 4.13 was released since this was posted.

I’m happy to announce that Plotly.py 4.12 is now available for download via pip and conda! For up-to-date installation and upgrading instructions (including the extra required steps for JupyterLab!) please see our Getting Started documentation page and if you run into trouble, check out our Troubleshooting Guide. Note that we skipped an announcement for version 4.11 so we’re adding the news in this post.

:newspaper: What’s new in Plotly.py 4.12

Our changelog has details and links to individual pull requests, but here are the highlights:

:straight_ruler: Easy Labelled Horizontal and Vertical Lines and Rectangles

Plotly has always supported the ability to draw lines and polygons on figures, as well as position text on figures but drawing and labelling simple axis-aligned shapes that don’t move when you pan away from the initial view has always been tricky and verbose.

In 4.12 we’ve added new convenience methods to figures: .add_hline(), .add_vline(), .add_hrect() and .add_vrect(). These methods allow you to very easily add lines and rectangles to your figures, with optional automatically-positioned labels!

:triangular_ruler: Adding Line, Rectangles and Traces to Multiple Subplots at Once

The new methods above all automatically add shapes and text to all the subplots in a figure by default thanks to the new accepted value "all" for the row and col arguments (and the “all” behaviour can be overridden by manually setting row or col to a number as well!). This new value is also accepted by add_shape(), add_annotation() and even add_trace(), although the defaults there have not changed. This means you can now easily add a copy of the same trace to each subplot in a figure:

:mag: New xref and yref values for layout objects

As part of the work above, layout.shapes, layout.annotations and layout.images now all support a new xref/yref mode for positioning. In the past, only absolute "paper" or data "x" coordinates were accepted, which made sense in a single-subplot context, but correctly positioning elements on multiple subplots required a lot of manual bookkeeping. You can now position these elements using e.g. xref="x2 domain", in which case the value of x is interpreted such that 0 and 1 are mapped to layout.xaxis2.domain[0] and layout.xaxis2.domain[1]. This enables you to easily position shapes with respect to subplot bounds.

:newspaper: What was new in Plotly.py 4.11

Our changelog has details and links to individual pull requests, but here are the highlights:

:spiral_calendar: Period positioning on date axes

When plotting data on date axes, if your data coded “January 1” or “January 31” in fact refers to data collected throughout the month of January, for example, you can configure your traces to display their marks at the start end, or middle of the month with the xperiod and xperiodalignment attributes. In the example below, the raw data is all coded with an X value of the 10th of the month, but is binned into monthly periods with xperiod="M1" and then displayed at the start, middle and end of the period. This feature was anonymously sponsored, and we thank our sponsor on behalf of the community! :pray:

:heart: Powered by Plotly.js 1.57 and perfect for Dash 1.17

This release of Plotly.py inherits all of the improvements to the underlying Javascript library that powers it. The full Plotly.js 1.57 changelog contains more details about what changed under the hood.

The version of Plotly.js that Plotly.py 4.12 is built on is the same one that’s bundled with the just-released Dash 1.17 so we recommend that if you’re a Dash user you upgrade to both Dash 1.17 and Plotly.py 4.12 to get the full benefit of all of these libraries working together.

:package: Get it now!

To sum up: Plotly.py 4.12 is out and if you’re excited about any of the above features, head on over to our Getting Started documentation page for full installation instructions, and don’t forget to upgrade your JupyterLab extensions if that is your environment of choice!

:reminder_ribbon: In Case You Missed It: Previous 4.x Announcements

  • Plotly.py 4.10:
    • date -axis and px.timeline() improvements
    • Full Figures for Development
    • A Faster px.imshow()
  • Plotly.py 4.9:
    • Kaleido for static image export
    • Hexbin Tile Maps
    • Timelines
  • Plotly.py 4.8:
    • Plotly Express Support for Wide- and Mixed-Form Data
    • a Pandas backend
  • Plotly.py 4.7:
    • Major performance improvments
    • Shape-drawing
  • Plotly.py 4.6:
    • unified hover labels
    • excluding weekends from time-series axes
    • imshow labelling and xarray support
  • Plotly.py 4.5:
    • legend titles
    • GeoJSON choropleth improvements
    • a new sunburst/treemap path API
    • uniformtext
  • Plotly.py 4.4:
    • new Plotly Express functions for pie charts, sunbursts, treemaps, funnels maps,
  • Plotly.py 4.3:
    • px.imshow for heatmaps and full-color images
    • facet wrapping in Plotly Express,
  • Plotly.py 4.2:
    • Treemaps
    • Plotly Express input enhancements to work without data frames
    • texttemplate everywhere
  • Plotly.py 4.1:
    • Mapbox maps without Mapbox tokens
    • Choropleth and Density mapbox maps
    • Indicators (big numbers, gauges, bullet charts)
  • Plotly.py 4.0 :
    • offline-only
    • plotly.express-first
    • displayable anywhere
3 Likes

The horizontal and vertical shapes are a very nice feature!

For the rectangles, I wonder if it is possible to specify only one limit. E.g.: add_vrect(x1=2) would draw a vrect that binds to the left side of the plot and has 2 as the right-side limit.

Thanks!

That’s an interesting idea! At the moment this isn’t possible because either both x0 and x1 are specified in data coordinates or in plot fractions, but we can’t have them be specified in different units.

Excellent! Are the lines equipped with event functionality like hover and click?

No, these horizontal/vertical lines/rects do not support events like hover and click. For that you’d need to use a trace. More info on the distinction here: https://plotly.com/python/shapes/

1 Like