ppsirg

graph generation with matplotlib

Sep 21st, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #GRAPH GENERATION WITH NUMPY
  2. import matplotlib.pyplot as plt
  3. from matplotlib.scale import LinearScale
  4. import numpy as np
  5.  
  6. def tempgraph():
  7. """gen a graph"""
  8. fig = plt.figure()
  9. ydata = range(1000)
  10. yspan = 0.1
  11. x1data = [20 * np.sin(i * yspan) + 20 for i in ydata]
  12. x2data = [20 * np.cos(i * yspan) + 20 for i in ydata]
  13. #create subplots
  14. ax1 = fig.add_subplot(1, 2, 1)
  15. ax1.plot(x1data, ydata, 'b-', scalex=False)
  16. ax2 = fig.add_subplot(1, 2, 2, sharey=ax1, scalex=False)
  17. ax2.plot(x2data, ydata, 'r-')
  18. #calculate grid
  19. #divisions of x axis
  20. xgrid = {
  21. 'rang': max(x1data),
  22. 'maj': 2,
  23. 'min': 10
  24. }
  25. #how many units per grid
  26. ygrid = {
  27. 'rang': max(ydata),
  28. 'maj': int(10 / yspan),
  29. 'min': int(5 / yspan)
  30. }
  31. #set grid and eliminate labels
  32. for i in [ax1, ax2]:
  33. plt.setp(i.get_xticklabels(), visible=False)
  34. plt.setp(i.get_yticklabels(), visible=False)
  35. i.grid(linewidth=1,
  36. axis='both',
  37. alpha=0.3,
  38. linestyle='-',
  39. which='both')
  40. for k in ('min', 'maj'):
  41. #put x grid
  42. v = [(t + 1.0) * xgrid['rang'] / (xgrid[k])
  43. for t in range(xgrid[k] - 1)]
  44. i.set_xticks(v, minor=(k == 'min'))
  45. #put y grid
  46. divs = ygrid['rang'] / ygrid[k]
  47. if ygrid['rang'] % ygrid[k] != 0:
  48. divs += 1
  49. l = [t * ygrid[k] for t in range(divs)]
  50. i.set_yticks(l, minor=(k == 'min'))
  51. plt.show()
  52. plt.savefig('test_graph.pdf')
  53.  
  54. if __name__ == '__main__':
  55. tempgraph()
Advertisement
Add Comment
Please, Sign In to add comment