<jupyter_start><jupyter_text>Python for Psychologists - Session 9 session8 recap & plotting Python offers multiple "plotting" libraries, each of them with different features.Today we want to cover two (probably the most common) libraries- matplotlib- seaborn A plot usually contains two main components, i.e., a figure and axes. Image the figure as a page on which you can draw whatever you like. Following that, a figure can contain multiple independent plots, a legend, color bar etc. The axes is the area where we plot our data and any labels are associated with. Each axes has a x and y -axis  ![fig](fig.png)  matplotlib. - We can use basic matplotlib commands to easiliy create plots.<jupyter_code>import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>`%matplotlib inline`or `plt.show()` will show your plot instantly. The latter is particularly used outside jupyter notebooks<jupyter_code>import numpy as np

x = np.linspace(0,10,20) # generates 20 numbers between 0 and 10 
y = x**2                 # x square<jupyter_output><empty_output><jupyter_text>Now that we got our first plot, let´s give it a name and label the x and y axis Now imagine you need more than one plot on your page. We can easily do this with `plt.subplot()`<jupyter_code>#nrows #ncolums #plot_number<jupyter_output><empty_output><jupyter_text>- we could also plot by creating Figure objects in matplotlib Let´s create an empty figure object with `.figure()` , i.e. an object oriented approach. By setting `figsize=(a,b)` one could increase or decrease ones "canvas"<jupyter_code>fig =<jupyter_output><empty_output><jupyter_text>Let´s add a blank set of axis using ``fig.add_axes([location_where_axes_should_be_located])``<jupyter_code>ax1 =  #left #bottom # widht #height  
fig<jupyter_output><empty_output><jupyter_text>Remember that figure can contain more than just one plot. Let´s try to insert a second figure on our canvas. This will help us to understand the input `.add_axes([])` takes<jupyter_code>ax2 = 
fig<jupyter_output><empty_output><jupyter_text>Let´s plot our x and y arrays on our new blank axis and add x and y labels as well as a plot name. However, here we need to use e.g., `.set_xlabel` instead of just `.xlabel`<jupyter_code>fig<jupyter_output><empty_output><jupyter_text>As for the first approach, we could also create multiple plots in the object oriented approach using `.subplots(nrows=, ncols=)` and **not** `.subplot()` as we did before! As we can see, we did create some overlap between our plots, no worris we can use `plt.tight_layout()` to solve this issue. Very conveniently, `plt.subplots()` will automatically add_axes based on the rows and colum input and you don´t have to specify it as we had to using `plt.figure()`<jupyter_code><jupyter_output><empty_output><jupyter_text>Now we could try to plot our x & y arrays to specific subplots. We could do this by indexing ax! In some way, your subplot behaves as a single cell in your dataframe, i.e. we could index it easily by choosing [row/column]<jupyter_code># changes color and linestyle 
 # changes the linewidth


 #changes lower and upper bound of x axis

fig<jupyter_output><empty_output><jupyter_text>seabornseaborn is based on matplotlib, but usually works with less lines of codes and therefore provides a easy to handle vizualisation interface.For further information, see https://seaborn.pydata.org/<jupyter_code>import pandas as pd
iris = pd.read_csv("iris.csv", sep=",")
iris.head()<jupyter_output><empty_output><jupyter_text>Let´s try to create a scatter plot with for sepal.length & sepal.width- matplotlib- seaborn<jupyter_code># create a figure and axis


# scatter the sepal_length against the sepal_width

# set a title and labels



import seaborn as sns<jupyter_output><empty_output><jupyter_text>We could also group our scatterplot by variety using the ``hue`` argument, i.e. different groups will be colored in different numbers. We could easily plot a line chart using `sns.lineplot()`.  The only argument that we need are the four numeric columns in our case.  We could also use ``sns.boxplot(x=,y=,data=)`` or ``sns.barplot(x=,y=,data=)`` to plot some characteristic of our three categories. The standard solution comes with a 95% confidence intervall around your point estimate.  A nice way to to get a first idea about your data (from a plotting perspective) is `sns.pairplot()` or `sns.heatmap()` As we can see, the output does not look that fine, here we can combine matplotlib and seaborn to customize our plot! We could also break our data up across multiple subplots (i.e. *faceting*) and combine it into one single figure. First we can create a multiplot grid (i.e. ``sns.FacetGrid``) which takes our column variety into account and hence creates three empty grids. Afterwards we can use the ``.map()`` function, that calls the specified function for each object of an iterable (i.e., the empty grids in our case)<jupyter_code>#plot a univariat distribution of observations<jupyter_output><empty_output><jupyter_text>Controlling figure aesthetics<jupyter_code>def sinplot(flip=1):
    x = np.linspace(0, 14, 100)
    for i in range(1, 7):
        plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)
        
sinplot()<jupyter_output><empty_output><jupyter_text>```sns.set_style()``` changes the figure theme, go check it out by using "darkgrid" or "whitegrid" or "white" or "ticks" or "dark" as an argument<jupyter_code>sns.set_style("ticks")
sinplot()<jupyter_output><empty_output><jupyter_text>We could also remove the top and right axis spine (only white or ticks thema can benefit from it) by using `sns.despine()`<jupyter_code>sinplot()
sns.despine()<jupyter_output><empty_output><jupyter_text>We could also scale our plots for different context by using `sns.set_context()`. Go and try it for "paper", "notebook", "talk", and "poster".<jupyter_code>sns.set_context("paper")
sinplot()
sns.despine()<jupyter_output><empty_output><jupyter_text>to switch back to the default seaborn settings, simply use `sns.set()`<jupyter_code>sns.set()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup