Reputation: 1144
Here is my graph, but it seems like over line x axis after 9. Anyone can help me to edit it?
Upvotes: 1
Views: 112
Reputation: 17126
You can set the x limits with a range, using either pyplot's xlim
or with the object oriented interface ax.set_xlim
. Both take a range as arguments. So if you wanted to set the x-axis to be between 5 and 10, you just do:
plt.xlim((5,10))
Given some initial boilerplate to set up a graph:
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_ylim((0, 2100))
ax.bar(0, 2000, width=5)
ax.bar(100, 500, width=5)
ax.bar(40, 1500, width=5)
You can then use set_xlim
to change the x limits:
ax.set_xlim((-50,150))
ax.set_xticks(np.arange(-50, 150, 20))
plt.show()
More generally, it's plt.xlim(left, right)
where left and right are the left and right boundaries you want for the graph. The units are always going to be the same as the data you feed in. Even if you use a log scale, the values you pass to xlim
will be applied to the normal scale (e.g., (0, 100)
would go to (0, 10**2)
on the log graph)
You can use it with a log scale just the same way (but it's a bit finickier). To get a log scale, you just need to change the xscale with ax.set_xscale("log")
Upvotes: 2