python用matplotlib画五角星_基于Matplotlib的Python绘图

# 使用该法,不用写plt.show(),以及可以边写边运行

%matplotlib notebook

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示中文标签

plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号

import pandas as pd

import numpy as np

标题及轴标签

def f(t):

s1 = np.cos(2*np.pi*t)

e1 = np.exp(-t)

return s1 * e1

t1 = np.arange(0.0, 5.0, 0.1)

t2 = np.arange(0.0, 5.0, 0.02)

t3 = np.arange(0.0, 2.0, 0.01)

box = dict(facecolor='yellow', pad=5, alpha=0.2)

# 整个画板的标题

plt.suptitle('我的画板标题', fontsize=16, fontweight='bold')

plt.subplots_adjust(left=0.2, wspace=0.8, top=0.8) #位置调整

plt.subplot(121)

plt.plot(t1, f(t1), 'o', t2, f(t2), '-')

plt.title('画板1',color='r')

plt.ylabel('Y轴',bbox=box)

plt.subplot(122)

plt.plot(t3, np.cos(2*np.pi*t3), '--')

plt.title('画板2', color='b')

plt.xlabel('X 轴',bbox=box)

plt.ylabel('Y 轴',bbox=box)

plt.show()

样式

x1 = np.linspace(0.0, 5.0)

x2 = np.linspace(0.0, 2.0)

y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)

y2 = np.cos(2 * np.pi * x2)

plt.subplot(2, 1, 1)

# "o-r"中r表示红色,o表示实点,-表示连接线

# 可以写成 ro- 或 or- 或 -or 顺序不要求

plt.plot(x1, y1, 'o-r')

plt.subplot(2, 1, 2)

plt.plot(x2, y2, '.-')

plt.show()

t = np.arange(0.0, 2.0, 0.01)

s = np.sin(2 * np.pi * t)

fig, ax = plt.subplots()

ax.plot(t, s)

# 网格

ax.grid(True, linestyle='-.')

# 坐标

# ax.tick_params(axis='both',labelcolor='r', labelsize='medium', width=3)

ax.tick_params(axis='x',labelcolor='gold', labelsize='medium', width=3)

ax.tick_params(axis='y',labelcolor='b', labelsize='medium', width=2)

# 注释

ax.annotate("这是注释\n"

"这是注释", (0.5, 0.5),

xycoords="axes fraction", va="center", ha="center",

bbox=dict(boxstyle="round, pad=1", fc="w"))

plt.show()

data = np.random.randn(30).cumsum()

plt.plot(data, 'r--', label='Default',marker='o')

# 写这步运行直接添加到上图中

plt.plot(data, 'k-', drawstyle='steps-post', label='steps-post')

plt.legend(loc='best')

使用内置样式

# 全部内置样式

from matplotlib import style print(plt.style.available)

‘bmh’, ‘classic’, ‘dark_background’, ‘fast’, ‘fivethirtyeight’, ‘ggplot’, ‘grayscale’, ‘seaborn-bright’, ‘seaborn-muted’, ‘seaborn-notebook’, ‘seaborn-paper’, ‘seaborn-pastel’, ‘seaborn-poster’, ‘seaborn-talk’, ‘seaborn-ticks’, ‘seaborn-white’, ‘seaborn-whitegrid’, ‘seaborn’, ‘Solarize_Light2’, ‘tableau-colorblind10’, ‘_classic_test’

plt.style.use('dark_background')

fig, ax = plt.subplots()

L = 6

x = np.linspace(0, L)

ncolors = len(plt.rcParams['axes.prop_cycle'])

shift = np.linspace(0, L, ncolors, endpoint=False)

for s in shift:

ax.plot(x, np.sin(x + s), 'o-')

ax.set_xlabel('x-axis')

ax.set_ylabel('y-axis')

ax.set_title("'dark_background' style sheet")

plt.show()

plt.style.use('fivethirtyeight')

x = np.linspace(0, 10)

# Fixing random state for reproducibility

np.random.seed(19680801)

fig, ax = plt.subplots()

ax.plot(x, np.sin(x) + x + np.random.randn(50))

ax.plot(x, np.sin(x) + 0.5 * x + np.random.randn(50))

ax.plot(x, np.sin(x) + 2 * x + np.random.randn(50))

ax.plot(x, np.sin(x) - 0.5 * x + np.random.randn(50))

ax.plot(x, np.sin(x) - 2 * x + np.random.randn(50))

ax.plot(x, np.sin(x) + np.random.randn(50))

ax.set_title("'fivethirtyeight' style sheet")

plt.show()

线条及填充

t = np.arange(-1, 2, .01)

s = np.sin(2 * np.pi * t)

#曲线

plt.plot(t, s)

# 以y轴0点画横线

plt.axhline(linewidth=8, color='#d62728')

# 画横线

plt.axhline(y=1)

# 画纵线

plt.axvline(x=1)

# Draw a thick blue vline at x=0 that spans the upper quadrant of the yrange

# plt.axvline(x=0, ymin=0.75, linewidth=8, color='#1f77b4')

# 画线段

plt.axhline(y=.5, xmin=0.25, xmax=0.75)

# 平行填充

plt.axhspan(0.25, 0.75, facecolor='0.5', alpha=0.5)

# 垂直填充

plt.axvspan(1.25, 1.55, facecolor='#2ca02c', alpha=0.5)

# 坐标轴

plt.axis([-1, 2, -1, 2])

plt.show()

交差及填充

x = np.arange(0.0, 2, 0.01)

y1 = np.sin(2*np.pi*x)

你可能感兴趣的:(python用matplotlib画五角星_基于Matplotlib的Python绘图)