<jupyter_start><jupyter_text>Table of Content[Matplotlib](Matplotlib)* [Introduction](Introduction)* [Plot Types](PlotTypes)	* [Line Plot](LinePlot)    * [Scatter Plot](ScatterPlot)    * [Histogram Plot](LinePlot)    * [Box Plot](LinePlot)    * [Bar Chart Plot](BarChartPlot)    * [Pie Chart Plot](BarChartPlot)* [Summary](Summary)  MatplotlibThe Matplotlib library can help you to present data as graphs.With Matplotlib anyone can make a simple interactive application without knowing anything about Matplotlib internals like draw buffers, event loops, or even about GUI toolkit is.The module can be installed using pip:```pip install matplotlib```Let's draw first simple plot. First we must import co-responding matplotlib interface(pyplot).    Next step is to import NumPy, while we will use NumPy to generate dataset(from 0 to 10, with 100 steps), and then we will draw our first graph: ```import matplotlib.pyplot as pltimport numpy as npx = np.linepace(0, 10, 100)plt.plot(x, x)plt.show()```Now if we save code in file first-plot.py and if we run a script:```python first-plot.py```our plot will be displayed.The way how Matplotlib will visualize plots depends on environment.If we are using Python script, then function ```plt.show()``` should be used.If we are using Jupyter notebook, then command ```%matplotlib``` should be used. This command will embed graphs directly in the notebook, with two possible options: * ```%matplotlib notebook``` will create interactive plots embedded within the notebook * ```%matplotlib inline``` will create static images of your plot embedded in the notebookAfter running following commands, plot in form of PNG image will be embedded in notebook:<jupyter_code>#%matplotlib notebook
%matplotlib inline

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

plt.plot(x, x)<jupyter_output><empty_output><jupyter_text>IntroductionImportant part of data analysis is data visualization. Data can be visualized using various plot types like line plot, scatter plot, histogram, box plot, bar chart and pie chart etc.Pyhton offers many visualization packages and Matplotlib package is one which is used very often.<NAME>, a neurobiologist, began developing Matplotlib around 2003, originally inspired to emulate commands from Mathworks’ MATLAB software.Version 0.1 was released in 2003. This version received an early boost when it was adopted as the plotting package of choice of the Space Telescope Science Institute (the folks behind the Hubble Telescope), which financially supported Matplotlib’s development and greatly expanded its capabilities.Current release is 3.0.x,and library is used by many developers worldwide, very often as a one of the main data visualization tools within the data scientific Python world.Matplotlib is a multi-platform library which supports many operating systems and graphics backends. Matplotlib is built on top of the NumPy arrays. Before we jump into more complex examples, let's first check basic functions of the matplotlib.pyplot:```plt.title(“Title”)```will add a title “Title” to your plot``` plt.xlabel(“X Label”) ``` will add a label “X Label” to your x-axis``` plt.ylabel(“Y Label”)```will add a label “Y Label” to your y-axis```plt.xticks([1, 2, 3, 4, 5]) ```will set the numbers on the x-axis to be 1, 2, 3, 4, 5. We can also pass and labels as a second argument. For, example, if we use this code ```plt.xticks([1, 2, 3, 4, 5], ["1M", "2M", "3M", "4M", "5M"])```it will set the labels 1M, 2M, 3M, 4M, 5M on the x-axis```plt.yticks()```works the same as plt.xticks(), but for the y-axis ```fig.savefig('plot.png')```will save plot in file in PNG formats```plt.plot(x, np.sin(x - 0), color='blue')        ```will draw line(second line, sinusoid calculated with NumPy function) in specified color. Color can be specified as:    * short color code: ```color='g'```    * grayscale between 0 and 1: ```color='0.75'```        * hex code (RRGGBB from 00 to FF): ```color='FFDD44'```     * RGB tuple: ```color='1.0,0.2,0.3'```    * HTML color ```color='chartreuse'```   ```plt.plot(x, x + 1, linestyle='dashed')```will draw line with special line style. Linestyles are:    * solid: ```linestyle='-'``` or ```linestyle='solid'```      * dashed: ```linestyle='--'``` or ```linestyle='dashed'```    * dashdot: ```linestyle='-.'``` or ```linestyle='dashdot'```    * dotted: ```linestyle=':'``` or ```linestyle='dotted'```     Plot TypesAs we already mentioned data can be visualized using various plots, so let's dig deeper in plot types: Line PlotLine Plot is a type of plot which displays information as a series of data points called ***markers*** connected with straight lines. Measurement points must be be ordered (typically x-axis values). Line plot is often used to visualize a trend in data over intervals of time, a time series data.To make a line plot with Matplotlib, we call function```plt.plot()```<jupyter_code>y = np.linspace(0, 200, 200)

