import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5],[1,4,9,16,25])
plt.show()
import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5],[1,4,9,16,25],'--') #关键字为linestyle
plt.show()
plt.plot([1,2,3,4,5],[2,3,4,5,6],'--',color='r')
import numpy as np
import matplotlib.pyplot as plt
x = linspace(-10,10,100)
y = np.sin(x)
plt.plot(x,y,linewidth=2.0) #默认为1.0
plt.show()
plt.plot(x,y,alpha=0.3) #值越小,越透明
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-10,10,100)
y = np.sin(x)
line = plt.plot(x,y)
plt.setp(line, color='r', linewidth=2, alpha=0.5)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
x = linspace(-10,10,100)
y = np.cos(x)
plt.plot(x,y,marker='o')
plt.show()
plt.plot(x,y,markerfacecolor='r')
plt.plot(x,y,markersize=10)
一张完整的图表包括:标题,坐标轴标签,注释
import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5],[1,4,9,16,25])
plt.xlabel('xlabel')
plt.ylabel('ylabel')
plt.show()
plt.xlabel('xlabel',fontsize=16)
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1,10,100)
y = np.sin(x)
plt.plot(x,y,color='b',fontsize=16)
plt.title('Neo')
plt.show()
plt.text(0,0,'到此一游')
plt.grid(True)
plt.annotate('Neo is here', xy =(-5,0), xytext=(2,0.3),arrowprops=dict{facecolor='black',shrink=0.05})
plt.style.available
所有自带绘图风格汇总
个人比较喜欢’ggplot’
import matplotlib.pyplot as plt
import numpy as np
x = linspace(-10,10,100)
y = np.sin(x)
plt.plot(x,y)
plt.style.use('dark_background')
plt.show()
import matplotlib.pyplot as plt
import numpy as np
x = linspace(-10,10,100)
y = np.sin(x)
plt.xkcd() #绘制手绘风格
plt.plot(x,y)
plt.show()
例1:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10,10)
y = np.sin(x)
# 211表示一会要画的图是2行1列的 最后一个1表示的是子图当中的第1个图,这个1是自然计数方式
plt.subplot(211)
plt.plot(x,y,color='r')
# 212表示一会要画的图是2行1列的 最后一个1表示的是子图当中的第2个图
plt.subplot(212)
plt.plot(x,y,color='b')
plt.show()
例2:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10,10)
y = np.sin(x)
plt.subplot(321)
plt.plot(x,y,color='r')
plt.subplot(322)
plt.plot(x,y,color='b')
plt.show()
(此法不能用于sns)
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(0)
x = np.arange(5)
y = np.random.randint(-5,5,5)
fig,axes = plt.subplots(ncols = 2, nrows=2)
# [1][0] 代表第1行第0列
v_bars = axes[1][0].bar(x,y,color='red')
# [0][1]代表第0行第1列
h_bars = axes[0][1].barh(x,y,color='red')
plt.show()
如何调整窗口尺寸大小?