Matplotlib.pyplot 使用

import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
plt.plot(x, y)
plt.show()

设置折线点的属性

import numpy as np
import matplotlib.pyplot as plt

# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)

# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^-')
plt.show()

解决中文编码问题

import matplotlib.font_manager as fm
myfont = fm.FontProperties(fname='C:/Windows/Fonts/msyh.ttc')

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [1, 4, 9, 16]

plt.plot(x, y, 'rs-')

plt.xlabel('横坐标轴', fontproperties=myfont, fontsize=30, color='black')
plt.ylabel('纵坐标轴', fontproperties=myfont, fontsize=30, color='black')
plt.title("解决中文编码问题",fontproperties=myfont, fontsize=28, color='black')

plt.show()

在点上面打标签

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [1, 4, 9, 16]

plt.plot(x, y, 'rs-')
for xy in zip(x, y):
    plt.annotate("(%s, %s)" % xy, xy=xy, xytext=(-40, 8), textcoords='offset points')
plt.show()

多条折线添加标注

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y1 = [1, 4, 9, 16]
y2 = [1, 8, 27, 64]
plt.plot(x, y1, 'rs-', label='two')
plt.plot(x, y2, 'bs-', label='three')

plt.legend(loc='lower right')
plt.show()

猜你喜欢

转载自blog.csdn.net/dooonald/article/details/79940666