Scripts that plot 'generalized' data.
NOTE: This repository is just for scripts that have a very broad ability to make plots from 'generalized' data. I have many other scripts/Jupyter notebooks that are more nuanced, and hence not in this repo. A few of them are referenced below under the section Plotting scripts for specific purposes
.
Descriptions of the scripts are below:
A Python script to plot panels of bar graphs with optional curves fit to the data. It uses lists of lists of x & y data. Each set of x & y values is plotted to a subplot stacked upon each other subplot in the panel.
This script is my 'hacky' recasting of the histogram-generating (actually kernel density estimate curve plot-generating) code of joypy as a less automated, fully-customizable plotter of panels of bar graphs with curves fit to the data. (There might be some more current, more convenient versions than this among the resources I found listed here.) (A fancy, R-based version with vertical color gradient fill is illustrated in this vignette for devoutsvg.) (Also reminscent of ridgline plots done in R by ggridges, see here for an example. Vaguely also reminiscent of Seaborn's example of Overlapping densities (‘ridge plot’) (in fact title has been updated from 'Overlapping densities (‘joy plot’)'), and horizontal violin plots that show observations with a 'stick', but where one-sided and set up so can compare more than one. Also vaguely reminiscent of small-multipes charts. Also vaguely reminiscent of raincloud plots and beanplots (sina plots too?) illustrated and discussed here for R and beyond (see Raincloud Plots blog post here with updates here and with associated paper announced here) (and maybe made here in R a different(?) way); however, Davide Poggiali has successfully brought raincloud plot to Python/Seaborn so that it is available in several languages with a nice preprint (before the final paper in Wellcome Open Research; see linl above) and set of tutorials. (Stacked violin plots for seurat and scanpy remind me of what you'd need if correlating two category intersections, see here and links to scanpy function therein.) Note that sina plots and raincloud plots are more favored recently than perhaps violoin and box plot, see here for example.
Originally written to be part of a mini-pipeline, shepherds_read_starts_at_start_of_origins_thru_to_plotting.py
, where I plot read starts at the start of origin promoters mined by plot_coverage_and_starts.py
Thus, it was written in a manner to make the core function easily imported elsewhere so that the "data" hard coded in the example can be replaced by real data.
Stylistically, it seems others have called this a 'panel of bar charts', see here and so my description/name is an effort to acknowledge that.
EXAMPLE OUTPUT THAT CAN BE MADE USING THIS SCRIPT:
Two ways to use this script:
-
Edit it to contain your data.
Edit the script directly via text editor. Assign your data and asign items like
labels
,colors
, etc.Adjust the output image to be in the format you'd like. You may find
.png
best while developing because more easily directly viewed thansvg
;svg
vector graphics format is more easily sized while maintaining quality and thus better suited for adapting for publication.Execute the edited script with:
python plot_panel_bar_plots_with_fit.py
The script can also simply be pasted into a cell in a Jupyter notebook and then executed as you would any code in a Jupyter notebook, i.e., type
shift-enter
or pressplay
button. An example of an edited script pasted into a Jupyter notebook is included here in this repo; scroll all the way to the bottom to see ouput. Go here to see the example Jupyter notebook rendered nicely.The resulting image produced in that notebook is below:
-
(Recommended way ==>) Import the function into your own script with your data or your Jupyter notebook with your data.
To do this, place the script
plot_panel_bar_plots_with_fit.py
into a directory with your code or place the script in your Jupyter environment on the same level where the notebook is located.Minimal code outline needed for that approach:
import matplotlib from matplotlib import pyplot as plt import plot_panel_bar_plots_with_fit as plt_panel_bar #<--IMPORTS IF `plot_panel_bar_plots_with_fit.py` PLACED IN SAME DIRECTORY # assign your data # assign items like `labels`, `colors`, etc. ## MAKE PLOT by calling function #### fig, axes = plt_panel_bar.plot_data( data, #plus ettings for options, examples: labels=my_data_labels, colors=my_label_and_bar_colors, bar_alphas=my_bar_alphas, label_alphas=my_label_alphas, shared_x_label=shared_x_label, shared_y_label=shared_y_label # Several other options are available for fully customizing output. ) ### END OF PLOT MAKING ##### # save or display returned plot in preferend format plt.savefig("plot.svg", orientation='landscape') # FOR VECTOR GRAPHICS; useful if merging into Adobe Illustrator. plt.savefig("plot.png") # may be best for development phase as viewable directly in more environments plt.show() # <=== Use that when placed in a Jupyter notebook
See
shepherds_read_starts_at_start_of_origins_thru_to_plotting.py
for a full example implementing theimport
approach.
The resulting images produced are below in the two color options, respectively, with the second option selectable by editing the script shepherds_read_starts_at_start_of_origins_thru_to_plotting.py
further to comment off the first option:
KNOWN ISSUES AND WORKAROUNDS:
I wanted to make plots where the x-axis was -70 through -1 and this script didn't seem to work and just output a tiny square as the plot. No clear error was reported although there was a warning:
UserWarning: Attempting to set identical left==right results
in singular transformations; automatically expanding.
left=0, right=0
'left=%s, right=%s') % (left, right))
Turns out the warning and the "tiny square" plot are because the script tries to be smart (in a less than optimal way, for now) and set the x_lower_val
to zero for sets less than 100 in size. However, if using strictly negative numbers for x-axis that causes a problem presently because the original implementation of handling that didn't take into account the entire range of x-axis values being negative values. The easy solution is just to supply x_lower_val
in your plot_data()
function call which overrides the script trying to set the values itself.
This belongs under this topic but because I want to binderize the script and the demonstrations, I put it as a separate repo here.
Typical input example (red annotation is just for illustrating concepts):
Typical output examples:
See here for more examples and information.
I have made many other scripts that plot data that aren't in this repository becauase they are for specific purposes or deal with specific forms of data.
Below are links to a few of these to aid in organization:
- plot_expression_across_chromosomes
- plot_read_data
- scripts to make Venn diagrams for overlapping words or gene data
- Jupyter notebook that plots optical density vs. yeast cell density in addition to making related calculations
- Several generalized scripts for making donut plots that include subgroups from Pandas dataframes
- preliminary Peter de Jong attractor plotting in a Jupyter notebook
- modernized version of my '3Dscatter_plot-binder' repo of 6947 fers 3D scatterplots in Matplotlib and Plotly and versions with ipywidgets to interact with them both in examples of notebooks and Voila apps.
- logan_results_analysis-binder has several examples of making barplots with nice features.
RELATED:
I have a VPython-jupyter repo where I added the plotting package Seaborn
.
The scripts in this repository are licensed under MIT.
- Use Pylustrator without installing anything to see how you hand edit Matplotli results in a GUI to then get the Python code that implements those changes
- modernized version of my '3Dscatter_plot-binder' repo offers 3D scatterplots in Matplotlib and Plotly and versions with ipywidgets to interact with them both in examples of notebooks and Voila apps.
- logan_results_analysis-binder has several examples of making barplots with nice features.
Came across these and noted here since may be useful later and not in the usual packages I am familiary with:
- pywaffle. Rectangular-like pie chat-like plot. See here for impetus. (Reminiscent of heatmaps but don't have to be full box or rectangle.)
- small-multipes charts from scratch in Matplotlib - related to Seaborns replplot / Small multiple time series.