Python 您所在的位置:网站首页 python如何在绘图并添加文字 Python

Python

#Python | 来源: 网络整理| 查看: 265

最近有点闲暇时间,趁时学习一下Python,搭建好Python环境,环境搭建就不多说,网上很多教程,我使用的anaconda。然后开始了coding之路

import numpy as np import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties font_set = FontProperties(fname='/Library/Fonts/Songti.ttc', size=15) x1 = [2011, 2012, 2013, 2014, 2015]# Make x, y arrays for each graph y1 = [100, 423, 934, 1600, 2542] x2 = [2011, 2012, 2014, 2016, 2018] y2 = [245, 466, 877, 1234, 1646] #创建图并命名 plt.figure('chatView') ax = plt.gca() #设置x轴、y轴名称 ax.set_xlabel(u"年",fontproperties=font_set) ax.set_ylabel(u"收入",fontproperties=font_set) # 折线图 ax.plot(x1,y1,label=u"1111", color="r", linewidth=2, alpha=0.6) ax.plot(x2,y2,label=u"2222", color='g', linewidth=2, alpha=0.6) #柱状图 ax.bar(x1,y1,0.5,color = 'orange',alpha = 0.5) ax.bar(x2,y2,0.5,color = 'y',alpha = 0.5) #离散点 ax.scatter(x1,y1,c='r',s=20,alpha=1) ax.scatter(x2,y2,c='g',s=20,alpha=1) #显示图示 ax.legend() plt.show()

运行效果如下图:

import numpy as np import matplotlib.pyplot as plt x = np.linspace(-3,3,50) #从-1-----1之间等间隔采50个数 y = 2*x + 1; plt.figure('y=2x+1') ax = plt.gca() ax.plot(x,y,label='y=2x+1',color='r',alpha=1) ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.spines['bottom'].set_position(('data', 0)) ax.spines['left'].set_position(('data', 0)) # 设置坐标轴的lable #标签里面必须添加字体变量:fontproperties='SimHei',fontsize=14。不然可能会乱码 # plt.xlabel(u'$这是x轴$') # plt.ylabel(u'$这是y轴$') x0 = 1 y0 = 2*x0 + 1 # 绘制(x0, y0)点 plt.scatter(x0, y0, s = 50, color = 'blue') plt.plot([x0,x0],[y0,0],'k--',lw=2.5,color='r') plt.plot([x0,0],[y0,y0],'k--',lw=2.5,color='r') plt.annotate(r'$2 * x + 1 = %s$' % y0, xy = (x0, y0), xycoords = 'data', xytext = (+30, -30), \              textcoords = 'offset points', fontsize = 16, arrowprops = dict(arrowstyle = '->',\                                                                             connectionstyle = 'arc3, rad = .2',color='r')) y = x**2 plt.plot(x,y,label='y=x^2',color = 'g',alpha=1,linewidth=2,linestyle='--') x = -2 y = x**2 plt.scatter(x,y,s=50,color='g') plt.plot([x,x],[y,0],'--',lw=2.5,color='g') plt.plot([x,0],[y,y],'--',lw=2.5,color='g') ax.legend() plt.show()

运行效果图:



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有