<jupyter_start><jupyter_text>Matplotlib ExampleThis is an example notebook describing the basics of Matplotlib, a plotting library for Python. More details are available on the [Matplotlib Home Page](https://matplotlib.org/).This example uses an [automotive](https://archive.ics.uci.edu/ml/datasets/Automobile) data set from [University of California, Irvine](https://uci.edu/).<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import math
from sklearn import datasets
from matplotlib.colors import ListedColormap<jupyter_output><empty_output><jupyter_text>Basic Line PlotThe following example generates sine a cosine curves as a list of values and plots them. Some basic plot customizations are also demonstrated.<jupyter_code>x = [0.1*i for i in range(64)]
sin = [math.sin(i) for i in x]
cos = [math.cos(i) for i in x]

plt.plot(x, sin, label='sin(x)', color='blue', linewidth=2.5)
plt.plot(x, cos, label='cos(x)', color='red', linewidth=0.5)
plt.ylabel('y')
plt.xlabel('x')
plt.legend(loc='upper right')
plt.show()<jupyter_output><empty_output><jupyter_text>Basic Bar Chart<jupyter_code>regions = ['Germany', 'Italy', 'Spain', 'Japan', 'South Korea', 'Sweden']
population = {'Germany':83783942, 'Italy':60461826, 'Spain':46754778, 'India':1380004385,
              'United States':331002651, 'Japan':126476461, 'South Korea':51269185, 'Sweden':10099265}

y_pos = range(len(regions))
values = [population[r]/1e6 for r in regions]

plt.bar(y_pos, values, align='center')
plt.xticks(y_pos, regions, rotation='vertical')
plt.ylabel('Population (millions)')
plt.title('Population by Region')
plt.show()<jupyter_output><empty_output><jupyter_text>Color MapsColors can be controlled with color maps. Matplotlib has a number of built-in color maps documented in this [reference](https://matplotlib.org/examples/color/colormaps_reference.html).<jupyter_code>iris = datasets.load_iris()

# Select all rows and only first two columns (sepal length/width)
X = iris.data[:, :2]

# Target will be used to plot samples in different colors for different species
Y = iris.target

# Use built-in color map, see https://matplotlib.org/examples/color/colormaps_reference.html
plt.scatter(X[:,0], X[:,1], c=Y, cmap='coolwarm')<jupyter_output><empty_output><jupyter_text>You can also create your own [custom color map](https://matplotlib.org/3.1.0/tutorials/colors/colormap-manipulation.html) using ListedColormap.<jupyter_code>colors =["red", "green", "blue"]
custom_map = ListedColormap(colors)

plt.scatter(X[:,0], X[:,1], c=Y, cmap=custom_map)<jupyter_output><empty_output><jupyter_text>Plotting Pandas DataPandas can create plots directly from a data frame. The following cell loads automobile data from a text file.<jupyter_code>!wget https://raw.githubusercontent.com/msiddalingaiah/MachineLearning/master/Notebooks/data/imports-85.data
columnNames = ['symboling','normalized_losses','make','fuel_type','aspiration','num_of_doors','body_style',
               'drive_wheels','engine_location','wheel_base','length','width','height','curb_weight',
               'engine_type','num_of_cylinders','engine_size','fuel_system','bore','stroke','compression_ratio',
               'horsepower','peak_rpm','city_mpg','highway_mpg','price']

cars = pd.read_csv('imports-85.data', sep=',', names = columnNames)<jupyter_output><empty_output><jupyter_text>There are some missing values for horsepower and price. The following cell selects only rows with numeric values for horsepower and price, then it converts those columns to floating point types.<jupyter_code>df = cars[cars.horsepower.str.isnumeric() & cars.price.str.isnumeric()].copy()
df.horsepower = df.horsepower.astype(float)
df.price = df.price.astype(float)<jupyter_output><empty_output><jupyter_text>Pandas dataframes contain an easy to use plot method. The following scatter plot shows that more expensive cars tend to produce more power.<jupyter_code>df.plot(kind='scatter', x='price', y='horsepower', title='Horsepower vs. Price')<jupyter_output><empty_output><jupyter_text>Aggregating DataPandas can perform aggregate calculations, similar to by the group-by clause in SQL. The following example is equivalent of the SQL query`SELECT count(make) FROM cars GROUP BY make ORDER BY make`<jupyter_code># This could all be done in one line, but it has been formatted for clarity

groups = cars.groupby(['make'], sort=True)
counts = groups['make'].count()
counts.plot(kind='bar', title='Number of cars')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1319073083778966, 'fraction_numerical': 0.020053475935828877, 'mean_word_length': 6.699828473413379, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 7, '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': '293433', 'license': 'MIT', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1437, 'n_words': 375, 'path': 'Notebooks/Matplotlib.ipynb', 'repo_name': 'cas10ph/MachineLearning', 'size': '4488'}