<jupyter_start><jupyter_text>Python :> Plotting and Visualization¶<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import math as m
x = np.linspace(-2, 5, 8)
y = (1/(3)**(1/2))*x+4
y1 = (1/(3)**(1/2))*x+2
y1
plt.figure(figsize = (6,3))
plt.title("My first matplotlib plot", fontsize=12,style = "italic", fontweight="bold")
plt.plot(x, y, "k-o", label = "sales", alpha = 0.8, linewidth=2)
plt.plot(x, y1, "r-o", label = "profit", alpha = 0.8, linewidth=2)
plt.xlabel("X-values", fontsize=12)
plt.ylabel("Y-value", fontsize=12)
plt.legend(loc = "lower right", fontsize=12)
plt.axis([-3, 6, 0, 8])
plt.savefig("/Users/amitkumarsahu/Desktop/pandas/myplot.png",dpi=300)
plt.show()
plt.close()
A = np.array([[1,2],[3,4]])
print(A,"\n")

B = np.array([[10, 11],[12, 13]])
print(B)
D = np.hstack([A,B])
print("result of hstack\n", D)
D1 = np.vstack([A,B])
print("result of vstack\n", D1)
a = np.linspace(-4,4,10000)
x = np.hstack([a,a[::-1]])
y = np.hstack([(16-a**2)**(1/2),-(16-a**2)**(1/2)])
plt.axis("equal")#making equal scale of both axis unit length
plt.plot(x,y,"r-")
plt.show()
a = np.linspace(-4, 4, 9)
a
a[::-1]
w = "word"
w[::-1]

x = np.hstack([a,a[::-1]])
x
a[::1]
import math as m
theta = np.linspace(0,2*m.pi,100)
x = np.sin(theta)
y = np.cos(theta)
plt.figure(figsize=(4,4))
plt.plot(x,y,"r-")
plt.show()
plt.close
#without using numpy library, but using math library
x1 = [] #blank list for storing x
y1 = [] #blank list for storing y
for i in range (0, int(2*3.14*100),1):
    x1.append(4*m.sin(i/100))
    y1.append(4*m.cos(i/100))
plt.figure(figsize=(5,5))
plt.plot(x1,y1,"r-")<jupyter_output><empty_output><jupyter_text>SubPlotting¶<jupyter_code>data1 = np.arange(10)
data2 = np.array([0, 2, 2, 6, 4, 10, 6, 14, 8, 18])
data3 = np.array(np.random.randint(0, 18, 20))
data4 = np.array(np.random.randint(0, 100, 50))
print(data1)
print(data2)
print(data3)
print(data4)
#plotting data 1
plt.plot(data1, "k-",label = "default")
plt.show()
##ploting data2
plt.plot(data2,"r--*", label = "steps")
plt.show()
##plotting data3
# len(data3) check lenth of data3
x = np.arange(1,21)
plt.scatter(x,data3,label = "Scater", color = "g", s=60, marker = "*", alpha = 0.9)
plt.show()
plt.hist(data4, bins = 6, alpha = 0.9, color = "g", edgecolor = "k", label = "Hist")
plt.show()
fig = plt.figure(figsize = (8, 8))
fig.subplots_adjust(hspace=0.2, wspace = 0.3)
ax1 = fig.add_subplot(2,2,1)##figure should have 2x2 or utp 4 fig, this is figure
ax1.plot(data1, "k-", label = "Line")
plt.legend()
plt.xlabel("stages")
plt.ylabel("values")
###################################################
ax2 = fig.add_subplot(2,2,2)
ax2.plot(data2, "r--*", label = "step")
plt.legend()
plt.xlabel("stages")
plt.ylabel("values")
##############################################
ax3=fig.add_subplot(2,2,3)
ax3.scatter(np.arange(20), data3, label="Scater", color = "g", s=25, marker="*", alpha=0.9)
plt.legend(loc = "best")
plt.xlabel("stages")
plt.ylabel("values")
######## Histogram plot ##########
ax4 = fig.add_subplot(2,2,4)
ax4.hist(data4,bins=10,alpha=0.9,histtype="stepfilled",color="g",edgecolor="r", label="hist")
plt.legend(loc="best")
plt.xlabel("stages")
plt.ylabel("values")
plt.show()
plt.close()<jupyter_output><empty_output><jupyter_text>17 Jan<jupyter_code>x = np.linspace(-3, 3, 14)

