This module contains methods for producing graphs and publishing them on Amazon S3, or in the location of your choice.
It is written and maintained for Newsworthy, but could possibly come in handy for other people as well.
pip install newsworthycharts
This module comes with two classes, Chart and Storage (and it's subclasses). When using the Chart class, the generated chart will be saved as a local file:
from newsworthycharts import SerialChart as Chart c = Chart(600, 800) c.title = "Number of smiles per second" c.xlabel = "Time" c.ylabel = "Smiles" c.caption = "Source: Ministry of smiles." data_serie_1 = [("2008-01-01", 6.1), ("2009-01-01", 5.9), ("2010-01-01", 6.8)] c.data.append(data_serie_1) c.highlight = "2010-01-01" c.render("test", "png")
You can use one of the predefine chart classes to make common chart types. Or you can use Newsworthycharts together with Matplotlib. This is useful is you just want to add text elements such as subtitle, notes or apply a predefine theme.
Here is how you would make a pie chart:
# data labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' sizes = [15, 30, 45, 10] # setup chart chart = Chart(width=800, height=600, storage=local_storage) chart.title = "My pie chart" chart.subtitle = "Look at all those colors" # NB: Render the chart to `chart.ax` chart.ax.pie(sizes, labels=labels, autopct='%1.1f%%') # Save the chart chart.render("tailored_chart", "png")
You can use a _storage_ object to save file to a specific location or cloud service:
from newsworthycharts import Chart from newsworthycharts import S3Storage s3 = S3Storage("my_bucket") c = Chart(600, 800, storage=s3) c.title = "Number of smiles per second" c.subtitle = "This chart tells you something very important." c.xlabel = "Time" c.ylabel = "Smiles" c.note = "There are some missing smiles in data" c.caption = "Source: Ministry of smiles." c.render("test", "png")
To store a file in a local folder, use the LocalStorage class:
from newsworthycharts import LocalStorage storage = LocalStorage("/path/to/generated/charts")
Charts are styled using built-in or user-defined styles:
from newsworthycharts import Chart # This chart has the newsworthy default style c = Chart(600, 800, style="newsworthy") # Style can also be the path to a style file (absolute or relative to current working directory) c2 = Chart(600, 800, style="path/to/styles/mystyle.mplstyle")
To set up you own style, copy the build-in default: <https://github.com/jplusplus/newsworthycharts/blob/master/newsworthycharts/rc/newsworthy>
Newsworthycharts will look first among the predefined style files for the requested style, so if you have a custom style file in you working directory you need to give it a unique name not already in use.
- data: A list of datasets
- annotate_trend = True # Print out values at points on trendline?
- trendline =  # List of x positions, or data points
- labels =  # Optionally one label for each dataset
- annotations =  # Manually added annotations
- interval = None # yearly|quarterly|monthly|weekly|daily
- units = 'number' # number|percent|degrees
- show_ticks = True # toggle category names, dates, etc
- subtitle = None
- note = None
- xlabel = None
- ylabel = None
- caption = None
- highlight = A position (typically a date, category label or index) to highlight. The semantics may differ somewhat between chart types.
- decimals = None # None means automatically chose the best number
- logo = None # Path to image that will be embedded in the caption area. Can also be set though a style property
- color_fn = None # Custom coloring function
To run tests:
python3 -m flake8 python3 -m pytest
To deploy a new version to [PyPi](https://pypi.org/project/newsworthycharts):
- Update Changelog below.
- Update the version number in newsworthycharts/__init__.py
- Create and push a git tag: git tag VERSION; git push --tags (not strictly needed, but nice)
- Build: python3 setup.py sdist bdist_wheel
- Check: python3 -m twine check dist/newsworthycharts-X.Y.X*
- Upload: python3 -m twine upload dist/newsworthycharts-X.Y.X*
...assuming you have Twine installed (pip3 install twine) and configured.
- Adding more base maps
- Getting rid of custom settings-hack
- Custom month locator with equal-width month bars
- Patch upgrade Matplotlib to 3.8.1
- Treat 'jpeg' format as 'jpg'
- Fixed a rendering bug in stacked bar charts with multiple values being 0
- Pillow upgraded to 10.1
- Fixed bug in value_labels, trying to access a color value that didn't exist
- Dropped Python 3.7 support (upstream)
- Uses Matplotlib 3.8
- Uses Pillow 10
- Fixed date formatting issue in daily charts
- No longer render EPS files by default.
- _Really_ fix dependencies
- Fix error in dependency verison
- Added BubbleMap
- Added se-4 basemap for Swedish counties
- Basemaps can now have multiplygons
- Downgraded adjustText to 0.7.3, as upgrade broke rendering constistency in some places
- Revert matplotlib-label-lines to previous version.
- Dropped Python 3.7 support (upstream)
- Pinned Pillow to exact version in setup.py, for consistent rendering
- StripeChart: First draft
- ProgressChart: Better value label placement.
- ProgressChart: Enable custom value labels as third argument in data serie.
- Bug fix: Handle translation in inset maps in ChoroplethMap.
- Bug fix: Path to translation file in ChoroplethMap.
- ChoroplethMap: Allow Newsworthy region names.
- RangePlot: Re-add start_label that had been (mistakenly?) commented out.
- Bug fix: Fixes legend issue in ProgressChart.
- Data point annotation now works for serial charts as well
- Bug fix: Re-enable qualitative_colors as color argument in SerialChart (line).
- Support for rendering jpeg files, as jpg
- Minimum required Python version is now 3.7 (jumping from 3.5)
- Fix z-ordering issue on multiple series (n > 2)
- Fix tag mismatch in dist
- Add missing haversine transform for non-projected crs
- height can be set to None for automatic ratio, for chart types that support it. Will default to 1:1 for most chart types, but maps will try to provide a reasonable default based on geometry. Some chart types still require explicit height
- It is now possible to use subsets of basemaps, by specifying a prefix: se|03-7 means regions starting with 03 in se-7
- Added .missing_label to ChoroplethMap. If None (default), no label will be printed.
- Always accentuate base_line (/y=0), and make sure that line is on top of any bars to avoid “floating” bars
- Improved error handling in ChoroplethMap
- Clean up figure layout logic (this should speed up rendering somewhat)
- Increased default max_ticks in SerialChart to 7
- ChoroplethMap legend formatting, following language, decimals and units settings, etc
- Minor tweaks to the layout algorithm. Might affect padding in some charts.
- ChoroplethMap now does some basic normalizing of region codes
- Added some data sanity checks, and improved error messages in ChoroplethMap
- Added tests for ChoroplethMap
- Do not default to broken y axis if chart contains a bar series.
- Fix bug and occasional crash when using baseline with None values
- Fix crash in serialchart coloring chain
- Fix regression in SeasonalChart bar coloring
- Added grey outline to choropleth maps
- The type argument is now a list with one type per data serie. Using a string is still supported for backwards compability. This makes it possible to make mixed type charts.
- Reworked, simpler and more stable bar coloring algorithm
- The type argument is no longer a getter/setter
- Reduced edge for bar chartswith many bars
- Removed unused, undocumented special colors value "qualitative_colors". We have reasonable defaults for all chart types, that can already be overridden. The qualitative colors are used by default for qualitative data.
- Removed unused, undocumented support for highlighting a series by label, rather than a value. The first series is highlighted by default, and that behaviour can already be overriden by the .colors setting
- Add more space for label title on se-7 maps
- Don't try to render map insets with no data
- Use style colors in categorical choropleth maps
- Added missing support for coloring categorical maps with .colors
- Make automatic labeling work on categorical maps with .colors
- Somewhat lighter fill for missing values in choropleth maps (lightgray -> gainsboro)
- Testing experimental label_title support, to be documented in 1.44.0
- Fixed weird ymax in some baseline cases
- Added bottom padding when baseline was below data-min
- Fixed cut off-bug with negative baseline
- Fix coloring bug in warm_cold color_fn with baseline
- Fix regression with quarterly locator
- Default to weekdays on x-axis if data spans 7 days or less
- Added .color_labels to label bar colors set by .color_fn
- Added .baseline setting for bar charts
- warm_cold coloring algorithm now works relative .baseline
- Added .baseline_annotation
- .color_fn can now be a lambda function (or the name of one of the built in functions), e.g. chart.color_fn = lambda x: "red" if x < 1.4 else "green"
- Bar charts will now always have a small white edge
- Don't break y axis if data is close to 0
- Offset quarters will be recognosed as quarters now (e.g. Feb, May, Aug, Nov)
- Fixed bug in .allow_broken_y_axis implementation, causing y-axis to be broken in too many places
- Various dependency updates
- Replaced deprecated PIL.Image.ANTIALIAS with PIL.Image.Resampling.LANCZOS for logotype resizing.
- Get rid of warnings about missing “glyph 10” when prerendering text to calculate text bos sizes
- Fixed bug where single values surrounded my None's were not printed out in serial-data line charts. This was an earlier regression that was not noticed for many releases.
- New, experimental chart type: Choropleth maps! Supports both categorical and continuous data.
- Better support for monthly time series spanning years
- Fixed bug where missing annotation slots could crash CategoricalChart
- Don't crash on deprecation warning
- Matplotlib upgraded from 3.6.2 to 3.6.3
- Pin some critical requirement versions
- Fix floating point bug in percent labels
- Test fixes
- Auto-decide .decimals if None
- Round 0.5 to 1, etc in value axis labels and annotations (the ROUND_HALF_UP behaviour)
- Add .force_decimals to print out e.g. ”1.0”. Requires .decimals to be explicitly set
- Serial Chart: Allow disabling ”broken y axis” feature by setting allow_broken_y_axis=False
- Deprecated units="count". Make all numbers equal. Use units="number" and decimals=0 to get the earlier behaviour.
- Remove overriding of decimal settings by units = count
- Remove noisy deprecation warning on user settings in rc files
- Formatters will now use the correct minus signs for the given locale.
- Added missing metadata to svg
- Added .__version__ attribute to the package
- Added pdf export, now more widely used than eps
- Author and software metadata now added to pdf and png, including the exakt NWCharts version used to produce an image
- S3Storage: Handle text files.
- Prevent logo from ever being > 155px, to restore previous behaviour.
- Made multi series bar seasonal bar charts work for opposite signs, so that we can make +/- charts
- Bug fix: Don't crash with factor argument in DW charts.
- Fixed rendering bug in non-transparent eps exports with transparent logos
- Fixed bug in argument parsing in S3Storage.save()
- Added storage_options argument to render() and render_all()
- Unified function signatures across storage classes.
- Added options argument to S3Storage.save()
- Enable logo scaling. Provided logos can now be any size, and will be scaled down to an appopriate format.
- Adds factor argument to .render() and .render_all().
- Adds missing transparent argument to .render_all().
- Matplotlib @ 3.6.2
- langcodes @ 3.3 to ensure consistent handling of macro languages (no is a valid language)
- Adds transparent argument to render method.
- ScatterPlot: Mark labeled dots more clearly.
- SerialChart: Better error when timepoints are duplicated.
- Bug fixes: Handle negative values when ymin=0 in SerialChart and remove line stroke from highlighted_x_ranges.
- SerialChart: New options: line_width and highlighted_x_ranges.
- Added label_placement='outside' option to SerialChart
- Matplotlib updated from 3.3 to 3.6, including among many, many other things: - support for .webp - a lot of additions and improvements to rcParams - new backends
- Custom NWCharts parameters to the rc style file is being deprecated, and should eventually be phased out
- Matplotlib and related modules are now pinned to a specific version
- Added support for generating webp images!
- Upgraded pytest to support Python 3.10+
- Fixed date locators to use thecorrect langauge/locale
- Added padding on top of title, to avoid cropping diactritics
- CategoricalChart: Make it possible to hide legend.
- CategoricalChartWithReference: Handle multi color bars.
- Chart / SerialChart: New feature: Mark broken y axis with symbol.
- SerialChart: Force y axis range to to given values when ymax and ymin is defined.
- SerialChart: Enable value labeling of each point on line.
- Highlight only current value in SeasonalChart; use different shades of grey for the rest
- Add SeasonalChart, a.k.a the Olsson chart
- ProgressChart: Handle missing values
- lib.formatter.Formatter: Handle null values
- ScatterPlot: Enable ymin and xmin in scatterplot.
- Color annoation outline by background color.
- Improved ScatterPlot.
- Bug fix: Inline labeling on charts with missing data.
- CategoricalChartWithReference: Adds highlight option
- Adds missing dependency.
- SerialChart: Introduces inline labeling on lines
- Tweeks on line labeling
- SerialChart: Introduces labeling on lines (rather than just legends)
- Bug fix: Handle charts without ticks to be able to render pie charts again
- Beter height handling in header and footer.
- Make Noto Sans default font.
- Enable colors property in stacked bar SerialChart.
- Adjusts x margin in RangePlot to fit value labels better.
- Increases line spacing in subtitle.
- Bug fix: Small change in Datawrapper API.
- Make ticks option work with SerialChart.init_from
- New feature: Use base Chart class to make custom charts.
- Bug fix: Labels outside canvas in RangePlot
- ClimateCars: Tweeks on 2030 chart.
- Handle np.int as years.
- CategoricalChart: Highlight multiple values with list
- Bug fix: ylabel placed outside canvas
- Style: Align caption with note
- RangePlot: Better label margins and bold labels.
- RangePlot: Rename argument values_labels => value_labels.
- Pick up qualitative colors from style file.
- Fixed coloring on highlighted progress charts.
- Adds ability to highlight both ends on range plot.
- Added ticks option to SerialChart, to set custom x-axis ticks
- Added color option to CategoricalChart, to work exactly as in SerialChart
- Fixed bug with highlight in line charts where some line was outside the highlighted date.
- Enable multiple targets in progress chart.
- Fixes highlight bug in progress chart.
- Small changes in range plot.
- Adds CO2 budget chart
- ClimateCar chart tweeks.
- Bug fix: Adds newsworthycharts.custom to build.
- Introduces progress charts and removes hard coded font sizes.
- Introduces range plots and enables custom coloring in serial charts.
- Fit long ticks on y axis.
- Set annotation fontsize to same as ticks by default.
- Bug fix: Subtitle placement
- Introduces subtitle and note.
- Updates default styles to align with Newsworthy style guide.
- Fit footer by logo height. Fixes bug that caused axis overlag when logo was large.
- Introduces stacked categorical bar charts
- Bug fix: Remove failing attemt to store chart in dw format
- Corrects zorder and centers tick on CategoricalChartWithReference
- Introduces new chart: CategoricalChartWithReference
- Fixes bad X ticks in weekly SerialChart (and charts that don't start in January).
- Add annotation_rotation option to categorical charts
- Fix a crash in some special cases with serial charts shorter than a year.
- Fix a bug where diff between series was not highlighted if one value was close to zero.
- Include translations in build.
- Translates region to Datawrapper standard when making maps.
- Allows list of dicts to be passed to DatawrapperChart to be make tables, categorical maps etc.
- Require requests.
- Bug fixes.
- Introduces Datawrapper Chart type.
- Adds ymax argument (to SerialChart)
- Bug fix: Handle missing values in SerialChart with line.
- Bug fix: Set y max to stacked max in stacked bar chart.
- Introduces stacked bars to SerialChart.
- Fixes bar_orientation bug with init_from()
- Fix an ugly bug where type=line would not work with init_from()
- Some cosmetic changes: no legend if only one series, color updates, thinner zero line.
- Make title and units work with init_from again
- Add warm/cold color function
- Really, really make init_from work, by allowingly allowing allowed attributes
- Fix bug where init_from would sometime duplicate data.
- Make sure init_from does not overwrite class methods.
- Protect private properties from being overwritten by init_from
- When units is count, decimal should default to 0 if not provided. This sometimes didn't work. Now it does.
- Make init_from work as expected with a language argument
- Make init_from work as expected with multiple data series
- Added a factory method to create charts from a JSON-like Python object, like so: SerialChart.init_from(config, storage)
- Fix packaging error in 1.5.0
- Expose available chart engines in CHART_ENGINES constant for dynamic loading
- Add color_fn property, for coloring bars based on value
- Increase line width in default style
- Upgrading Numpy could potentially affect how infinity is treated in serial charts.
- Revert text adjusting for categorical charts, as it had issues
- Add new ScatterPlot chart class
- Improved text adjusting in serial charts
- More secure YAML file parsing
- Make small bar charts with very many bars look better
- Make labels work again, 1.3.1 broke those in some circumstances
- Make inner_max/min_x work with leading / trailing None values
- Make sure single, orphaned values are visible (as points) in line charts
- Allow (and recommend) using Matplotlib 3. This may affect how some charts are rendered.
- Removed undocumented and incomplete Latex support from caption.
- Don't highlight diff outside either series' extreme ends.
- Use strong color if there is nothing to highlight.
- Fix a bug where decimals setting was not used in all annotations. Potentially breaking in some implementations.
- Make the annotation offset 80% of the fontsize (used to be a hardcoded number of pixels)
- Small cosmetic update: Decrease offset of annotation.
- Require Matplotlib < 3, because we are still relying on some features that are deprecated there. Also, internal changes to Matplot lib may cause some charts to look different depending on version.
- Make annotation use default font size, as relative sizing didn't work here anyway
- Move class properties to method properties to make sure multiple Chart instances work as intended/documented. This will make tests run again.
- None values in bar charts are not annotated (trying to annotate None values used to result in a crash)
- More tests
- Annotations should now work as expected on series with missing data
- Fix bug where decimal setting wasn't always respected
- Make no decimals the default if unit is "count"
- First version