========== Linestyles ========== This examples showcases different linestyles copying those of Tikz/PGF.
()
| 210 | |
| 211 | |
| 212 | def PyplotLineStyles(): |
| 213 | """ |
| 214 | ========== |
| 215 | Linestyles |
| 216 | ========== |
| 217 | |
| 218 | This examples showcases different linestyles copying those of Tikz/PGF. |
| 219 | """ |
| 220 | import numpy as np |
| 221 | import matplotlib.pyplot as plt |
| 222 | from collections import OrderedDict |
| 223 | from matplotlib.transforms import blended_transform_factory |
| 224 | |
| 225 | linestyles = OrderedDict( |
| 226 | [('solid', (0, ())), |
| 227 | ('loosely dotted', (0, (1, 10))), |
| 228 | ('dotted', (0, (1, 5))), |
| 229 | ('densely dotted', (0, (1, 1))), |
| 230 | |
| 231 | ('loosely dashed', (0, (5, 10))), |
| 232 | ('dashed', (0, (5, 5))), |
| 233 | ('densely dashed', (0, (5, 1))), |
| 234 | |
| 235 | ('loosely dashdotted', (0, (3, 10, 1, 10))), |
| 236 | ('dashdotted', (0, (3, 5, 1, 5))), |
| 237 | ('densely dashdotted', (0, (3, 1, 1, 1))), |
| 238 | |
| 239 | ('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))), |
| 240 | ('dashdotdotted', (0, (3, 5, 1, 5, 1, 5))), |
| 241 | ('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))]) |
| 242 | |
| 243 | plt.figure(figsize=(10, 6)) |
| 244 | ax = plt.subplot(1, 1, 1) |
| 245 | |
| 246 | X, Y = np.linspace(0, 100, 10), np.zeros(10) |
| 247 | for i, (name, linestyle) in enumerate(linestyles.items()): |
| 248 | ax.plot(X, Y + i, linestyle=linestyle, linewidth=1.5, color='black') |
| 249 | |
| 250 | ax.set_ylim(-0.5, len(linestyles) - 0.5) |
| 251 | plt.yticks(np.arange(len(linestyles)), linestyles.keys()) |
| 252 | plt.xticks([]) |
| 253 | |
| 254 | # For each line style, add a text annotation with a small offset from |
| 255 | # the reference point (0 in Axes coords, y tick value in Data coords). |
| 256 | reference_transform = blended_transform_factory(ax.transAxes, ax.transData) |
| 257 | for i, (name, linestyle) in enumerate(linestyles.items()): |
| 258 | ax.annotate(str(linestyle), xy=(0.0, i), xycoords=reference_transform, |
| 259 | xytext=(-6, -12), textcoords='offset points', color="blue", |
| 260 | fontsize=8, ha="right", family="monospace") |
| 261 | |
| 262 | plt.tight_layout() |
| 263 | return plt.gcf() |
| 264 | |
| 265 | |
| 266 | def PyplotLinePolyCollection(): |