y = 4*a*((x)**2)
x
y
y1 = np.arange(-10, 71, 1)
x1 = np.zeros(len(y1))
x2 = np.arange(-3,4,1)
y2 = np.zeros(len(x2))
plt.figure(figsize=(8,4))
plt.title("plotting Parabola")
plt.plot(x,y, "k-o", linewidth=2)
plt.plot(x1,y1, "r-", linewidth=2)
plt.plot(x2,y2, "r-", linewidth=2)
plt.xlabel("x-Values")
plt.ylabel("y-Value")
plt.show()
plt.close()<jupyter_output><empty_output><jupyter_text>Plotting with Pandas<jupyter_code>import pandas as pd
index = ["one", "two", "three", "four", "five", "six"]
columns = pd.Index(["a", "b", "c", "d"],name="Genus")
df = pd.DataFrame(np.arange(24).reshape(6,4)*2, index, columns)
df
plt.plot(df["a"])
plt.plot(df["b"])
plt.grid(False)

plt.plot(df["a"])
plt.plot(df["b"])
plt.grid(False)<jupyter_output><empty_output><jupyter_text>Bar plot<jupyter_code>df.plot.bar()
plt.ylabel("values")

df.plot.barh()
df.plot.barh(stacked=True, alpha=0.8)<jupyter_output><empty_output><jupyter_text>Ticks: sticks, yticks, xlim, sticklabels<jupyter_code>fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(data1, "k-", label="default")
#plt.plot(data1,linestyle="-", color="k")
ax.plot(data2,"ro-", label1="steps", alpha=0.9)
#plt.plot(data2,linestyle="--", color="r", marker="o")
ax.legend(loc="best", fontsize=12)
ax.set_title("my first matplotlib plot",fontsize=12, style="italic", fontweigh,"bold")
ax.set_xlabel("stages", fontsize=12)
#changing the size of figure, axes, grid, legend, xtick,ytick
plt.rc("figure", figsize=(9,4,5))
ticks=ax.set_xticks([0,2,4,6,8])
labels=ax.set_xticklabels(["zero","two","four", "six","eight"], rotation=30,fontsize=20)
plt.show()
plt.close()<jupyter_output><empty_output><jupyter_text>20 Jan<jupyter_code>df1 = pd.DataFrame(np.arange(50).reshape(10,5)*2, columns=["A", "B", "C", "D", "E"])
df1
c = {"boxes":"DarkRed", "whiskers":"DarkOrange", "medians":"DarkBlue", "caps":"Gray"}
df1.plot.box(color=c)
df1.plot.box(vert=False, positions=[1, 4, 5, 6, 8])<jupyter_output><empty_output><jupyter_text>Pie Plot<jupyter_code>index=["a", "b", "c", "d"]
columns=(["x", "y"])
df2 = pd.DataFrame(2* np.arange(8).reshape(4,2),index,columns)
df2
df2.plot.pie(subplots=True, figsize=(8,4))
df2.plot.pie(subplots=True,autopct="%.lf", fontsize=20, figsize=(12,12))
plt.subplots_adjust(wspace=0.5,hspace=0.5)
plt.plot(data1, "k-", label="Default")
plt.plot(data2, "r-*", label="Step")
plt.legend(loc="best", fontsize=12)
plt.title("My first matplotlib plot", fontsize=12, style="italic", fontweight="bold")
plt.axis([0,20,0,20])
plt.xticks(np.arange(0,21,2))
plt.yticks(np.arange(0,21,2))
plt.xlabel("Stages", fontsize=12)
plt.ylabel("Value", fontsize=12)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.25088102030542037, 'fraction_numerical': 0.05588185937237792, 'mean_word_length': 6.760416666666667, 'pattern_counts': {'":': 4, '<': 33, '<?xml version=': 0, '>': 34, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '189557', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 2844, 'n_tokens_neox': 2638, 'n_words': 458, 'path': 'Plotting.ipynb', 'repo_name': 'priyankakushi/machine-learning', 'size': '5959'}