1. 标签
"""
Demo of the legend function with a few features.
In addition to the basic legend, this demo shows a few optional features:
* Custom legend placement.
* A keyword argument to a drop-shadow.
* Setting the background color.
* Setting the font size.
* Setting the line width.
"""
import numpy as np
import matplotlib.pyplot as plt
# Example data
a = np.arange(0,3, .02)
b = np.arange(0,3, .02)
c = np.exp(a)
d = c[::-1]
# Create plots with pre-defined labels.
# Alternatively, you can pass labels explicitly when calling `legend`.
fig, ax = plt.subplots()
ax.plot(a, c, 'k--', label='Model length')
ax.plot(a, d, 'k:', label='Data length')
ax.plot(a, c+d, 'k', label='Total message length')
# Now add the legend with some customizations.
legend = ax.legend(loc='upper center', shadow=True)
# The frame is matplotlib.patches.Rectangle instance surrounding the legend.
frame = legend.get_frame()
frame.set_facecolor('0.90')
# Set the fontsize
for label in legend.get_texts():
label.set_fontsize('large')
for label in legend.get_lines():
label.set_linewidth(1.5) # the legend line width
plt.show()
http://matplotlib.org/examples/api/legend_demo.html
2. 颜色
"""
Demo of custom color-cycle settings to control colors for multi-line plots.
This example demonstrates two different APIs:
1. Setting the default rc-parameter specifying the color cycle.
This affects all subsequent plots.
2. Setting the color cycle for a specific axes. This only affects a single
axes.
"""
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2 * np.pi)
offsets = np.linspace(0, 2*np.pi, 4, endpoint=False)
# Create array with shifted-sine curve along each column
yy = np.transpose([np.sin(x + phi) for phi in offsets])
plt.rc('lines', linewidth=4)
fig, (ax0, ax1) = plt.subplots(nrows=2)
plt.rc('axes', color_cycle=['r', 'g', 'b', 'y'])
ax0.plot(yy)
ax0.set_title('Set default color cycle to rgby')
ax1.set_color_cycle(['c', 'm', 'y', 'k'])
ax1.plot(yy)
ax1.set_title('Set axes color cycle to cmyk')
# Tweak spacing between subplots to prevent labels from overlapping
plt.subplots_adjust(hspace=0.3)
plt.show()
http://matplotlib.org/examples/color/color_cycle_demo.html
3. 图片
"""
Simple demo of the imshow function.
"""
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
image_file = cbook.get_sample_data('ada.png')
image = plt.imread(image_file)
plt.imshow(image)
plt.axis('off') # clear x- and y-axes
plt.show()
4. 填充
"""
Simple demo of the fill function.
"""
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 1)
y = np.sin(4 * np.pi * x) * np.exp(-5 * x)
plt.fill(x, y, 'r')
plt.grid(True)
plt.show()