Python - matplotlib modify xticks for a "loglog" plot - python

I am plotting in log scale for each axis, but I don't want scientific notation for the x axis, so I modified it as follows:
from matplotlib import pyplot as plt
from matplotlib.ticker import FormatStrFormatter
a = np.array([10.**(-2), 10.**(-3), 5.*10**(-3),10.**(-4), 10.**(-6), 10.**(-7)])
b = np.array([16, 12.5, 14.5, 9.5, 8., 7.5])
axes = plt.subplot(111)
plt.loglog(a,b, 'k.',markersize=10,markerfacecolor='None',label='')
plt.ylim(10**(-6),10**(-1))
plt.xlim(5,30)
plt.subplots_adjust(left=0.15)
plt.legend(loc=2)
axes.xaxis.set_minor_formatter(FormatStrFormatter("%.0f"))
plt.show()
But, as you can see in the picture below, there is 10 in scientific notation amongst the x axis labels... I don't know how to suppress it and just have 10...

You can use the ScalarFormatter from matplotlib.ticker
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter
fig = plt.figure()
ax = fig.add_subplot(111)
ax.semilogx(range(100))
ax.set_xscale('log')
ax.set_yscale('log')
ax.xaxis.set_major_formatter(ScalarFormatter())
#ax.yaxis.set_major_formatter(ScalarFormatter()) # for the y axis
fig.show()

Related

Matplotlib imshow ticks are wrong with negative values

import matplotlib.pyplot as plt
import numpy as np
a = np.random.randn(5,5)
plt.imshow(a)
plt.xticks(range(5))
plt.yticks([i-2 for i in range(5)])
plt.show()
results in
??
Also imagine I had 500 instead of 5 ticks, how could I pass the ticks but have less be displayed (for example every 10th)?
Use the extent parameter, and no need to use xticks or yticks:
plt.imshow(a, extent=(-0.5, 4.5, -2.5, 2.5))
Output:
Use MultipleLocator for your second question:
from matplotlib.ticker import MultipleLocator
a = np.random.randn(500,500)
fig, ax = plt.subplots(figsize=(10, 10))
ax.imshow(a, extent=(-0.5, 500.5, -250.5, 250.5))
ax.xaxis.set_major_locator(MultipleLocator(25))
ax.yaxis.set_major_locator(MultipleLocator(25))
Output:

Seaborn Adjusting Markers

As you can see here, the X axis labels here are quite unreadable. This will happen regardless of how I adjust the figure size. I'm trying to figure out how to adjust the labeling so that it only shows certain points. The X axis are all numerical between -1 to 1, and I think it would be nice and more viewer friendly to have labels at -1, -.5, 0, .5 and 1.
Is there a way to do this? Thank you!
Here's my code
sns.set(rc={'figure.figsize':(20,8)})
ax = sns.countplot(musi['Positivity'])
ax.set_xticklabels(ax.get_xticklabels(), rotation=40, ha='right')
plt.tight_layout()
plt.show()
Basically seaborn is wrapper on matplotlib. You can use matplotlib ticker function to do a Job. Refer the below example.
Let's Plots tick every 1 spacing.
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import seaborn as sns
sns.set_theme(style="whitegrid")
x = [0,5,9,10,15]
y = [0,1,2,3,4]
tick_spacing = 1
fig, ax = plt.subplots(1,1)
sns.lineplot(x, y)
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
plt.show()
Now Let's plot ticks every 5 ticks.
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import seaborn as sns
sns.set_theme(style="whitegrid")
x = [0,5,9,10,15]
y = [0,1,2,3,4]
tick_spacing = 5
fig, ax = plt.subplots(1,1)
sns.lineplot(x, y)
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
plt.show()
P.S.: This solution give you explicit control of the tick spacing via the number given to ticker.MultipleLocater(), allows automatic limit determination, and is easy to read later.

Format the y-axis label in the higher thousands with many zeros to scientific notation?

How can I tighten the y-axis by using scientific notation?
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x=[1,2,3,4,5,6,7,8,9,10]
y=np.array([1,1,1,2,10,2,1,1,1,1])*100000
ax.plot(x, y)
With plt.ticklabel_format:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x=[1,2,3,4,5,6,7,8,9,10]
y=np.array([1,1,1,2,10,2,1,1,1,1])*100000
ax.plot(x, y)
plt.ticklabel_format(axis="y", style="sci", scilimits=(0,5))
plt.show()

Set scale of axis in plot using matplotlib

I am unable to scale the y-axis. My code is as follows:
import matplotlib.pyplot as pt
import numpy as np
fig = pt.figure()
ax = fig.add_subplot(111)
sample = 20
x=np.arange(sample)
y=10*np.sin(2*np.pi*x/20)
pt.plot(x,y)
pt.show()
The y axis has scale of 5. I'm trying to make it 1.
You can do so using set_yticks this way:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
sample = 20
x=np.arange(sample)
y=10*np.sin(2*np.pi*x/20)
ax.plot(x,y)
ax.set_yticks(np.arange(min(y), max(y)+1, 1.0)) # setting the ticks
ax.set_xlabel('x')
ax.set_ylabel('y')
fig.show()
Which produces this image wherein y-axis has a scale of 1.

matplotlib pcolor with modified axis

Goal: I want to modify the axis of the pcolor plot in such a way that the pcolor plot is not changed and not shifted, only the axis!
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import numpy as np
fig, ax = plt.subplots(1,1)
phi = np.random.random([20,10])
xticks = range(10)
yticks = range(10)
ax.pcolor(phi,norm=LogNorm(vmin=10E-3, vmax=10E3))
ax.set_xticks(xticks)
ax.set_xscale("log")
ax.set_yticks(yticks)
fig.show()
failed plot

Categories

Resources