# In[1]
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
import numpy as np
# In[2]
fig=plt.figure()
ax=plt.axes()
ax.plot
method to plot some data.# In[3]
fig=plt.figure()
ax=plt.axes()
x=np.linspace(0,10,100)
ax.plot(x,np.sin(x));
# In[4]
plt.plot(x,np.sin(x))
plot
function multiple times.# In[5]
plt.plot(x,np.sin(x))
plt.plot(x,np.cos(x))
plt.plot
function takes additional arguments that can be used to specify the line colors and styles.color
keyword, which accpets a string argument representing virtually any imaginable color.# In[6]
plt.plot(x,np.sin(x-0), color='blue') # specify color by name
plt.plot(x,np.sin(x-1), color='g') # short color code(rgbmyk)
plt.plot(x,np.sin(x-2), color='0.75') # grayscale between 0 and 1
plt.plot(x,np.sin(x-3), color='#FFDD44') # hex code(RRGGBB, 00 to FF)
plt.plot(x,np.sin(x-4), color=(1.0,0.2,0.3)) # RGB tuple, values 0 to 1
plt.plot(x,np.sin(x-5), color='chartreuse') # HTML color names supported
linestyle
keyword.# In[7]
plt.plot(x,x+0, linestyle='solid')
plt.plot(x,x+1, linestyle='dashed')
plt.plot(x,x+2, linestyle='dashdot')
plt.plot(x,x+3, linestyle='dotted')
# For short, you can use the following codes
plt.plot(x,x+4, linestyle='-')
plt.plot(x,x+5, linestyle='--')
plt.plot(x,x+6, linestyle='-.')
plt.plot(x,x+7, linestyle=':')
linestyle
and color
codes into a single non-keyword argument to the plt.plot
function.# In[8]
plt.plot(x,x+0,'-g') # solid green
plt.plot(x,x+1,'--c') # dashen cyan
plt.plot(x,x+2,'-.k') # dashdot black
plt.plot(x,x+3,':r') # dotted red
plt.xlim
and plt.yilm
functions.# In[9]
plt.plot(x,np.sin(x))
plt.xlim(-1,11)
plt.ylim(-1.5,1.5);
# In[10]
plt.plot(x,np.sin(x))
plt.xlim(10,0)
plt.ylim(1.2,-1.2);
plt.axis
, which allows more qualitative specifications of axis limits.# In[11]
plt.plot(x,np.sin(x))
plt.axis('tight');
x
is visually equivalent to one unit in y
.# In[12]
plt.plot(x,np.sin(x))
plt.axis('equal');
on
, off
, square
,image
, and more. For more information on these, refer to the plt.axis documentation# In[13]
plt.plot(x,np.sin(x))
plt.title("A Sine Curve")
plt.xlabel("x")
plt.ylabel("sin(x)");
plt.legend
method.# In[14]
plt.plot(x,np.sin(x),'-g',label='sin(x)')
plt.plot(x,np.cos(x),':b',label='cos(x)')
plt.axis('equal')
plt.legend();
plt.legend
function keeps track of the line style and color, and matches these with the correct label.For more information about plt.legend
, refer to this url :
plt.legend documentation
plt
functions translate directly to ax
methods, this is not the case for all commands.plt.xlabel
=> ax.set_xlabel
plt.ylabel
=> ax.set_ylabel
plt.xlim
=> ax.set_xlim
plt.ylim
=> ax.set_ylim
plt.title
=> ax.set_title
ax.set
method to set all these properties at once.# In[15]
ax=plt.axes()
ax.plot(x,np.sin(x))
ax.set(xlim=(0,10),ylim=(-2,2),
xlabel='x',ylabel='sin(x)',
title='A simple plot');
좋은 정보 얻어갑니다, 감사합니다.