<jupyter_start><jupyter_text>Resources* http://www.matplotlib.org - The project web page for matplotlib.* https://github.com/matplotlib/matplotlib - The source code for matplotlib.* http://matplotlib.org/gallery.html - A large gallery showcaseing various types of plots matplotlib can create. Highly recommended! * http://www.loria.fr/~rougier/teaching/matplotlib - A good matplotlib tutorial.<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 5, 11)
y = x ** 2
%matplotlib inline
plt.scatter(x,y)
# generate some random data. Graph doesn't have to be the same
import matplotlib.pyplot as plt
import numpy as np

x = np.random.rand(100)
y = np.random.rand(100)

plt.hist(x,10)

plt.show()
# Recreate
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

data = [[1,2,5,1,3,4], [5,7,2,2,1.5,5], [7,2,5]]

plt.figure()
plt.boxplot(data)


plt.tight_layout()
plt.ylim(0,9)

# Generate a box plot using some random sample data
x = np.arange(0,100)
y = x*2
z = x**2<jupyter_output><empty_output><jupyter_text>** Follow along with these steps: *** ** Create a figure object called fig using plt.figure() *** ** Use add_axes to add an axis to the figure canvas at [0,0,1,1]. Call this new axis ax. *** ** Plot (x,y) on that axes and set the labels and titles to match the plot below:**<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

x = np.arange(0,100)
y = x*2
z = x**2

fig1 = plt.figure()
ax1 = fig1.add_axes([0,0,1,1])
ax1.plot(x,y)
ax1.set_title("title")
ax1.set_xlabel("x")                                 # x axis title
ax1.set_ylabel("y")                                 # y axis title<jupyter_output><empty_output><jupyter_text>*** Create a figure object and put two axes on it, ax1 and ax2. Located at [0,0,1,1] and [0.2,0.5,.2,.2] respectively.***<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

x = np.arange(0,100)
y = x*2
z = x**2

fig1 = plt.figure()
ax1 = fig1.add_axes([0,0,1,1])

ax2 = fig1.add_axes([0.2,0.5,.2,.2])





import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

x = np.arange(0,100)
y = x*2
z = x**2

fig1 = plt.figure()
ax1 = fig1.add_axes([0,0,1,1])
ax1.plot(x,y)

ax2 = fig1.add_axes([0.2,0.5,.2,.2])
ax2.plot(x,y)<jupyter_output><empty_output><jupyter_text>*** Now plot (x,y) on both axes. And call your figure object to show it.***<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

x = np.arange(0,100)
y = x*2
z = x**2

fig1 = plt.figure()
ax1 = fig1.add_axes([0,0,1,1])
ax1.plot(x,y)
ax1.set_xlabel("x")                                 # x axis title
ax1.set_ylabel("y")                                 # y axis title


ax2 = fig1.add_axes([0.2,0.5,.2,.2])
ax2.plot(x,y,'tab:orange')
ax2.set_xlim(20,22)
ax2.set_ylim(30,50)
ax2.set_xlabel("x")                                 # x axis title
ax2.set_ylabel("y")                                 # y axis title<jupyter_output><empty_output><jupyter_text>Advanced Matplotlib Concepts  Logrithmic scaleIt is also possible to set a logarithmic scale for one or both axes. This functionality is in fact only one application of a more general transformation system in Matplotlib. Each of the axes' scales are set seperately using `set_xscale` and `set_yscale` methods which accept one parameter (with the value "log" in this case):<jupyter_code>x = np.linspace(0, 5, 11)
y = x ** 2
import matplotlib.pyplot as plt


figure= plt.figure(figsize=(10, 4), dpi=72)
       
x = np.linspace(0, 5, 11)
y = x ** 2
z = np.exp(x)

axis1 = figure.add_subplot(1,2,1)  
axis1.plot(x,y)
axis1.plot(x,z)
axis1.set_title("Normal Scale")

axis2 = figure.add_subplot(1,2,2)    
axis2.plot(x,y)
axis2.plot(x,z)
axis2.set_yscale('log')
axis2.set_title(("Log Scale (y)"))

plt.show()
# Recreate<jupyter_output><empty_output><jupyter_text>Placement of ticks and custom ticks labelWe can explicitly determine where we want the axis ticks with `set_xticks` and `set_yticks`, which both take a list of values for where on the axis the ticks are to be placed. We can also use the `set_xticklabels` and `set_yticklabels` methods to provide a list of custom text labels for each tick location:<jupyter_code>import matplotlib.pyplot as plt


figure= plt.figure(figsize=(10, 4), dpi=72)
       
x = np.linspace(0, 5, 11)
y = x ** 2
z = np.exp(x)

axis1 = figure.add_subplot(111)  
axis1.plot(x,y)
axis1.plot(x,z)

axis1.tick_params(labelsize=20,direction='out',pad=10)
axis1.set_xticklabels(['','','α', 'β', 'γ', 'δ', 'ε'])


# did not managed to remve the 1st tick mark
# issue with greek letters formatting as top is cropped

greek_letterz=[chr(code) for code in range(945,970)]

print(greek_letterz)
# Recreate<jupyter_output><empty_output><jupyter_text>Scientific NotationWith large numbers on axes, it is often better use scientific notation:<jupyter_code>import matplotlib.pyplot as plt
from matplotlib import ticker

figure= plt.figure(figsize=(6, 4), dpi=72)
       
x = np.linspace(0, 5, 11)
y = x ** 2
z = np.exp(x)


axis1 = figure.add_subplot(1,1,1)  
axis1.plot(x,y)
axis1.plot(x,z)


formatter = ticker.ScalarFormatter(useMathText=True)
formatter.set_scientific(True) 
formatter.set_powerlimits((-1,1)) 

axis1.yaxis.set_major_formatter(formatter) 
axis1.set_title("scientific notation")
axis1.set_yticks([0, 50, 100, 150])

xx = np.linspace(-0.75, 1., 100)
n = np.array([0,1,2,3,4,5])
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

xx = np.linspace(-0.75, 1., 100)

y = xx*(0.5 + 0.5 * np.random.randint(-.05,5))
n = np.array([0,1,2,3,4,5])


fig1, a = plt.subplots(1, 4, figsize=(12,3))

a[0].scatter(xx, y)
a[0].set_title("scatter")

a[1].step(n, n**2)
a[1].set_title("step")

a[2].bar(n, n**2)
a[2].set_title("bar")

a[3].fill_between(x, x**2, x**3, color="green")
a[3].set_title("fill_between");
# Use xx for scatter plot, and n for rest of the three plots 
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

xx = np.linspace(-0.75, 1., 100)
n = np.array([0,1,2,3,4,5])

fig1, axis1 = plt.subplots()
axis1.plot(xx, xx**3)

axis1.spines['right'].set_color('none')
axis1.spines['top'].set_color('none')
axis1.xaxis.set_ticks_position('bottom')
axis1.spines['bottom'].set_position(('data',0)) 
axis1.yaxis.set_ticks_position('left')
axis1.spines['left'].set_position(('data',0))  




# Plot xx<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14794007490636704, 'fraction_numerical': 0.04915730337078652, 'mean_word_length': 4.210569105691057, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '318625', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 2656, 'n_tokens_neox': 2519, 'n_words': 656, 'path': 'Exercises - Qasim/Python. Pandas, Viz/Exercise 20 (Matplotlib part 3) - answers K.ipynb', 'repo_name': 'k21k/Python-Notes', 'size': '6408'}