XKCD

演示如何创建类似xkcd的绘图。

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  1. with plt.xkcd():
  2. # Based on "Stove Ownership" from XKCD by Randall Monroe
  3. # http://xkcd.com/418/
  4. fig = plt.figure()
  5. ax = fig.add_axes((0.1, 0.2, 0.8, 0.7))
  6. ax.spines['right'].set_color('none')
  7. ax.spines['top'].set_color('none')
  8. plt.xticks([])
  9. plt.yticks([])
  10. ax.set_ylim([-30, 10])
  11. data = np.ones(100)
  12. data[70:] -= np.arange(30)
  13. plt.annotate(
  14. 'THE DAY I REALIZED\nI COULD COOK BACON\nWHENEVER I WANTED',
  15. xy=(70, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10))
  16. plt.plot(data)
  17. plt.xlabel('time')
  18. plt.ylabel('my overall health')
  19. fig.text(
  20. 0.5, 0.05,
  21. '"Stove Ownership" from xkcd by Randall Monroe',
  22. ha='center')

XKCD示例1

  1. with plt.xkcd():
  2. # Based on "The Data So Far" from XKCD by Randall Monroe
  3. # http://xkcd.com/373/
  4. fig = plt.figure()
  5. ax = fig.add_axes((0.1, 0.2, 0.8, 0.7))
  6. ax.bar([0, 1], [0, 100], 0.25)
  7. ax.spines['right'].set_color('none')
  8. ax.spines['top'].set_color('none')
  9. ax.xaxis.set_ticks_position('bottom')
  10. ax.set_xticks([0, 1])
  11. ax.set_xlim([-0.5, 1.5])
  12. ax.set_ylim([0, 110])
  13. ax.set_xticklabels(['CONFIRMED BY\nEXPERIMENT', 'REFUTED BY\nEXPERIMENT'])
  14. plt.yticks([])
  15. plt.title("CLAIMS OF SUPERNATURAL POWERS")
  16. fig.text(
  17. 0.5, 0.05,
  18. '"The Data So Far" from xkcd by Randall Monroe',
  19. ha='center')
  20. plt.show()

XKCD示例2

下载这个示例