There are essentially two ways to use Matplotlib:
figurefig, ax = pyplot.subplots() create a figure containing a single axes.ax.plot()
fig = pyplot.figre() create an empty figure with no Axes
fig ,(axs1,ax2s) = plt.subplots(1,2,figsize=(5,2.7)) create a figure with a 1x2 grid of Axes
import matplotlib as mpl
import matplotlob.pyplot as plt
# pylab is old examples, and is strongly deprecated
import numpy as np
"""
Pyplot-stytle
"""
# Size, Layout and create Figure
plt.figure(figsize=(5,2.7),layout='constrained')
# Plot some data on the implicit axes
plt.plot(x,y,label='first type')
plt.plot(x,z,label='second type with difference')
# Write Axes Label
plt.xlabel('x label')
plt.ylabel('f label')
# Title
plt.title("Simple Plot")
# Legend, label of line
plt.legend()
# Show
plt.show()
"""
OO-style
"""
# we use '.pyplot.figure' to create the Figure
fig, ax = plt.subplots(figsize=(5,2.7), layout='constrained')
# Plot some data on the axes
ax.plot(x,y,label='linear')
ax.plot(x,z,label='qudratic')
# Add label to the axes
ax.set_label('x label')
ax.set_label('f label')
# Title
ax.set_title('Simple Plot')
# Legend
ax.legend()
# show
fig.show()
"""
Making helper function
"""
def my_plotter(ax, data_x, data_y ,param_dict):
"""
A helper function to make a graph
"""
out = ax.plot(data_x, data_y, **paran_dict)
return out
""""""
data1, data2, data3, data4 = np.random.randn(4, 100)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(5,2.7))
my_plotter(ax1,data1,data2,{'marker':'x'})
my_plotter(ax2,data3,data4,{'marker':'o'})
Plotmatplotlib.axes.Axes.plot — Matplotlib 3.7.2 documentation
Plotting functions expect numpy.array or numpy.ma.masked_array as input
b = np.matrix([[1,2],[3,4]])
b_asarray = np.asarray(b)
Most methods will also parse an addressable object like a dict. Matplotlib allows you to provide the data keyword argument and generate plots passing the string correspoding to the x and y variables.
ax.scatter('a_x','a_y', c='c',data=data_dic)
return <list>[line2D] ,需要使用, 解包。l, = ax.plot()
print(plt.style.available)color=’blue’ , facecolor=’C0’ , edgecolor=’k’linewidth=3 (1 pt = 1/72 inch)linestyle=’-.’‘o’