1.plot 绘制直线图
import matplotlib.pyplot as plt
import matplotlib
x = [1,2,3]
y = [4,5,6]
plt.plot(a,b)
plt.show()
每次都执行plt.show()
才显示图像,比较麻烦,我们运行一下代码%matplotlib inline
这样以后用plt.plot()
就可以显示图像了,下面代码承接上文代码:
plt.plot(x,y,'r--',label="aaa")
plt.legend() # 配合plt.plot(label="aaa")用于显示图例
matplotlib.rcParams['font.family']='SimHei'
matplotlib.rcParams['font.size'] = 12
plt.xlabel("x 轴")
plt.ylabel("y 轴")
-
plt.plot()
- 第一参数为x轴的值,第二个参数为y轴的值
- 第三个参数指明图像线的属性(本例中是用红色的虚线)
- 第四个参数指明图像中的线是指什么,即图例(plt.legend()用于将图例显示在图像上)
-
plt.xlabel(“x 轴”) 设置x,y轴名称
2. subplot 子图
plt.subplot(211)
或plt.subplot(2,1,1)
其实就是对图做了切分,现在展示的是一个2行1列的,最后的参数指明是哪一个图plt.subplot(2,1,1)就是第一个,plt.subplot(2,1,2)就是第二个
1.绘制折线图,并给出解决中文无法显示的问题
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
#方法一,改变全局字体
matplotlib.rcParams['font.family']='SimHei' # 'fangsong'
matplotlib.rcParams['font.size'] = 12
matplotlib.rcParams['axes.unicode_minus']=False #用来正常显示负号
plt.subplot(211)
plt.plot([3,1,4,5,2],label = '报考人数') #添加label时要用legend()函数
plt.legend(loc = "best")
plt.xlabel("横轴")
plt.ylabel("纵轴(值)")
#方法二,plt.xlabel()内增加fontproperties,fontsize
plt.subplot(212)
x = np.arange(0.0,5.0,0.02)
y = np.cos(2*np.pi*x)
plt.xlabel('横轴,时间',fontproperties='simhei',fontsize = 20)
plt.ylabel('纵轴,时间',fontproperties='simhei',fontsize= 10)
plt.plot(x,y,'r--')
plt.show()
绘制直方图
import matplotlib.pyplot as plt
import numpy as np
# 生成需要显示的数据
np.random.seed(0)
mu,sigma = 100,20
a = np.random.normal(mu,sigma,size = 100)
#第二个参数为直方图的个数,histtype = 'stepfilled'设置柱子之间颜色间隙
plt.hist(a, bins=10, normed=0, histtype = 'stepfilled',facecolor="blue", edgecolor="black", alpha=0.7)
# plt.hist(a,10,normed = 1,histtype = 'stepfilled',facecolor = 'b',alpha = 0.75)
plt.title('Histgram')
plt.show()
plt.hist()
中参数rwidth=0.9
可调节柱之间的间距
新版本不用normed ,改用density
.
plt.hist(a, bins=10, density = 1, facecolor="blue", edgecolor="black", alpha=0.7)
改进前
改进后(2019-7-22)
绘制散点图
https://www.cnblogs.com/sunshinewang/p/6853813.html
import numpy as np
import matplotlib.pyplot as plt
N = 1000
x = np.array([0,1,2,3,4,5,6,7,8,9])
y = np.array([1,1,1,-1,-1,-1,1,1,1,-1])
plt.scatter(x, y)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
N = 1000
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.array([5.56, 5.70, 5.91, 6.4, 6.8, 7.05, 8.9, 8.7, 9, 9.05])
plt.scatter(x, y)
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.array([5.72, 5.72, 5.72, 6.46, 6.46, 6.46, 9.13, 9.13, 9.13, 9.13])
plt.scatter(x, y, c='red')
plt.show()
解决中文无法显示&&负号无法显示的问题
方法一,改变全局字体
import matplotlib
matplotlib.rcParams['font.family']='SimHei' # 'fangsong'
matplotlib.rcParams['font.size'] = 12
matplotlib.rcParams['axes.unicode_minus']=False #用来正常显示负号
方法二,plot()内增加fontproperties,fontsize
plt.xlabel('横轴,时间',fontproperties='simhei',fontsize = 20)
plt.ylabel('纵轴,时间',fontproperties='simhei',fontsize= 10)
绘制3D图像
https://blog.csdn.net/shu15121856/article/details/72590620#
热力图
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.set(style='whitegrid', color_codes=True)
data = np.array([[0,0,0,1],[0,0,0,1],[0,0,0,0],[0,0,0,0]])
sns.heatmap(data)