python如何使用Matplotlib的作图
关注公众号:Python爬虫数据分析挖掘,免费获取更多开源项目源码
Matplotlib官网 如果想了解更多可查看官网。
import numpy as np import matplotlib.pyplot as plt %matplotlib inline #写了这个就可以不用写plt.show() plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签 plt.rcParams['axes.unicode_minus']=False #用来正常显示负号 X = np.linspace(0, 2*np.pi,100)# 均匀的划分数据 Y = np.sin(X) Y1 = np.cos(X) plt.title("Hello World!!") plt.plot(X,Y) plt.plot(X,Y1)
X = np.linspace(0, 2*np.pi,100) Y = np.sin(X) Y1 = np.cos(X) plt.subplot(211) # 等价于 subplot(2,1,1) #一个图版画两个图 plt.plot(X,Y) plt.subplot(212) plt.plot(X,Y1,color = 'r')
柱状图
data = [5,25,50,20]
plt.bar(range(len(data)),data)
水平绘制柱状图
data = [5,25,50,20]
plt.barh(range(len(data)),data)
多个柱状图
data = [[5,25,50,20], [4,23,51,17], [6,22,52,19]] X = np.arange(4) plt.bar(X + 0.00, data[0], color = 'b', width = 0.25,label = "A") plt.bar(X + 0.25, data[1], color = 'g', width = 0.25,label = "B") plt.bar(X + 0.50, data[2], color = 'r', width = 0.25,label = "C") # 显示上面设置的 lable plt.legend()
叠加型柱状图
data = [[5,25,50,20], [4,23,51,17], [6,22,52,19]] X = np.arange(4) plt.bar(X, data[0], color = 'b', width = 0.25) plt.bar(X, data[1], color = 'g', width = 0.25,bottom = data[0]) plt.bar(X, data[2], color = 'r', width = 0.25,bottom = np.array(data[0]) + np.array(data[1])) plt.show()
散点图
N = 50 x = np.random.rand(N) y = np.random.rand(N) plt.scatter(x, y)
气泡图
N = 50 x = np.random.rand(N) y = np.random.rand(N) colors = np.random.randn(N) # 颜色可以用数值表示 area = np.pi * (15 * np.random.rand(N))**2 # 调整大小 plt.scatter(x, y, c=colors, alpha=0.5, s = area)
N = 50 x = np.random.rand(N) y = np.random.rand(N) colors = np.random.randint(0,2,size =50) plt.scatter(x, y, c=colors, alpha=0.5,s = area)
直方图
a = np.random.rand(100) plt.hist(a,bins= 20) plt.ylim(0,15)
a = np.random.randn(10000) plt.hist(a,bins=50) plt.title("标准正太分布")
箱线图
x = np.random.randint(20,100,size = (30,3)) plt.boxplot(x) plt.ylim(0,120) # 在x轴的什么位置填一个 label,我们这里制定在 1,2,3 位置,写上 A,B,C plt.xticks([1,2,3],['A','B','C']) plt.hlines(y = np.median(x,axis = 0)[0] ,xmin =0,xmax=3)
添加文字描述
# 设置画布颜色为 blue fig, ax = plt.subplots(facecolor='blue') # y 轴数据 data = [[5,25,50,20], [4,23,51,17], [6,22,52,19]] X = np.arange(4) plt.bar(X+0.00, data[0], color = 'darkorange', width = 0.25,label = 'A') plt.bar(X+0.25, data[1], color = 'steelblue', width = 0.25,label="B") plt.bar(X+0.50, data[2], color = 'violet', width = 0.25,label = 'C') ax.set_title("Figure 2") plt.legend() # 添加文字描述 方法一 W = [0.00,0.25,0.50] for i in range(3): for a,b in zip(X+W[i],data[i]): plt.text(a,b,"%.0f"% b,ha="center",va= "bottom") plt.xlabel("Group") plt.ylabel("Num") plt.text(0.0,48,"TEXT")
添加文字描述 方法二
X = np.linspace(0, 2*np.pi,100)# 均匀的划分数据 Y = np.sin(X) Y1 = np.cos(X) plt.plot(X,Y) plt.plot(X,Y1) plt.annotate('Points', xy=(1, np.sin(1)), xytext=(2, 0.5), fontsize=16, arrowprops=dict(arrowstyle="->")) plt.title("这是一副测试图!")
多个图形描绘 subplots
%pylab inline pylab.rcParams['figure.figsize'] = (10, 6) # 调整图片大小 # np.random.seed(19680801) n_bins = 10 x = np.random.randn(1000, 3) fig, axes = plt.subplots(nrows=2, ncols=2) ax0, ax1, ax2, ax3 = axes.flatten() colors = ['red', 'tan', 'lime'] ax0.hist(x, n_bins, normed=1, histtype='bar', color=colors, label=colors) ax0.legend(prop={'size': 10}) ax0.set_title('bars with legend') ax1.hist(x, n_bins, normed=1, histtype='bar', stacked=True) ax1.set_title('stacked bar') ax2.hist(x, n_bins, histtype='step', stacked=True, fill=False) ax2.set_title('stack step (unfilled)') # Make a multiple-histogram of data-sets with different length. x_multi = [np.random.randn(n) for n in [10000, 5000, 2000]] ax3.hist(x_multi, n_bins, histtype='bar') ax3.set_title('different sample sizes')
使用Pandas 绘图
import pandas as pd df = pd.DataFrame(np.random.rand(50, 2), columns=['a', 'b']) # 散点图 df.plot.scatter(x='a', y='b')
df = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d']) # 绘制柱状图 df.plot.bar()
# 堆积的柱状图 df.plot.bar(stacked=True)
# 水平的柱状图 df.plot.barh(stacked=True)
df = pd.DataFrame({'a':np.random.randn(1000)+1,'b':np.random.randn(1000),'c':np.random.randn(1000) - 1}, columns=['a', 'b', 'c']) # 直方图 df.plot.hist(bins=20)
# 箱线图 df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E']) df.plot.box()
耐得住寂寞,才能登得顶
Gitee码云:https://gitee.com/lyc96/projects