increase space among number in X axis - python

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()

Related

How do I increase the fontsize of the scale tick in matplotlib?

I am trying to increase the fontsize of the scale tick in a matplotlib plot when using scientific notation for the tick labels.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 100, 100)
y = np.power(x, 3)
plt.ticklabel_format(
axis="y",
style="sci",
scilimits=(0,0),
useMathText=True
)
plt.yticks(fontsize=30)
plt.xticks(fontsize=30)
plt.plot(x, y)
plt.show()
The above is a minimal example. As you can see, the fontsize of the (x10^6) is tiny and I would like it be the same size as the other ticks.
Minimal example
You could try using plt.rc('font', size=30) to set the font size of everything on the plot?
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 100, 100)
y = np.power(x, 3)
plt.ticklabel_format(
axis="y",
style="sci",
scilimits=(0,0),
useMathText=True
)
#set all font in plot to a given size
plt.rc('font', size=30)
plt.plot(x, y)
plt.show()
You need to change the size of the offset_text that belongs to the y axis:
ax = plt.gca()
txt = ax.yaxis.get_offset_text()
txt.set_fontsize('large')
Note this works also for colourbars.

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()

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

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.

How to specify ticks formatter for several subplots

I need to specify ticks formatter for each plot of several subplots:
import numpy as np
import pylab as plt
import matplotlib.ticker as ticker
x = np.arange(10)
y = x
fig = plt.figure()
for i in [1, 2, 3]:
ax = fig.add_subplot(3, 1, i)
ax.plot(x, y)
ticks = ticker.FuncFormatter(lambda x, pos: '{}:{:g}'.format(i, x))
ax.xaxis.set_major_formatter(ticks)
plt.show()
But only the last (bottom) fotmatter is used for all other plots. What I do wrong?
You can use a ticker.FormatStrFormatter object as shown below.
I believe the problem with your original approach was that you were setting the Formatter for each axis to the tick variable and then overwriting it on the next iteration, as such all your graphs were using the tick variable from the last iteration.
When you create Formatter objects you have to have one for each subplot, in my code below it's not a problem because I don't assign the FormatStrFormatter to a variable.
import numpy as np
import pylab as plt
import matplotlib.ticker as ticker
x = np.arange(10)
y = x
fig, axes = plt.subplots(nrows=3, ncols=1)
for i, ax in enumerate(axes):
ax.plot(x, y)
ax.xaxis.set_major_formatter(ticker.FormatStrFormatter('{}:%d'.format(i+1)))
plt.show()
EDIT
Here is a version which uses the original FuncFormatter formatter object. The map method creates three separate ticker objects from their associated lambda functions. The for loop iterates over both ax and tick to assign each subplot.
import numpy as np
import pylab as plt
import matplotlib.ticker as ticker
x = np.arange(10)
y = x
fig, axes = plt.subplots(nrows=3, ncols=1)
def create_ticker(i):
# Create a FuncFormatter.
return ticker.FuncFormatter(lambda x, pos: '{}:{:g}'.format(i+1, x))
ticks = map(create_ticker, range(3))
for ax, tick in zip(axes, ticks):
ax.plot(x, y)
ax.xaxis.set_major_formatter(tick)
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