Why is the y axis in increments of 1 rather than 90 - python

I want to make a sine graph but the y-axis is off, how can i change that.
Also that do the 3 numbers in the brackets after linspace mean?
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 7, 100)
y = np.sin(x)
plt.plot(x, y)
plt.grid(True)
plt.show()
Thank You

See set_xlim to adjust the limits of the x-axis.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 7, 100)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x,y)
ax.grid(True)
ax.set_xlim(0,7)
plt.show()
Before:
After:
The three numbers in the np.linspace command mean start, stop, and num. I.e. it generates an array of 100 evenly spaced numbers between 0 and 7.

Related

increase space among number in X axis

I have the following plot:
How can I increase the space among values in X axis with matplotlib?
Thanks!
You can set a log scale and invert the x-axis:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import ScalarFormatter
x = [round(28800 * 2 ** (-i)) for i in range(10)]
y = np.random.randint(0, 80, len(x))
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_xscale('log')
ax.set_xticks(x)
ax.xaxis.set_major_formatter(ScalarFormatter())
ax.invert_xaxis()
plt.show()

matplotlib 3d: moving tick's label

Is there a way to move tick labels in Matplot3dlib like this?
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
x = np.outer(np.linspace(-2, 2, 30), np.ones(30))
y = x.copy().T # transpose
z = np.cos(x ** 2 + y ** 2)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(x, y, z,cmap='viridis', edgecolor='none')
ax.set_title('Surface plot')
plt.show()
There are some ways using pad parameters.
However, I want to move more precisely like figure in the link above.
Any help appreciated.
-- Addition --
When I changing PAD parameter like the code below, the tick's label is more closer to the axis. However, I want to move it a little bit more to -x direction.
tick's label position changing
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
x = np.outer(np.linspace(-2, 2, 30), np.ones(30))
y = x.copy().T # transpose
z = np.cos(x ** 2 + y ** 2)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(x, y, z,cmap='viridis', edgecolor='none')
ax.set_title('Surface plot')
ax.tick_params(axis='x', which='major', pad=-5)
plt.show()

matplotlib yticks elements too far to each other, and problem on ticks spacing

How can I control the spacing between subplot's y ticks? I have 2 scatter plots on a subplot, on the first scatter plot the Ys are always equal to 0, and on the second scatter plot I have a y values that are ranging from 7.0 to 7.25 and more. There is too much space between 0 and 7, and there is no space between from 7 to 7.25( it should be like this 0, 7.000,7.005,7.010,7.015,7.020,7.025,~~)
Update sample code
import random
import matplotlib.pyplot as plt
import numpy as np
import mpld3
x = [x for x in range(100)]
y=[random.uniform(7, 8) for x in range(100)]
y1=[0 for x in range(100)]
fig, ax = plt.subplots()
ax.scatter(x,y)
ax.scatter(x,y1)
fig.show()

Manipulate number of squares in a grid matplotlib

Right now I have a grid in my plots using the option
from matplotlib import pyplot as plt
plt.grid(True)
Because of the nature of my plot, the lines of the grid are at every 500 units in x and every 5 units in y. Is there a way where I can increment the number of horizontal lines (i.e. increment to a line per y unit)?
You can do this with which='minor', but you need to turn on minor ticks first. For example:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,10,100)
y = x**2
plt.plot(x,y)
ax = plt.gca()
minor_ticks = np.arange(0,100,5)
ax.set_yticks(minor_ticks, minor=True)
ax.yaxis.grid(which='minor')
plt.show()

how to make a particular axis which isn't linear or log?

I want to replicate this axis (see picture) and I have a range of values from 1-10 - what format is this in and how can it be achieved in matplotlib?
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
fig, ax = plt.subplots()
x = np.linspace(1, 11, 100)
y = np.sin(x)
ax.plot(x, y)
ax.set_xscale('log')
ax.xaxis.set_major_formatter(ticker.ScalarFormatter())
ax.xaxis.set_major_locator(ticker.FixedLocator([1, 3, 6, 10]))
ax.set_xlim(0, 11)
plt.show()

Categories