Commit 127ba05e by Renato Alves 🌱

### Add scatter and point plot

parent bee1f5db
 ... ... @@ -518,6 +518,12 @@ x = np.linspace(0, 2, 4) > > > > plt.plot(x, np.sin(x), label='sin') > > plt.plot(x, np.sin(x**3), label='sin-cubic') > > > > # And the other elements of the plot that we need to repeat > > plt.xlabel('x label') > > plt.ylabel('y label') > > plt.title("Simple Plot") > > plt.legend() > > ~~~ > > {: .language-python } > > ... ... @@ -529,6 +535,73 @@ x = np.linspace(0, 2, 4) > {: .solution } {: .challenge } ## Point plots and other variants The `plot()` function is highly versatile by allowing us to modify the type of line drawn, include point markers, use different colors, line thickness, and many other options covered in the [`plot()` documentation][matplotlib-plot]. Modifying markers, lines and color is such a common task that this function provides a convenient shorthand notation to specify all three options in one go. You can supply a string with the `"[marker][line][color]"` notation as the last argument. For example, `"o-b"` encodes a `circle (o)` marker, a continuous `line (-)` and both in blue `color (b)`. Each component is optional so providing only a line style (`"-"`) or a marker and a color (`"ob"`) is perfectly valid. As markers and lines are aspects of a plot that are common to other plotting functions, they also have dedicated pages in `matplotlib`'s documentation. You can visit the [gallery of markers][matplotlib-markers], [the equivalent page for line styles][matplotlib-linestyle] and the [gallery of colors][matplotlib-colors], to which you can refer by [name][matplotlib-color-names], [RGB or hexadecimal code][matplotlib-color-codes]. Lets now try to customize our polynomial plot from before: ~~~ x = np.linspace(0, 2, 10) # We reduce the number of samples for visual clarity plt.plot(x, x, "o-b", label='linear') # full circles and continuous line in blue plt.plot(x, x**2, "v--r", label='quadratic') # down pointing triangles a dashed line in red plt.plot(x, x**3, "*-.g", label='cubic') # starts with dot dashed line in green ~~~ {: .language-python } ![custom marker line color plot](../fig/my-custom-marker-line-color.png) > ## Point and scatter > > `matplotlib` includes also a [`scatter()` function][matplotlib-scatter] > that provides additional features over simple point plots. > Using the [`scatter()` documentation][matplotlib-scatter] and the scatter plot examples > in the [`scatter()` gallery section][matplotlib-scatter-example], > create a variant of the point plot above with points of increasing size. > > **Hint**: you will need to use the `s=` attribute of the `scatter()` function. > See the [`scatter()` documentation][matplotlib-scatter] for additional options. > You may also need to multiply all values by a constant if the difference in sizes is too small. > > > ## Solution > > > > Since `scatter()` doesn't draw lines, we cannot use the `[marker][line][color]` notation, > > but we can still specify the `marker=` style. > > > > A possible solution is: > > ~~~ > > x = np.linspace(0, 2, 20) > > scale_factor = 10 > > > > plt.scatter(x, x, s=scale_factor * x, marker="o", label='linear') > > plt.scatter(x, x**2, s=scale_factor * x**2, marker="v", label='quadratic') > > plt.scatter(x, x**3, s=scale_factor * x**3, marker="*", label='cubic') > > > > # And the other elements of the plot that we need to repeat > > plt.xlabel('x label') > > plt.ylabel('y label') > > plt.title("Simple Plot") > > plt.legend() > > ~~~ > > {: .language-python } > > ![custom scatter marker line color plot](../fig/my-scatter-marker-line-color.png) > {: .solution } {: .challenge } ### Histograms ... ...
 ... ... @@ -82,9 +82,17 @@ [markdown-cheatsheet]: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet [matlab-home]: https://www.mathworks.com/products/matlab.html [matplotlib-backends]: https://matplotlib.org/tutorials/introductory/usage.html#the-builtin-backends [matplotlib-colors]: https://matplotlib.org/3.2.2/api/colors_api.html [matplotlib-color-names]: https://matplotlib.org/3.2.2/gallery/color/named_colors.html [matplotlib-color-codes]: https://matplotlib.org/3.2.2/tutorials/colors/colors.html [matplotlib-gallery]: https://matplotlib.org/gallery/index.html [matplotlib-hinton]: https://matplotlib.org/examples/specialty_plots/hinton_demo.html [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-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 [mit-license]: https://opensource.org/licenses/mit-license.html [modulo-operator]: https://python-reference.readthedocs.io/en/latest/docs/operators/modulus.html [morea]: https://morea-framework.github.io/ ... ...

59.5 KB

Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!