<jupyter_start><jupyter_text>Data Visualization  `matplotlib` - from the documentation:https://matplotlib.org/3.1.1/tutorials/introductory/pyplot.html`matplotlib.pyplot` is a collection of command style functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.In `matplotlib.pyplot` various states are preserved across function calls, so that it keeps track of things like the current figure and plotting area, and the plotting functions are directed to the current axes."axes" in most places in the documentation refers to the axes part of a figure and not the strict mathematical term for more than one axis).<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Call signatures::```    plot([x], y, [fmt], data=None, **kwargs)    plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)``` Quick plot The main usage of `plt` is the `plot()` and `show()` functions<jupyter_code>plt.plot()
plt.show()<jupyter_output><empty_output><jupyter_text>List<jupyter_code>plt.plot([8, 24, 27, 42])
plt.ylabel('numbers')
plt.show()
# Plot the two lists, add axes labels
x=[4,5,6,7]
y=[2,5,1,7]<jupyter_output><empty_output><jupyter_text>`matplotlib` can use *format strings* to quickly declare the type of plots you want. Here are *some* of those formats:|**Character**|**Description**||:-----------:|:--------------||'--'|Dashed line||':'|Dotted line||'o'|Circle marker||'^'|Upwards triangle marker||'b'|Blue||'c'|Cyan||'g'|Green|<jupyter_code>plt.plot([3, 4, 9, 20], 'gs')
plt.axis([-1, 4, 0, 22])
plt.show()
plt.plot([3, 4, 9, 20], 'b^--', linewidth=2, markersize=12)
plt.show()
plt.plot([3, 4, 9, 20], color='blue', marker='^', linestyle='dashed', linewidth=2, markersize=12)
plt.show()
# Plot a list with 10 numbers with a magenta dotted line and circles for points.


import numpy as np

# evenly sampled time 
time = np.arange(0, 7, 0.3)
# gene expression
ge = np.arange(1, 8, 0.3)

