线的样式

这个例子展示了复制Tikz / PGF的不同线条样式。

线的样式图示

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from collections import OrderedDict
  4. from matplotlib.transforms import blended_transform_factory
  5. linestyles = OrderedDict(
  6. [('solid', (0, ())),
  7. ('loosely dotted', (0, (1, 10))),
  8. ('dotted', (0, (1, 5))),
  9. ('densely dotted', (0, (1, 1))),
  10. ('loosely dashed', (0, (5, 10))),
  11. ('dashed', (0, (5, 5))),
  12. ('densely dashed', (0, (5, 1))),
  13. ('loosely dashdotted', (0, (3, 10, 1, 10))),
  14. ('dashdotted', (0, (3, 5, 1, 5))),
  15. ('densely dashdotted', (0, (3, 1, 1, 1))),
  16. ('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))),
  17. ('dashdotdotted', (0, (3, 5, 1, 5, 1, 5))),
  18. ('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))])
  19. plt.figure(figsize=(10, 6))
  20. ax = plt.subplot(1, 1, 1)
  21. X, Y = np.linspace(0, 100, 10), np.zeros(10)
  22. for i, (name, linestyle) in enumerate(linestyles.items()):
  23. ax.plot(X, Y+i, linestyle=linestyle, linewidth=1.5, color='black')
  24. ax.set_ylim(-0.5, len(linestyles)-0.5)
  25. plt.yticks(np.arange(len(linestyles)), linestyles.keys())
  26. plt.xticks([])
  27. # For each line style, add a text annotation with a small offset from
  28. # the reference point (0 in Axes coords, y tick value in Data coords).
  29. reference_transform = blended_transform_factory(ax.transAxes, ax.transData)
  30. for i, (name, linestyle) in enumerate(linestyles.items()):
  31. ax.annotate(str(linestyle), xy=(0.0, i), xycoords=reference_transform,
  32. xytext=(-6, -12), textcoords='offset points', color="blue",
  33. fontsize=8, ha="right", family="monospace")
  34. plt.tight_layout()
  35. plt.show()

下载这个示例