美文网首页程序员
小白python自学者的Matplotlib制图笔记(二)-4

小白python自学者的Matplotlib制图笔记(二)-4

作者: meidanzuo | 来源:发表于2017-02-28 22:10 被阅读0次

1 我画出来的图:


2 运行代码在此:



3 简略讲解版本:

#导入库

import matplotlib.pyplot as plt

import numpy as np

#设置各部分数据

x = np.linspace(0,10,100)

y = np.cos(x)

z = np.sin(x)

data = 2 * np.random.random((10,10))

data2 = 3 * np.random.random((10,10))

Y,X = np.mgrid[-3:3:100j,-3:3:100j]

U = -1 -X**2+ Y

V = 1 + X - Y**2

#建立子图 带入xy

fig,ax = plt.subplots()

lines = ax.plot(x,y)

#建立散点图,进行设置

ax.scatter(x,y,marker=".")

ax.plot(x,y,marker="o")

#设置

plt.plot(x,y,linewidth=4.0)

plt.plot(x,y,ls='solid')

plt.plot(x,y,ls='--')

plt.plot(x,y,'--',x**2,y**2,'-.')

plt.setp(lines,color='r',linewidth='4.0')

#设置标签

ax.text(1,

        -2.1,

        'Example Graph',

        style='italic')

#设置

ax.annotate("Sine",

            xy=(8,0),

            xycoords='data',

            xytext=(10.5,0),

            textcoords='data',

            arrowprops=dict(arrowstyle="->",connectionstyle="arc3"),)

#设置标题          

plt.title(r'$sigma_i=15$',fontsize=20)            

ax.margins(x=0.0,y=0.1) 

ax.axis('equal')

ax.set(xlim=[0,10.5],ylim=[-1.5,1.5])

ax.set_xlim(0,10.5)

#设置标签

ax.set(title='An Example Axes',

       ylabel='Y-Axis',

       xlabel='X-Axis')

ax.legend(loc='best')

ax.xaxis.set(ticks=range(1,5),

             ticklabels=[3,100,-12,"foo"])

ax.tick_params(axis='y',

               direction='inout',

               length=10)

#自动调节subplot 参数进行指定填充

fig.tight_layout()

#显示

plt.show()

#关闭

plt.cla()

plt.clf()

plt.close()


4 详细注释版本:

#导入numpy库用来科学计算,matplotlib库画图

import matplotlib.pyplot as plt

import numpy as np

'''调用了numpy的linspace()建立了了一个数组,

其参数的含义分别是开始值,终止值,创建元素个数,

往往最后可能会有一个endpoint=False,表示最后一个值是否被包含,不写默认为True.

类似于:np.linspace(0,10,100,endpoint=False)的格式'''

x = np.linspace(0,10,100)

#并把这100个值赋予X。y,z分别是cosine和sine值(x,y,z都是numpy数组)

#此处可参考http://www.jianshu.com/p/7fbecf5255f0

y = np.cos(x)

z = np.sin(x)

'''np.random.random()返回随机的浮点数,在半开区间 [0.0, 1.0),

data指画出一个10*10形状的二维数组,由范围 [0.0, 1.0)的随机数组成,

并且每个随机数都要*2 data2则表示*3'''

data = 2 * np.random.random((10,10))

data2 = 3 * np.random.random((10,10))

'''np.mgrid()用于返回多维结构,np.mgrid[ 第1维,第2维 ,第3维 , …]

一维:eg:np.mgrid[-1:1:5j]

array([-1. , -0.5,  0. ,  0.5,  1. ])

第一个参数是初始值,第二个为终止值,第三个为参数个数,猜测j代表横坐标或者纵坐标?

不理解二维多维数组,直到我找到了这篇文章:

http://www.cnblogs.com/NanShan2016/p/5491200.html,

k,b=np.mgrid[1:3:3j,4:6:3j]

可以这么理解:

k轴范围为1~3,b轴范围为4~6:

k与b为咱们相关的x,y轴

【step1:k扩展】(朝右扩展):

[1 1 1]

[2 2 2]

[3 3 3]

【step2:b扩展】(朝下扩展):

[4 5 6]

[4 5 6]

[4 5 6]

【step3:定位(ki,bi)】(把上面的k、b联合起来):

[(1,4) (1,5) (1,6)]

[(2,4) (2,5) (2,6)]

[(3,4) (3,5) (3,6)]

啊 这不就是咱么理解的横纵坐标吗'''

Y,X = np.mgrid[-3:3:100j,-3:3:100j]

#此处是对X,Y坐标进行运算

U = -1 -X**2+ Y

V = 1 + X - Y**2

#创建子图,散点图

fig,ax = plt.subplots()

ax.scatter(x,y,marker=".")

ax.plot(x,y,marker="o")

#设置子图数据

plt.plot(x,y,linewidth=4.0)

plt.plot(x,y,ls='solid')

plt.plot(x,y,ls='--')

plt.plot(x,y,'--',x**2,y**2,'-.')

#对artist 对象设置属性,lines为之前设置的对象,setp函数可以对多条线进行设置的

plt.setp(lines,color='r',linewidth='4.0')

#在轴上添加文本

ax.text(1,

-2.1,

'Example Graph',

style='italic')

#用箭头在指定的一个数据点创建一个注释或一段文本

ax.annotate("Sine",

xy=(8,0),

xycoords='data',

xytext=(10.5,0),

textcoords='data',

arrowprops=dict(arrowstyle="->",connectionstyle="arc3"),)

#设置当前axes 标题

plt.title(r'$sigma_i=15$',fontsize=20)

#设置或检索自动缩放功能

ax.margins(x=0.0,y=0.1)

#获取或设置轴属性的便捷方法

ax.axis('equal')

#设置x,y轴的范围

ax.set(xlim=[0,10.5],ylim=[-1.5,1.5])

ax.set_xlim(0,10.5)

#设置标题和x,y轴的标签

ax.set(title='An Example Axes',

ylabel='Y-Axis',

xlabel='X-Axis')

#Legend 对象列表,用于显示图示

ax.legend(loc='best')

#设置刻度标签

ax.xaxis.set(ticks=range(1,5),

ticklabels=[3,100,-12,"foo"])

#改变刻度及刻度标签外观

ax.tick_params(axis='y',

direction='inout',

length=10)

#调整subplot布局

fig3.subplots_adjust(wspace=0.5,

hspace=0.3,

left=0.125,

right=0.9,

top=0.9,

bottom=0.1)

#自动调节subplot 参数进行指定填充

fig.tight_layout()

#spines 是连接轴刻度标记的线,而且标明了数据区域的边界

ax1.spines['top'].set_visible(False)

ax1.spines['bottom'].set_position(('outward',10))

#保存

plt.savefig('foo.png')

plt.savefig('foo.png',transparent=True)

#显示

plt.show()

#清除当前axes

plt.cla()

#清除当前figure

plt.clf()

#关闭figure 窗口。

plt.close()



我的代码是从下面的网址中抄下来运行的,当时不知道干嘛的,只是为了熟悉Matplotlib。我只能保证注释大体正确吧,有问题可以指出啊 ,我就是想要大家告诉我答案呀! 加油呀↖(^ω^)↗ 米娜桑

mp.weixin.qq.com/s/qNdYnM-GV8WHgj9hLEKEtg

相关文章

网友评论

    本文标题:小白python自学者的Matplotlib制图笔记(二)-4

    本文链接:https://www.haomeiwen.com/subject/dhpfgttx.html