码迷,mamicode.com
首页 > 移动开发 > 详细

python mapplotlib

时间:2015-10-01 17:55:16      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:

http://liam0205.me/2014/09/11/matplotlib-tutorial-zh-cn/   这是讲解比较详细的样列

 

自己做的一些笔记

# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright (c) 2015, Nicolas P. Rougier. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
# -----------------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt
"""
#dpi 图像分辨率
plt.figure(figsize=(8,5), dpi=80)
ax = plt.subplot(111)
#图像四周的框框
ax.spines[‘right‘].set_color(‘none‘)
ax.spines[‘top‘].set_color(‘none‘)
#设置横轴与框的位置
ax.xaxis.set_ticks_position(‘bottom‘)
ax.spines[‘bottom‘].set_position((‘data‘,0))
#设置纵轴与框的位置
ax.yaxis.set_ticks_position(‘left‘)
#设置框的中心点位置与轴的关系
ax.spines[‘left‘].set_position((‘data‘,0))

#得到一个均匀分度的list 把-pi 到pi
X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X)

plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-", label="cosine")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-", label="sine")

#左右边界
plt.xlim(X.min()*1.1, X.max()*1.1)

#设置坐标值
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
[r‘$-\pi$‘, r‘$-\pi/2$‘, r‘$0$‘, r‘$+\pi/2$‘, r‘$+\pi$‘]) #LaTex

plt.ylim(C.min()*1.1,C.max()*1.1)
plt.yticks([-1, +1],
[r‘$-1$‘, r‘$+1$‘])

#对两函数标注的位置
plt.legend(loc=‘upper left‘, frameon=False)
# plt.savefig("../figures/exercice_8.png",dpi=72)
plt.show()
"""

"""
# -----------------------------------------------------------------------------
# Copyright (c) 2015, Nicolas P. Rougier. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
# -----------------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt

plt.figure(figsize=(8,5), dpi=80)
ax = plt.subplot(111)
ax.spines[‘right‘].set_color(‘none‘)
ax.spines[‘top‘].set_color(‘none‘)
ax.xaxis.set_ticks_position(‘bottom‘)
ax.spines[‘bottom‘].set_position((‘data‘,0))
ax.yaxis.set_ticks_position(‘left‘)
ax.spines[‘left‘].set_position((‘data‘,0))

X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X)

plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-", label="cosine")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-", label="sine")

plt.xlim(X.min()*1.1, X.max()*1.1)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
[r‘$-\pi$‘, r‘$-\pi/2$‘, r‘$0$‘, r‘$+\pi/2$‘, r‘$+\pi$‘])

plt.ylim(C.min()*1.1,C.max()*1.1)
plt.yticks([-1, +1],
[r‘$-1$‘, r‘$+1$‘])

t = 2*np.pi/3
plt.plot([t,t],[0,np.cos(t)],
color =‘blue‘, linewidth=1.5, linestyle="--")
#100 表示点的大小
plt.scatter([t,],[np.cos(t),], 100, color =‘blue‘)

#LaTex
plt.annotate(r‘$\sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$‘,
xy=(t, np.sin(t)), xycoords=‘data‘,
xytext=(+10, +30), textcoords=‘offset points‘, fontsize=16,
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))

plt.plot([t,t],[0,np.sin(t)],
color =‘red‘, linewidth=1.5, linestyle="--")
plt.scatter([t,],[np.sin(t),], 50, color =‘red‘)
# xy需要标注点的位置 xytext 注释位置 textcoords注释位置的坐标系是什么
plt.annotate(r‘$\cos(\frac{2\pi}{3})=-\frac{1}{2}$‘,
xy=(t, np.cos(t)), xycoords=‘data‘,
xytext=(-90, -50), textcoords=‘offset points‘, fontsize=16,
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontsize(16)
label.set_bbox(dict(facecolor=‘white‘, edgecolor=‘None‘, alpha=0.65 ))
plt.legend(loc=‘upper left‘, frameon=False)
#plt.savefig("../figures/exercice_9.png",dpi=72)
plt.show()
"""
"""
from pylab import *

axes([0.1,0.1,.8,.8])
xticks([]), yticks([])
text(0.6,0.6, ‘axes([0.1,0.1,.8,.8])‘,ha=‘center‘,va=‘center‘,size=20,alpha=.5)

axes([0.2,0.2,.3,.3])
xticks([]), yticks([])
text(0.5,0.5, ‘axes([0.2,0.2,.3,.3])‘,ha=‘center‘,va=‘center‘,size=16,alpha=.5)

# plt.savefig("../figures/axes.png",dpi=64)
show()
"""
"""
# -----------------------------------------------------------------------------
# Copyright (c) 2015, Nicolas P. Rougier. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
# -----------------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt

n = 256
X = np.linspace(-np.pi,np.pi,n,endpoint=True)
Y = np.sin(2*X)

plt.axes([0.025,0.025,0.95,0.95])

plt.axes()
plt.plot (X, Y+1, color=‘blue‘, alpha=1.00)
plt.fill_between(X, 1, Y+1, color=‘blue‘, alpha=.25)

plt.plot (X, Y-1, color=‘blue‘, alpha=1.00)
plt.fill_between(X, -1, Y-1, (Y-1) > -1, color=‘blue‘, alpha=.25)
plt.fill_between(X, -1, Y-1, (Y-1) < -1, color=‘red‘, alpha=.25)

plt.xlim(-np.pi,np.pi), plt.xticks([])
plt.ylim(-2.5,2.5), plt.yticks([])
# savefig(‘../figures/plot_ex.png‘,dpi=48)
plt.show()
"""
# -----------------------------------------------------------------------------
# Copyright (c) 2015, Nicolas P. Rougier. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
# -----------------------------------------------------------------------------
#柱状图
import numpy as np
import matplotlib.pyplot as plt

n = 12
X = np.arange(n)
Y1 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n)
Y2 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n)

plt.axes([0.025,0.025,0.95,0.95])
plt.bar(X, +Y1, facecolor=‘#9999ff‘, edgecolor=‘white‘)
plt.bar(X, -Y2, facecolor=‘#ff9999‘, edgecolor=‘white‘)

for x,y in zip(X,Y1):
plt.text(x+0.4, y+0.05, ‘%.2f‘ % y, ha=‘center‘, va= ‘bottom‘)

for x,y in zip(X,Y2):
plt.text(x+0.4, -y-0.05, ‘%.2f‘ % y, ha=‘center‘, va= ‘top‘)

plt.xlim(-.5,n), plt.xticks([])
plt.ylim(-1.25,+1.25), plt.yticks([])

# savefig(‘../figures/bar_ex.png‘, dpi=48)
plt.show()

python mapplotlib

标签:

原文地址:http://www.cnblogs.com/ChenAlong/p/4851225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!