# red dashes, blue squares and green triangles
plt.plot(time, ge, 'r--', time, ge**2, 'bs', time, ge**3, 'g^')
plt.show()<jupyter_output><empty_output><jupyter_text>linestyle or ls	[ '-' | '--' | '-.' | ':' |<jupyter_code>lines = plt.plot([1, 2, 3])
plt.setp(lines)
names = ['A', 'B', 'C', 'D']
values = [7, 20, 33, 44]
values1 = np.random.rand(100)

plt.figure(figsize=(9, 3))

plt.subplot(131)
plt.bar(names, values)
plt.subplot(132)
plt.scatter(names, values)
plt.subplot(133)
plt.hist(values1)
plt.suptitle('Categorical Plotting')
plt.show()
import pandas as pd
df_iris = pd.read_csv('https://raw.githubusercontent.com/uiuc-cse/data-fa14/gh-pages/data/iris.csv')
df_iris.head()
x1 = df_iris.petal_length
y1 = df_iris.petal_width

x2 = df_iris.sepal_length
y2 = df_iris.sepal_width

plt.plot(x1, y1, 'g^', x2, y2, 'bs')
plt.show()<jupyter_output><empty_output><jupyter_text>Histogram<jupyter_code>help(plt.hist)
n, bins, patches = plt.hist(df_iris.petal_length, bins=20,facecolor='#8303A2', alpha=0.8, rwidth=.8, align='mid')

# Add a title
plt.title('Iris dataset petal length')

# Add y axis label
plt.ylabel('number of plants')<jupyter_output><empty_output><jupyter_text>Boxplot<jupyter_code>help(plt.boxplot)
plt.boxplot(df_iris.petal_length)

# Add a title
plt.title('Iris dataset petal length')

# Add y axis label
plt.ylabel('petal length')<jupyter_output><empty_output><jupyter_text>The biggest issue with `matplotlib` isn't its lack of power...it is that it is too much power. With great power, comes great responsibility. When you are quickly exploring data, you don't want to have to fiddle around with axis limits, colors, figure sizes, etc. Yes, you *can* make good figures with `matplotlib`, but you probably won't. https://python-graph-gallery.com/matplotlib/ Pandas works off of `matplotlib` by default. You can easily start visualizing dataframs and series just by a simple command.  Using pandas `.plot()` Pandas abstracts some of those initial issues with data visualization. However, it is still a `matplotlib` plotEvery plot that is returned from `pandas` is subject to `matplotlib` modification.<jupyter_code>df_iris.plot.box()
plt.show()
# Plot the histogram of the petal lengths
# Plot the histograms of all 4 numerical characteristics in a plot



df_iris.groupby("species")['petal_length'].mean().plot(kind='bar')
plt.show()
df_iris.plot(x='petal_length', y='petal_width', kind = "scatter")
plt.savefig('output.png')
plt.savefig('output.png')<jupyter_output><empty_output><jupyter_text>https://github.com/pandas-dev/pandas/blob/v0.25.0/pandas/plotting/_core.pyL504-L1533  Multiple Plots<jupyter_code>df_iris.petal_length.plot(kind='density')
df_iris.sepal_length.plot(kind='density')
df_iris.petal_width.plot(kind='density')<jupyter_output><empty_output><jupyter_text>`matplotlib` allows users to define the regions of their plotting canvas. If the user intends to create a canvas with multiple plots, they would use the `subplot()` function. The `subplot` function sets the number of rows and columns the canvas will have **AND** sets the current index of where the next subplot will be rendered.<jupyter_code>plt.figure(1)
# Plot all three columns from df in different subplots
# Rows first index (top-left)
plt.subplot(3, 1, 1)
df_iris.petal_length.plot(kind='density')
plt.subplot(3, 1, 2)
df_iris.sepal_length.plot(kind='density')
plt.subplot(3, 1, 3)
df_iris.petal_width.plot(kind='density')
# Some plot configuration
plt.subplots_adjust(top=.92, bottom=.08, left=.1, right=.95, hspace=.25, wspace=.35)
plt.show()
# Temporary styles
with plt.style.context(('ggplot')):
    plt.figure(1)

    # Plot all three columns from df in different subplots
    # Rows first index (top-left)
    plt.subplot(3, 1, 1)
    df_iris.petal_length.plot(kind='density')
    plt.subplot(3, 1, 2)
    df_iris.sepal_length.plot(kind='density')
    plt.subplot(3, 1, 3)
    df_iris.petal_width.plot(kind='density')
    # Some plot configuration
    plt.subplots_adjust(top=.92, bottom=.08, left=.1, right=.95, hspace=.25, wspace=.35)
    plt.show()
# Plot the histograms of the petal length and width and sepal length and width 
# Display them on the columns of a figure with 2X2 subplots
# color them red, green, blue and yellow, respectivelly<jupyter_output><empty_output><jupyter_text>`seaborn` - dataset-oriented plotting Seaborn is a library that specializes in making *prettier* `matplotlib` plots of statistical data. It is built on top of matplotlib and closely integrated with pandas data structures.https://seaborn.pydata.org/introduction.htmlhttps://python-graph-gallery.com/seaborn/<jupyter_code>import seaborn as sns<jupyter_output><empty_output><jupyter_text>`seaborn` lets users *style* their plotting environment.<jupyter_code>sns.set(style='whitegrid')<jupyter_output><empty_output><jupyter_text>However, you can always use `matplotlib`'s `plt.style`<jupyter_code>#dir(sns)
sns.scatterplot(x='petal_length',y='petal_width',data=df_iris)
plt.show()
sns.scatterplot(x='petal_length',y='petal_width', hue = "species",data=df_iris)
plt.show()<jupyter_output><empty_output><jupyter_text>Violin plot Fancier box plot that gets rid of the need for 'jitter' to show the inherent distribution of the data points<jupyter_code>columns = ['petal_length', 'petal_width', 'sepal_length']

fig, axes = plt.subplots(figsize=(10, 10))
sns.violinplot(data=df_iris.loc[:,columns], ax=axes)
axes.set_ylabel('number')
axes.set_xlabel('columns', )
plt.show()<jupyter_output><empty_output><jupyter_text>Distplot<jupyter_code>sns.set(style='darkgrid', palette='muted')

# 1 row, 3 columns
f, axes = plt.subplots(4,1, figsize=(10,10), sharex=True)
sns.despine(left=True)

# Regular displot
sns.distplot(df_iris.petal_length, ax=axes[0])

# Change the color
sns.distplot(df_iris.petal_width, kde=False, ax=axes[1], color='orange')

# Show the Kernel density estimate
sns.distplot(df_iris.sepal_width, hist=False, kde_kws={'shade':True}, ax=axes[2], color='purple')

# Show the rug
sns.distplot(df_iris.sepal_length, hist=False, rug=True, ax=axes[3], color='green')<jupyter_output><empty_output><jupyter_text>FacetGrid<jupyter_code>sns.set()
columns = ['species', 'petal_length', 'petal_width']
facet_column = 'species'
g = sns.FacetGrid(df_iris.loc[:,columns], col=facet_column, hue=facet_column, col_wrap=5)
g.map(plt.scatter, 'petal_length', 'petal_width')
sns.relplot(x="petal_length", y="petal_width", col="species",
            hue="species", style="species", size="species",
            data=df_iris)
plt.show()<jupyter_output><empty_output><jupyter_text>https://jakevdp.github.io/PythonDataScienceHandbook/04.14-visualization-with-seaborn.html  `plotnine` - R ggplot2 in python plotnine is an implementation of a grammar of graphics in Python, it is based on ggplot2. The grammar allows users to compose plots by explicitly mapping data to the visual objects that make up the plot.Plotting with a grammar is powerful, it makes custom (and otherwise complex) plots are easy to think about and then create, while the simple plots remain simple.<jupyter_code>!pip install plotnine<jupyter_output><empty_output><jupyter_text>https://plotnine.readthedocs.io/en/stable/<jupyter_code>from plotnine import *
ggplot(data=df_iris) + aes(x="petal_length", y = "petal_width") + geom_point()
# add transparency - to avoid over plotting
ggplot(data=df_iris) + aes(x="petal_length", y = "petal_width") + geom_point(alpha=0.7)
# change point size 
ggplot(data=df_iris) + aes(x="petal_length", y = "petal_width") + geom_point(size = 0.7, alpha=0.7)
# more parameters 
ggplot(data=df_iris) + aes(x="petal_length", y = "petal_width") + geom_point() + scale_x_log10() + xlab("Petal Length")
n = "3"
ft = "length and width"
title = 'species : %s, petal : %s' % (n,ft)

ggplot(data=df_iris) +aes(x='petal_length',y='petal_width',color="species") + geom_point(size=0.7,alpha=0.7) + facet_wrap('~species',nrow=3) + theme(figure_size=(9,5)) + ggtitle(title)

p = ggplot(data=df_iris) + aes(x='petal_length') + geom_histogram(binwidth=1,color='black',fill='grey')
p
ggsave(plot=p, filename='hist_plot_with_plotnine.png')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14517231279898468, 'fraction_numerical': 0.01962315727814117, 'mean_word_length': 6.198875614898102, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 73, 'https://': 8, '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': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '12692', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 3950, 'n_tokens_neox': 3669, 'n_words': 923, 'path': 'classes/visualization.ipynb', 'repo_name': 'betteridiot/b575f19', 'size': '10243'}