Hosted by CoCalc
Download
Kernel: Python 3 (system-wide)
import matplotlib.pyplot as pd labels=['saheli','kesla','sukhtawa','podar'] sizes=[215,180,241,130] colors=['green','red','blue','cyan'] explode=(0.2,0,0,0) #explode 2nd slice pd.pie(sizes,explode=explode,labels=labels,colors=colors,shadow=True) pd.axis('equal') pd.show()
Image in a Jupyter notebook
import matplotlib.pyplot as np factories=['yadavproductions','patelorg','winefactory','milkianswaleorg'] sizes=[290,130,102,240] colors=['blue','lightgreen','grey','yellow'] explode=(0.1,0,0,0) #explode 1st slice np.pie(sizes,labels=factories,explode=explode,colors=colors,shadow=True) np.title('factories associated in our village') np.axis('equal') np.show()
Image in a Jupyter notebook
import matplotlib.pyplot as plt fig=plt.figure() ax=fig.add_axes([0,0,1,1]) languages=['c','c++','java','python','php'] students=[23,17,35,29,12] ax.bar(languages,students) plt.show()
Image in a Jupyter notebook
import numpy as np import matplotlib.pyplot as plt data=[[30,25,50,20],[40,23,51,17],[35,22,45,19]] x=np.arange(5) fig=plt.figure() ax=fig.add_axes([0,0,1,1]) ax.bar(x+0.15,data[1],color='blue',width=0.25) ax.bar(x+0.25,data[2],color='green',width=0.25) ax.bar(x+0.50,data[3],color='red',width=0.25) pyplot.legend() plt.show()
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_414/4132333676.py in <cell line: 7>() 5 fig=plt.figure() 6 ax=fig.add_axes([0,0,1,1]) ----> 7 ax.bar(x+0.15,data[1],color='blue',width=0.25) 8 ax.bar(x+0.25,data[2],color='green',width=0.25) 9 ax.bar(x+0.50,data[3],color='red',width=0.25) /usr/local/lib/python3.8/dist-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs) 1410 def inner(ax, *args, data=None, **kwargs): 1411 if data is None: -> 1412 return func(ax, *map(sanitize_sequence, args), **kwargs) 1413 1414 bound = new_sig.bind(ax, *args, **kwargs) /usr/local/lib/python3.8/dist-packages/matplotlib/axes/_axes.py in bar(self, x, height, width, bottom, align, **kwargs) 2340 yerr = self._convert_dx(yerr, y0, y, self.convert_yunits) 2341 -> 2342 x, height, width, y, linewidth, hatch = np.broadcast_arrays( 2343 # Make args iterable too. 2344 np.atleast_1d(x), height, width, y, linewidth, hatch) <__array_function__ internals> in broadcast_arrays(*args, **kwargs) /usr/local/lib/python3.8/dist-packages/numpy/lib/stride_tricks.py in broadcast_arrays(subok, *args) 536 args = [np.array(_m, copy=False, subok=subok) for _m in args] 537 --> 538 shape = _broadcast_shape(*args) 539 540 if all(array.shape == shape for array in args): /usr/local/lib/python3.8/dist-packages/numpy/lib/stride_tricks.py in _broadcast_shape(*args) 418 # use the old-iterator because np.nditer does not handle size 0 arrays 419 # consistently --> 420 b = np.broadcast(*args[:32]) 421 # unfortunately, it cannot handle 32 or more arguments directly 422 for pos in range(32, len(args), 31): ValueError: shape mismatch: objects cannot be broadcast to a single shape
Image in a Jupyter notebook
import numpy as np import matplotlib.pyplot as plt n=5 menmeans=(20,35,30,35,27) womenmeans=(25,32,34,20,25) ind=np.arange(n) #the x locations for the groups width=0.35 fig=plt.figure() ax=fig.add_axes([0,0,1,1]) ax.bar(ind,menmeans,width,color='r') ax.bar(ind,womenmeans,width,bottom=menmeans,color='b') ax.set_ylabel('scores') ax.set_title('scores by group and gender') ax.set_xticks(ind,('g1','g2','g3','g4','g5')) ax.set_yticks(np.arange(0,81,10)) ax.legend(labels=['men','women']) plt.show()
Image in a Jupyter notebook
from matplotlib import pyplot as plt import numpy as np fig,ax=plt.subplots(1,1) a=np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27]) ax.hist(a,bins=[0,25,50,75,100]) ax.set_title("histogram of result") ax.set_xticks([0,25,50,75,100]) ax.set_xlabel('marks') ax.set_ylabel('no. of students') plt.show()
Image in a Jupyter notebook
from matplotlib import pyplot as plt import numpy as np fig=plt.figure() ax=fig.add_axes([0,0,1,1]) ax.axis('equal') languages=['c','c++','java','python','php'] students=[23,17,35,29,12] ax.pie(students,labels=languages,autopct='%1.2f%%') plt.show()
Image in a Jupyter notebook
import matplotlib.pyplot as plt girls_grades=[89,90,70,89,100,80,90,100,80,34] boys_grades=[30,29,49,48,100,48,38,45,20,30] grades_range=[10,20,30,40,50,60,70,80,90,100] fig=plt.figure() ax=fig.add_axes([0,0,1,1]) ax.scatter(grades_range,girls_grades,color='r') ax.scatter(grades_range,boys_grades,color='b') ax.set_xlabel('grades range') ax.set_ylabel('grades scored') ax.set_title('scatter plot') plt.show()
Image in a Jupyter notebook
import numpy as np import matplotlib.pyplot as plt xlist=np.linspace(-3.0,3.0,100) ylist=np.linspace(-3.0,3.0,100) x,y=np.meshgrid(xlist,ylist) z=np.sqrt(x**2+y**2) fig,ax=plt.subplots(1,1) cp=ax.contourf(x,y,z) fig.colorbar(cp) #add a colorbar to a plot ax.set_title('filled contours plot') #ax.set_xlabel('x(cm)') ax.set_ylabel('y(cm)') plt.show()
Image in a Jupyter notebook
import matplotlib.pyplot as plt import numpy as np x,y=np.meshgrid(np.arange(-2,2,.2),np.arange(-2,2,.25)) z=x*np.exp(-x**2-y**2) v,u=np.gradient(z,.2,.2) fig,ax=plt.subplots() q=ax.quiver(x,y,u,v) plt.show()
Image in a Jupyter notebook
from mpl_toolkits import mplot3d import numpy as np import matplotlib.pyplot as plt fig=plt.figure() ax=plt.axes(projection='3d') z=np.linspace(0,1,100) x=z*np.sin(20*z) y=z*np.cos(20*z) c=x+y ax.scatter(x,y,z,c=c) ax.set_title('3d scatter plot') plt.show()
Image in a Jupyter notebook
from mpl_toolkits import mplot3d import numpy as np import matplotlib.pyplot as plt def f(x,y): return np.sin(np.sqrt(x**2+y**2)) x=np.linspace(-6,6,30) y=np.linspace(-6,6,30) x,y=np.meshgrid(x,y) z=f(x,y) fig=plt.figure() ax=plt.axes(projection='3d') ax.contour3D(x,y,z,50,cmap='binary') ax.set_xlabel('x') ax.set_ylabel('y') ax.set_zlabel('z') ax.set_title('3d contour') plt.show()
Image in a Jupyter notebook
from mpl_toolkits import mplot3d import numpy as np import matplotlib.pyplot as plt x=np.outer(np.linspace(-2,2,30),np.ones(30)) y=x.copy().T #transpose z=np.cos(x**2+y**2) fig=plt.figure() ax=plt.axes(projection='3d') ax.plot_surface(x,y,z,cmap='viridis',edgecolor='none') ax.set_title('surface plot') plt.show()
Image in a Jupyter notebook
print("hello matplotlib")
hello matplotlib
from matplotlib import pyplot as plt import numpy as np import math #needed for definition of pi x=np.arange(0,math.pi*2,0.05) y=np.sin(x) plt.plot(x,y) plt.xlabel("angle") plt.ylabel("sine") plt.title('sine wave') plt.show()
Image in a Jupyter notebook
print('hello anand')
hello anand
print('yadav')
yadav
import pandas as pd from matplotlib import pyplot as plt data.head() df=pd.dataframe(data) name=df['car'].head(12) price=df['price'].head(12) fig,ax=plt.subplots(figsize=(16,9)) ax.barh(name,price) for s in['top','bottom','left','right']: ax.spines[s].set_visible(false) ax.xaxis.set_tick_position('none') ax.yaxis.set_tick_position('none') ax.xaxis.set_tick_params(pad=5) ax.yaxis.set_tick_params(pad=10) ax.grid(b=True,color='grey',linestyle='-.',linewidth=0.5,alpha=0.2) ax.invert_yaxis() for i in ax.patches: plt.text(i.get_width()+0.2,i.get_y()+0.5,str(round((i.get_width()),2)),fontsize=10,fontweight='bold',color='grey') ax.set_title('sports car and their price in crore',loc='left',) fig.text(0.9,0.15,'AnandYadav03',fontsize=12,color='green',ha='right',va='bottom',alpha=0.7) plt.show()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_456/3659930258.py in <cell line: 4>() 2 from matplotlib import pyplot as plt 3 ----> 4 data.head() 5 df=pd.dataframe(data) 6 name=df['car'].head(12) NameError: name 'data' is not defined
import pandas as pd data={"calories":[480,320,290],"duration":[60,50,30]} df=pd.DataFrame(data) print(df) print(df.loc[0]) print(df.loc[[0,1]])
calories duration 0 480 60 1 320 50 2 290 30 calories 480 duration 60 Name: 0, dtype: int64 calories duration 0 480 60 1 320 50
import pandas as pd import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from pandas import DataFrame fig=plt.figure() fig=plt.figure(figsize=(12,8),dpi=80) ax=fig.add_subplot(111,projection='3d') pnt3d=ax.scatter3D(data_with_dummies_without_2017 ['Year'],data_with_dummies_without_2017['Machine'], data_with_dummies_without_2017['Grinding_Volume'],c=data_with_dummies_without_2017['Grinding_volume']) cbar=plt.color(pnt3d) cbar.set_label('Grinding Volume(cm3)') fig.set_facecolor('white') ax.set_facecolor('white') plt.xticks(np.arange(2018,2021,1)) plt.yticks(np.arange(1,3,1)) ax.set_xlabel('Year') ax.set_ylabel('Machine') ax.set_zlabel('Grinding Volume(cm3)') plt.show()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_619/1136005278.py in <cell line: 8>() 6 fig=plt.figure(figsize=(12,8),dpi=80) 7 ax=fig.add_subplot(111,projection='3d') ----> 8 pnt3d=ax.scatter3D(data_with_dummies_without_2017 9 ['Year'],data_with_dummies_without_2017['Machine'], 10 data_with_dummies_without_2017['Grinding_Volume'],c=data_with_dummies_without_2017['Grinding_volume']) NameError: name 'data_with_dummies_without_2017' is not defined
<Figure size 864x504 with 0 Axes>
Image in a Jupyter notebook