### Merge branch 'plotting' of git.embl.de:grp-bio-it-workshops/intermediate-python into plotting

parents b11f21c6 1a30f390
 ... ... @@ -30,7 +30,7 @@ can summarize an entire work of several pages in a few square centimeters. > *Where there is great power there is great responsibility* - Winston Churchill As much as an image can be used for information, it can be used for misinformation. As much as an image can be used for information, it [can be used for misinformation][wikipedia-misleading]. Automatic behavior, formatting mistakes or deliberate manipulation, can lead to misleading messages by displaying data in erroneous or unclear ways. ... ... @@ -526,8 +526,7 @@ Given plot(X, Y) it then takes a pair of coordinates from both variables as wi > > > ## Solution > > > > Using only 4 samples the curve doesn't look as smooth as before. x = np.linspace(0, 2, 4) > > Using samples = 4 the curve doesn't look as smooth as before: > > ![simple-plot with few datapoints](../fig/my-simple-plot-jagged-curve.png) > > > > And a sine plot would look like: ... ... @@ -549,12 +548,22 @@ x = np.linspace(0, 2, 4) > > Notice also that as the values of X become larger, > > the number of samples is not enough > > and visual artifacts start to be noticeable in the sine-cubic plot. > > > > Using samples = 1000 makes most of the artifacts disappear: > > ![simple-sine plot without artifacts](../fig/my-sine-plot-more-samples.png) > {: .solution } {: .challenge } ### Histograms Another very common type of plot is the histogram which can be produced using the [plt.hist() function][matplotlib-hist]. A histogram is a bar-plot where one axis represents the range of values in the data being plotted, and the other axis, a count of the values in the interval defined by the bar. The width of the bars is determined by the bins attribute which specifies how many bars should be displayed. ~~~ import numpy as np import matplotlib.pyplot as plt ... ... @@ -569,10 +578,12 @@ normal_dist = np.random.normal(mean, stdev, samples) # Create a histogram with the sampled values plt.hist(normal_dist, bins) # Add a dotted thin line grid on both axis plt.grid(linestyle="dotted", linewidth=1) # Provide a descriptive title plt.title(f"Normal distribution - mean={mean}, stdev={stdev}, samples={samples}, binning={1/bins}") plt.title(f"$X \\sim \\mathcal{N}(\\mu,\\,\\sigma^{2})$ - $\\mu$={mean}, $\\sigma^{2}$={stdev}, samples={samples}, binning={1/bins}") ~~~ {: .language-python } ... ... @@ -589,7 +600,7 @@ plt.title(f"$X \\sim \\mathcal{N}(\\mu,\\,\\sigma^{2})$ - $\\mu$={mean}, $\\sigm > to have a nicely typeset μ character in the title. > Doing full stylization with LaTeX we could use: > ~~~ > plt.title(f"$X \\sim \\mathcal{N}(\\mu,\\,\\sigma^{2})$-$\\mu$={mean},$\\sigma^{2}$={stdev}, samples={samples}, binning={1/bins}") > plt.title(f"$X \\sim \\mathcal{{N}}(\\mu,\\,\\sigma^{{2}})$-$\\mu$={mean},$\\sigma^{{2}}$={stdev}, samples={samples}, binning={1/bins}") > ~~~ > {: .language-python } > ... ... @@ -604,10 +615,42 @@ plt.title(f"$X \\sim \\mathcal{N}(\\mu,\\,\\sigma^{2})$-$\\mu$={mean},$\\sigm > by prefixing it with r, becoming r"\alpha". {: .callout } ## Scatter > ## A Dense Histogram > > Exploring the [documentation of plt.hist()][matplotlib-hist], find how to add > a *probability density* projection of the plot above. > > When plotting as density, the values in the Y axis change. > Is this representation easier to understand than the default histogram with counts? > What if in addition the histogram is made cumulative? > > > ## Solution > > > > The plt.hist() function accepts a density=True and a cumulative=True option. > > Although the Y axis values change, the bars should have the same visual representation > > (unless a new random sample was generated). > > > > A density plot transforms the Y scale such that the area under the histogram > > adds to 1. > > A value of 0.40 implies that the area occupied by the central bar represents 40% of the points. > > ~~~ > > plt.hist(normal_dist, bins, density=True) > > ~~~ > > {: .language-python } > > ![Histogram as density](../fig/normal-hist-density.png) > > > > A perhaps more intuitive plot, is represented by the cumulative density, which > > as previously described should add to 1. > > ~~~ > > plt.hist(normal_dist, bins, density=True, cumulative=True) > > ~~~ > > {: .language-python } > > ![Histogram as cumulative density](../fig/normal-hist-cumuldensity.png) > > > {: .solution } {: .challenge } ## Bar ## Histograms ## Subplots - the next exercise assumes an example of subplots arranged in a single row or column ... ...
 ... ... @@ -94,6 +94,7 @@ [matplotlib-home]: https://matplotlib.org/ [matplotlib-linestyle]: https://matplotlib.org/3.2.2/gallery/lines_bars_and_markers/linestyles.html [matplotlib-markerstyle]: https://matplotlib.org/3.2.2/api/markers_api.html [matplotlib-hist]: https://matplotlib.org/3.2.2/api/_as_gen/matplotlib.pyplot.hist.html [matplotlib-plot]: https://matplotlib.org/3.2.2/api/_as_gen/matplotlib.pyplot.plot.html [matplotlib-scatter]: https://matplotlib.org/3.2.2/api/_as_gen/matplotlib.pyplot.scatter.html [matplotlib-scatter-examples]: https://matplotlib.org/3.2.2/api/_as_gen/matplotlib.pyplot.scatter.html#examples-using-matplotlib-pyplot-scatter ... ... @@ -170,6 +171,7 @@ [w3c-py-classes]: https://www.w3schools.com/python/python_classes.asp [walrus]: https://www.geeksforgeeks.org/walrus-operator-in-python-3-8/ [wikipedia-latex]: https://en.wikipedia.org/wiki/LaTeX [wikipedia-misleading]: https://en.wikipedia.org/wiki/Misleading_graph [wikipedia-pdf]: https://en.wikipedia.org/wiki/PDF [wikipedia-png]: https://en.wikipedia.org/wiki/Portable_Network_Graphics [wikipedia-raster]: https://en.wikipedia.org/wiki/Raster_graphics ... ...

38.8 KB

37.1 KB | W: | H:

36.1 KB | W: | H:

• 2-up
• Swipe
• Onion skin

40 KB | W: | H:

39 KB | W: | H:

• 2-up
• Swipe
• Onion skin
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!