<jupyter_start><jupyter_text>Functional method<jupyter_code>plt.plot(x,y)
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("Line Plot")
plt.savefig('line1.png',bbox_inches='tight')
plt.show()<jupyter_output><empty_output><jupyter_text>OOP method<jupyter_code>fig = plt.figure()
ax = fig.add_axes([0,0,1,1]) ## location and size of the figure [left,bottom,width,height] 
ax.set_xlabel("x label")
ax.set_ylabel("y label")
ax.set_title("Line Plot")
ax.plot(x,y)
plt.show()
fig.savefig('line2.png',bbox_inches="tight")<jupyter_output><empty_output><jupyter_text>Inset Line Plot<jupyter_code>fig = plt.figure()
ax = fig.add_axes([0,0,1,1]) ## location and size of the figure [left,bottom,width,height] 
ax.set_title("Outer plot")
ax.set_xlabel("x label")
ax.set_ylabel("y label")
ax.plot(x,y)
axSmall = fig.add_axes([0.6,0.2,0.3,0.3]) ## location and size of the figure [left,bottom,width,height] 
axSmall.set_title("Inner plot")
axSmall.set_xlabel("x label")
axSmall.set_ylabel("y label")
axSmall.plot(x,y)
plt.show()
fig.savefig('insetLine.png',bbox_inches="tight")<jupyter_output><empty_output><jupyter_text>Multiple Lines<jupyter_code>y1 = [0,20,50,70,80]
y2 = [0,40,60,80,100]
x = [1,2,3,4,5]
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.set_title("Growth Comparison")
ax.set_xlabel("Year")
ax.set_ylabel("Revenue(in Lakhs)")
ax.plot(x,y1)
ax.plot(x,y2)
plt.show()
fig.savefig('multipleLines.png',bbox_inches="tight") ##saves the image as exactly seen in jupyter notebook<jupyter_output><empty_output><jupyter_text>Adding the legend<jupyter_code>company_a = [0,20,50,70,80]
company_b = [0,40,60,80,100]
years = [1,2,3,4,5]
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.set_title("Growth Comparison")
ax.set_xlabel("Year")
ax.set_ylabel("Revenue(in Lakhs)")
ax.plot(years,company_a, label = 'Company A')
ax.plot(years,company_b, label = 'Company B')
ax.legend(loc = 'best')
plt.show()
fig.savefig('multipleLines.png',bbox_inches="tight")<jupyter_output><empty_output><jupyter_text>Subplots - Let's show the same data in two different plots in the same canvas<jupyter_code>company_a = [0,20,50,70,80]
company_b = [0,40,60,80,100]
years = [1,2,3,4,5]
fig, axes = plt.subplots(1,2)
axes[0].set_title("Growth Company A")
axes[0].set_xlabel("Year")
axes[0].set_ylabel("Revenue(in Lakhs)")
axes[0].plot(years,company_a,'y') ## y for yellow line
axes[1].set_title("Growth Company B")
axes[1].set_xlabel("Year")
axes[1].set_ylabel("Revenue(in Lakhs)")
axes[1].plot(years,company_b,'g') ## g for green line
##plt.show() overlaps the figure, so use the below one
plt.tight_layout()
fig.savefig('subplots.png',bbox_inches="tight")<jupyter_output><empty_output><jupyter_text>Grid using subplots<jupyter_code>company_a = [0,20,50,70,80]
company_b = [0,40,60,80,100]
years = [1,2,3,4,5]
fig, axes = plt.subplots(2,2)
axes[0][0].set_title("Growth Company A")
axes[0][0].set_xlabel("Year")
axes[0][0].set_ylabel("Revenue(in Lakhs)")
axes[0][0].plot(years,company_a,'y') ## y for yellow line
axes[0][1].set_title("Growth Company B")
axes[0][1].set_xlabel("Year")
axes[0][1].set_ylabel("Revenue(in Lakhs)")
axes[0][1].plot(years,company_b,'g') ## g for green line
axes[1][0].set_title("Growth Company A")
axes[1][0].set_xlabel("Year")
axes[1][0].set_ylabel("Revenue(in Lakhs)")
axes[1][0].plot(years,company_a,'y') ## y for yellow line
axes[1][1].set_title("Growth Company B")
axes[1][1].set_xlabel("Year")
axes[1][1].set_ylabel("Revenue(in Lakhs)")
axes[1][1].plot(years,company_b,'g') ## g for green line
##plt.show() overlaps the figure, so use the below one
plt.tight_layout()
fig.savefig('subplotsGrid.png',bbox_inches="tight")<jupyter_output><empty_output><jupyter_text>Line Plot with 2 different Y axis<jupyter_code>company_a_growth = [0,20,50,70,80]
company_a_employee = [4,10,20,40,200]
year = [1,2,3,4,5]
fig, axes = plt.subplots()
axes.plot(year, company_a_growth,'b')
axes.set_ylabel('Growth')
axes.set_xlabel('Time(in years)')
axesTwin = axes.twinx()
axesTwin.plot(year, company_a_employee, 'r')
axesTwin.set_ylabel('Employee Count')
plt.tight_layout()
fig.savefig('multipleScales.png',bbox_inches="tight")<jupyter_output><empty_output><jupyter_text>Bar plot - quarterly performance for last year<jupyter_code>company_a_quarterly = [20,30,10,40]
company_b_quarterly = [40,40,10,20]
positions = [10,15,20,25]
labels = ['Q1','Q2','Q3','Q4']
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.bar([pos - 0.5 for pos in positions],company_a_quarterly,1,align='center')
ax.bar([pos + 0.5  for pos in positions],company_b_quarterly,1,align='center')
ax.set_xticks(positions)
ax.set_xticklabels(labels)
ax.set_title('Quarterly Analysis')
ax.annotate('Minimum', xy=(20, 10), xytext=(20, 14), arrowprops=dict(facecolor='red', shrink=0.01))
ax.set_ylabel('No. of projects')
ax.set_xlabel('Quarters of the year 2019')
plt.show()
fig.savefig('barplot.png',bbox_inches="tight")<jupyter_output><empty_output><jupyter_text>Pie Chart<jupyter_code>departments = ['Development','Marketing','Sales']
company_a = [40,20,5]
company_b = [30,10,25]
plt.pie(company_a,labels=departments,explode=(0,0,0.1))
plt.title('Company A')
plt.show()
plt.pie(company_b,labels=departments,explode=(0,0.1,0))
plt.title('Company B')
plt.show()
departments = ['Development','Marketing','Sales']
company_a = [40,20,5]
company_b = [30,10,25]
fig, axes = plt.subplots(1,2)
_,_,autotextsA = axes[0].pie(company_a,labels=departments,autopct='%.1f%%',explode=(0,0,0.1)) ##exploding the lowest percentage
for text in autotextsA:
  text.set_color('white')
