Python matplotlib 绘图保存图片简单示例

一个Python matplotlib 绘图保存图片简单示例,保存的时候遇到过保存空白图像的问题,是因为将plt.savefig('./linuxidc.com.jpg')放到了plt.show()之后,只要先保存在显示就可以正常保存了。

from cycler import cycler
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

# Define a list of markevery cases and color cases to plot
cases = [None,
        8,
        (30, 8),
        [16, 24, 30],
        [0, -1],
        slice(100, 200, 3),
        0.1,
        0.3,
        1.5,
        (0.0, 0.1),
        (0.45, 0.1)]

colors = ['#1f77b4',
          '#ff7f0e',
          '#2ca02c',
          '#d62728',
          '#9467bd',
          '#8c564b',
          '#e377c2',
          '#7f7f7f',
          '#bcbd22',
          '#17becf',
          '#1a55FF']

# Configure rcParams axes.prop_cycle to simultaneously cycle cases and colors.
mpl.rcParams['axes.prop_cycle'] = cycler(markevery=cases, color=colors)

# Create data points and offsets
x = np.linspace(0, 2 * np.pi)
offsets = np.linspace(0, 2 * np.pi, 11, endpoint=False)
yy = np.transpose([np.sin(x + phi) for phi in offsets])

# Set the plot curve with markers and a title
fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.6, 0.75])

for i in range(len(cases)):
    ax.plot(yy[:, i], marker='o', label=str(cases[i]))
    ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)

plt.title('www.linuxidc.com')

plt.savefig('./www.linuxidc.com.jpg')

plt.show()

就会在当前目录下生成图片。

Python matplotlib 绘图保存图片简单示例

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/143de646ece86eb78d49dc3d42f36597.html