plt.plot(y, y)
print(y)<jupyter_output>[  0.           1.00502513   2.01005025   3.01507538   4.0201005
   5.02512563   6.03015075   7.03517588   8.04020101   9.04522613
  10.05025126  11.05527638  12.06030151  13.06532663  14.07035176
  15.07537688  16.08040201  17.08542714  18.09045226  19.09547739
  20.10050251  21.10552764  22.11055276  23.11557789  24.12060302
  25.12562814  26.13065327  27.13567839  28.14070352  29.14572864
  30.15075377  31.15577889  32.16080402  33.16582915  34.17085427
  35.1758794   36.18090452  37.18592965  38.19095477  39.1959799
  40.20100503  41.20603015  42.21105528  43.2160804   44.22110553
  45.22613065  46.23115578  47.2361809   48.24120603  49.24623116
  50.25125628  51.25628141  52.26130653  53.26633166  54.27135678
  55.27638191  56.28140704  57.28643216  58.29145729  59.29648241
  60.30150754  61.30653266  62.31155779  63.31658291  64.32160804
  65.32663317  66.33165829  67.33668342  68.34170854  69.34673367
  70.35175879  71.35678392  72.36180905  73.36683417  74.3718593
  75.37688442[...]<jupyter_text>The first argument is used for the data on the horizontal axis, and the second is used for the data on the vertical axis. If we want to add some additional customizations to our plot we should do it before we display it.    For example, we might want to add labels to the axis and title for the plot.Let's see more detailed example:<jupyter_code>x = np.linspace(1, 10, 100)
y = np.cos(x)

plt.xlabel("X")
plt.ylabel("Y")
plt.grid(True)
plt.title("Title")
plt.plot(x, y,  color='r', linestyle='dashed')<jupyter_output><empty_output><jupyter_text>Scatter Plot    Scatter plot is a type of the plot which shows all individual data points. Datapoints are not connected with lines. Each data point has the value of the x-axis value and the value from the y-axis values. This type of plot can be used to display trends or correlations. In data science, it shows how 2 variables compare.To make a scatter plot with Matplotlib, we can use the ```plt.scatter()``` function.Again, the first argument is used for the data on the horizontal axis, and the second - for the vertical axis.Let's see example where we plot relation between outside temperature and number of the visitors in museum:<jupyter_code>temperature = np.array([30, 32, 33, 28.5, 35, 29, 29])
visitors = np.array([100, 115, 115, 75, 125, 79, 89])
plt.scatter(temperature, visitors)
plt.xlabel("Temperature")
plt.ylabel("Visitors")
plt.title("Temperature vs Number of visitors")<jupyter_output><empty_output><jupyter_text>Histogram PlotHistogram plot represents the distribution of numeric data accurately. To create a histogram, we divide the entire range of values into a series of intervals, and then, we count how many values fall into each interval. The intervals are also called bins. The bins are consecutive and non-overlapping intervals of a variable. They must be adjacent and are often of equal size.To make a histogram with Matplotlib, we can use the ```plt.hist()``` function. The first argument is the numeric data, the second argument is the number of bins. The default value for the bins argument is 10.<jupyter_code>numbers = np.array([100, 115, 115, 75, 125, 79, 89])

plt.hist(numbers, bins = 10)
plt.xlabel("Number")
plt.ylabel("Frequency")
plt.title("Histogram")<jupyter_output><empty_output><jupyter_text>We can see from the histogram above that there are: * 2 values between 70 and 80    * 1 values between 80 and 90    * 0 values between 90 and 100    * 1 value between 100 and 110    * 2 values between 110 and 120    * 1 value between 120 and 130     Box PlotBox plot, also called the box-and-whisker plot: a way to show the distribution of values based on the five-number summary: minimum, first quartile, median, third quartile, and maximum.The ***minimum*** and the ***maximum*** are just the min and max values from our data.The ***median*** is the value that separates the higher half of a data from the lower half. It’s calculated by the following steps: order your values, and find the middle one. In a case when our count of values is even, we actually have 2 middle numbers, so the median here is calculated by summing these 2 numbers and divide the sum by 2. For example, if we have the numbers 1, 2, 5, 6, 8, 9, your median will be (5 + 6) / 2 = 5,5.The **first quartile*** is the median of the data values to the left of the median in our ordered values. For example, if we have the numbers 1, 3, 4, 7, 8, 8, 9, the first quartile is the median from the 1, 3, 4 values, so it’s 3.The ***third quartile*** is the median of the data values to the right of the median in our ordered values. For example, if we use these numbers 1, 3, 4, 7, 8, 8, 9 again, the third quartile is the median from the 8, 8, 9 values, so it’s 8.Now, when we have some knowledge about the box plots, we can see that it’s super easy to create this plot with Matplotlib. All we need is the function ```plt.boxplot()```. The first argument is the data points.In this example the values 1, 2, and 21 are outliers, the maximum is 10, the minimum is 5 and the mean is 7.<jupyter_code>values = [1, 2, 5, 6, 6, 7, 7, 8, 8, 8, 9, 10, 21]

plt.boxplot(values)
plt.yticks(range(1, 22))
plt.ylabel("Value")
plt.show()<jupyter_output><empty_output><jupyter_text>Bar Chart PlotBar chart represents categorical data with rectangular bars. Each bar has a height corresponds to the value it represents. It’s useful when we want to compare a given numeric value on different categories. It can also be used with 2 data series.To make a bar chart with Maplotlib, we’ll need the ```plt.bar()``` function.From the example, we can see how the usage of the different programming languages compares. Note that some programmers can use many languages, so here the percentages are not summed up to 100%.<jupyter_code>labels = ["JavaScript", "Java", "Python", "C#"]
usage = [69.8, 45.3, 38.8, 34.4]

positions = range(len(labels))

plt.bar(positions, usage)
plt.xticks(positions, labels)
plt.ylabel("Usage (%)")
plt.title("Programming language usage")<jupyter_output><empty_output><jupyter_text>We can have horizontal bars with differnt colors:<jupyter_code>plt.barh(positions, usage, color=['red', 'green', 'blue', 'cyan'])
plt.yticks(positions, labels)
plt.xlabel("Usage (%)")
plt.title("Programming language usage")<jupyter_output><empty_output><jupyter_text>Pie Chart PlotPie chart: a circular plot, divided into slices to show numerical proportion. They are widely used in the business world. To make a bar chart with Matplotlib, we can use the ```plt.pie()``` function. The autopct parameter allows us to display the percentage value using the Python string formatting.Let's see and example:<jupyter_code>sizes = [25, 20, 45, 10]
labels = ["Cats", "Dogs", "Tigers", "Snakes"]

plt.pie(sizes, labels = labels)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09949995762352742, 'fraction_numerical': 0.08475294516484448, 'mean_word_length': 4.664906385021603, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '137955', 'license': 'MIT', 'n_tokens_mistral': 4305, 'n_tokens_neox': 3510, 'n_words': 1578, 'path': 'Matplotlib.ipynb', 'repo_name': 'fractus-io/matplotlib-tutorial', 'size': '11807'}