joe oxley
joe oxley

Reputation: 73

Axis plotting in python - matplotlib

I currently have a plot for my data, with a vertical line in red marking the point where "average number of spins" is. I was wondering if is possible to change the marker for the points for "millionspins,millions" into a blue/black thin cross, and to place major/minor gridlines onto both axis? All the available help online seems to draw from modules that we do not have. Also how do you label the red line that I have plotted to be "average number of spins"?

Many thanks.

matplotlib.pyplot.scatter(millionspins,millions)
plt.xlabel("Number of spins")
plt.plot([averageiterations,averageiterations],[1000000,1500000],'r-')
plt.xlim(0,max(millionspins))
plt.ylim(1000000,max(millions))
plt.ylabel("Bank")
plt.xlim(0,max(millionspins))
plt.ylim(1000000,max(millions))
show()

Upvotes: 4

Views: 737

Answers (2)

unutbu
unutbu

Reputation: 879201

Use marker='+' to make the marker a thin cross:

plt.scatter(millionspins, millions, c='b', marker='+')

Use plt.grid to show grid lines:

plt.grid(True, which='both')

By the way, you could use plt.axvline to create the red vertical line. That way, if the user rescales the plot, the endpoints of the vertical line won't show:

import matplotlib.pyplot as plt
import numpy as np
N = 100
millionspins = np.random.randint(10000000, size=N)
millions = np.random.randint(10000000, size=N)
averageiterations = millionspins.mean()

plt.scatter(millionspins, millions, marker='+')
plt.axvline([averageiterations], color='r')

plt.xlabel("Number of spins")
plt.ylabel("Bank")

plt.xlim(0,max(millionspins))
plt.ylim(1000000,max(millions))

plt.grid(True, which='both')
plt.show()

enter image description here

Upvotes: 1

tacaswell
tacaswell

Reputation: 87366

I think what you want is:

ax = plt.gca()
ax.grid(True)
ax.plot(millionspins, millions, marker='+', color='b')

Unless you need to make each marker a different size or color, I would suggest using plot instead of scatter because it is faster and you can more easily modify it later (if you want to do animations).

Upvotes: 1

Related Questions