axes[0].set_title('Company A')
_,_,autotextsB = axes[1].pie(company_b,labels=departments,autopct='%.1f%%',explode=(0,0.1,0))
for text in autotextsB:
  text.set_color('white')
axes[1].set_title('Company B')
plt.tight_layout()
fig.savefig('pieplot.png',bbox_inches="tight")<jupyter_output><empty_output><jupyter_text>Stacked Bar Graph<jupyter_code>import numpy as np
departments = ['Development','Marketing','Sales']
development = np.array([40,30])
marketing = np.array([20,10])
sales = np.array([5,25])
company_names = ['Company A','Company B']
plt.bar(company_names,development,width= 0.4,label = 'Development',color='blue')
plt.bar(company_names,marketing,width= 0.4,bottom=development,label = 'Marketing',color='green')
plt.bar(company_names,sales,width= 0.4,bottom=development+marketing,label = 'Sales',color='orange')
plt.xlabel('Companies')
plt.ylabel('Number of employees')
plt.title('Workforce comparison')
plt.legend(loc='best')
plt.savefig('stackedbar.png',bbox_inches="tight")
plt.show()<jupyter_output><empty_output><jupyter_text>Scatter plotShows relations between samples<jupyter_code>import numpy as np

random_x = np.random.randint(low=0, high=200, size=100)
random_y = np.random.randint(low=0, high=200, size=100)
plt.scatter(x=random_x,y=random_y,color='y')
plt.xlabel('X label')
plt.ylabel('Y label')
plt.savefig('scatter.png',bbox_inches="tight")
plt.show()
Scatter plot with multiple colors
random_x_1 = np.random.randint(low=0, high=50, size=50)
random_x_2 = np.random.randint(low=50, high=100, size=50)
random_x_3 = np.random.randint(low=100, high=200, size=50)
random_y = np.random.randint(low=0, high=200, size=50)
plt.scatter(x=random_x_1,y=random_y,color='y')
plt.scatter(x=random_x_2,y=random_y,color='b')
plt.scatter(x=random_x_3,y=random_y,color='r')
plt.xlabel('X label')
plt.ylabel('Y label')
plt.savefig('scatterColors.png',bbox_inches="tight")
plt.show()<jupyter_output><empty_output><jupyter_text>Histogram<jupyter_code>x = np.random.normal(size = 500)
plt.hist(x, bins = 50) ## bins are the number of intervals we want to divide our samples into
plt.savefig('hist50.png',bbox_inches="tight")
plt.show()<jupyter_output><empty_output><jupyter_text>Bar plot with Error bars<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
a = np.array([10,12,14])
b = np.array([40,90,12])
c = np.array([23,27,31])
researchers = ['A','B','C']
means = [np.mean(a),np.mean(b),np.mean(c)]
standard_deviations = [np.std(c),np.std(b),np.std(c)]
plt.title('Bar plot with Error bars')
plt.bar(researchers,means,yerr=standard_deviations)
plt.savefig('errorBar.png',bbox_inches="tight")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18954328394759398, 'fraction_numerical': 0.04677360107750704, 'mean_word_length': 9.19725343320849, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '246740', 'license': 'Apache-2.0', 'n_tokens_mistral': 3681, 'n_tokens_neox': 3342, 'n_words': 505, 'path': 'DataVisualizationUsingMatplotlib/DataVisualizationUsingMatplotlib.ipynb', 'repo_name': 'hgayan7/Tutorials', 'size': '8167'}