# In[1]
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
import numpy as np
plt.plot
and ax.plot
to produce line plots.# In[2]
x=np.linspace(0,10,30)
y=np.sin(x)
plt.plot(x,y,'o',color='black');
# In[3]
rng=np.random.default_rng(0)
for marker in ['o','.',',','x','+','v','^','<','>','s','d']:
plt.plot(rng.random(2),rng.random(2),marker,color='black',
label="marker='{0}'".format(marker))
plt.legend(numpoints=1,fontsize=13)
plt.xlim(0,1.8);
# In[4]
plt.plot(x,y,'-ok');
plt.plot
specify a wide range of properties of the lines and markers.# In[5]
plt.plot(x,y,'-p',color='gray',
markersize=15,linewidth=4,
markerfacecolor='white',
markeredgecolor='gray',
markeredgewidth=2)
plt.ylim(-1.2,1.2);
plt.plot
the primary workhorse for two-dimensional plots in Matplotlib.For more information about plt.plot
, refer to this url :
plt.plot documentation
plt.scatter
function, which can be used very similar to the plt.plot
function.# In[6]
plt.scatter(x,y,marker='o');
plt.scatter
from plt.plot
is that it can be used to create scatter plots where the properties of each individual point can be individually controlled or mapped to data.alpha
keyword to adjust the transparency(투명도) level.#In[7]
rng=np.random.default_rng(0)
x=rng.normal(size=100)
y=rng.normal(size=100)
colors=rng.random(100)
sizes=1000*rng.random(100)
plt.scatter(x,y,c=colors,s=sizes,alpha=0.3)
plt.colorbar(); # show color scale
colorbar
command), and that the size argument is given in pixels.# In[8]
from sklearn.datasets import load_iris
iris=load_iris()
features=iris.data.T
plt.scatter(features[0],features[1],alpha=0.4,
s=100*features[3],c=iris.target,cmap='viridis')
plt.xlabel(iris.feature_names[0])
plt.ylabel(iris.feature_names[1]);
plt.plot
can be noticeably more efficient than plt.scatter
.plt.scatter
has the capability to render a different size and/or color for each point, so the renderer must do the extra work of constructing each point individually.plt.plot
, the markers for each point are guaranteed to be identical, so the work of determining the appearance of the points is done only once for the entire set of data.# In[9]
x=np.linspace(0,10,50)
dy=0.8
y=np.sin(x)+dy*np.random.randn(50)
plt.errorbar(x,y,yerr=dy,fmt='.k');
fmt
is a format code controlling the appearance of lines and points, and it has the same syntax as the shorthand used in plt.plot
.errorbar
function has many options to fine-tune(미세 조정하다) the outputs.# In[10]
plt.errorbar(x,y,yerr=dy,fmt='o',color='black',
ecolor='lightgray',elinewidth=3,capsize=0);
For more information about plt.errorbar
, refer to this url :
plt.errorbar documentation
plt.plot
and plt.fill_between
for a useful result.# In[11]
from sklearn.gaussian_process import GaussianProcessRegressor
# define the model and draw some data
model=lambda x: x*np.sin(x)
xdata=np.array([1,3,5,6,8])
ydata=model(xdata)
# compute the gaussian process fit
gp=GaussianProcessRegressor()
gp.fit(xdata[:,np.newaxis],ydata)
xfit=np.linspace(0,10,1000)
yfit,dyfit=gp.predict(xfit[:,np.newaxis],return_std=True)
xfit
, yfit
, and dyfit
, which sample the continuous fit to our data.plt.errorbar
function as in the previous section, but we don't really want to plot 1,000 points with 1,000 errorbars.plt.fill_between
function with a light color to visualize this continuous error.# In[12]
plt.plot(xdata,ydata,'or')
plt.plot(xfit,yfit,'-',color='gray')
plt.fill_between(xfit,yfit-dyfit,yfit+dyfit,color='gray',alpha=0.2)
plt.xlim(0,10);
fill_between
call signature: we pass an x value, then the lower y bound, then the upper y bound, and the result is that the area between these regions is filled.For more information about plt.fill_between
function, refer to this url :
plt.fill_between documentation
And this is about plt.fill
that is the closely related with plt.fill_between
:
plt.fill documentation