How can I reduce the distance between the numbering of an axis and the ticks corresponding to them. I tried using pad=0 for the tick_params but it doesn't seem to work. Below is a reproducible (simplified) code of my issue (and the figure):
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
plt.rcParams["figure.figsize"] = (10,10)
fig = plt.figure()
ax = Axes3D(fig)
ax.set_xlabel("X" , fontsize=20)
ax.set_ylabel("Y", fontsize=20)
ax.set_zlabel("Z" , fontsize=20)
ax.view_init(azim=-20)
ax.tick_params(axis='x', which='major', pad=0)
x = np.arange(0,10,0.01)
y = np.ones(len(x))
z = np.sin(x)
plt.plot(x,y,z)
Changing the values of pad seem to not have any effect. Note: I need the plot in that specific orientation (azim=-20). How can I achieve what I need? Thank you!
The pad argument also takes negative values to bring the ticklabels closer to the ticks.
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
fig = plt.figure()
ax = axes3d.Axes3D(fig)
ax.set_xlabel("X" , fontsize=20)
ax.set_ylabel("Y", fontsize=20)
ax.set_zlabel("Z" , fontsize=20)
ax.view_init(azim=-20)
ax.tick_params(axis='x', which='major', pad=-5)
x = np.arange(0, 10, 0.01)
y = np.ones(len(x))
z = np.sin(x)
plt.plot(x, y, z)
plt.show()
EDIT: Alternative outcome with set figure size and dpi value.
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
mpl.rcParams["figure.figsize"] = 10, 10
mpl.rcParams["figure.dpi"] = 100
fig = plt.figure()
ax = axes3d.Axes3D(fig)
ax.set_xlabel("X" , fontsize=20)
ax.set_ylabel("Y", fontsize=20)
ax.set_zlabel("Z" , fontsize=20)
ax.view_init(azim=-20)
ax.tick_params(axis='x', which='major', pad=-5)
x = np.arange(0, 10, 0.01)
y = np.ones(len(x))
z = np.sin(x)
plt.plot(x, y, z)
plt.show()
Related
I am trying to add an additional small colorbar for the inset axis. The current code, without that, is
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import matplotlib.colors as colors
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
A = np.linspace(1,20,20)
B = A
X,Y = np.meshgrid(A,B)
Z = X**2 + Y**2
fig, ax = plt.subplots()
im = ax.pcolor(X, Y, Z, cmap='hot_r')
ax.set_xlabel('x',fontsize=labelsize)
ax.set_ylabel('y',fontsize=labelsize)
ca = fig.colorbar(im)#, shrink=0.5, aspect=5)
axins = ax.inset_axes([0.1, 0.5, 0.25, 0.25])
axins.pcolor(A[0:4], B[0:4], Z[0:4,0:4], cmap='hot_r')
axins.tick_params(axis='both', which='major', labelsize=11)
for axis in ['top','bottom','left','right']:
axins.spines[axis].set_linewidth(1)
axins.spines[axis].set_color('gray')
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec='gray', lw=1)
plt.tight_layout()
You could create an additional inset axis for the colorbar. E.g. located just right of the inset. Then create a colorbar proving this axis (cax=...).
Please note that pcolor creates faces (large pixels) between the given x and y positions. So, you need one row and one column more of position then the number of colors. The current version of matplotlib gives a warning in case too many colors (or not enough x and y positions) are given.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
A = np.linspace(1, 20, 20)
B = A
X, Y = np.meshgrid(A, B)
Z = X ** 2 + Y ** 2
fig, ax = plt.subplots()
im = ax.pcolor(X, Y, Z[:-1, :-1], cmap='hot_r')
ax.set_xlabel('x', fontsize=12)
ax.set_ylabel('y', fontsize=12)
ca = fig.colorbar(im) # , shrink=0.5, aspect=5)
axins = ax.inset_axes([0.1, 0.5, 0.25, 0.25])
axins_cbar = ax.inset_axes([0.37, 0.5, 0.02, 0.25])
img_in = axins.pcolor(A[0:5], B[0:5], Z[0:4, 0:4], cmap='hot_r')
axins.tick_params(axis='both', which='major', labelsize=11)
for axis in ['top', 'bottom', 'left', 'right']:
axins.spines[axis].set_linewidth(1)
axins.spines[axis].set_color('gray')
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec='gray', lw=1)
fig.colorbar(img_in, cax=axins_cbar)
plt.tight_layout()
plt.show()
How can I move the z-axis of a 3D plot on the other side (including the label, ticks, and numbering). Here is small code and figure of what I mean:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
fig = plt.figure()
ax = Axes3D(fig)
ax.set_xlabel("X" , fontsize=20)
ax.set_ylabel("Y", fontsize=20)
ax.set_zlabel("Z" , fontsize=20)
x = np.arange(0,10,0.01)
y = np.ones(len(x))
z = np.sin(x)
plt.plot(x,y,z)
One possible solution:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
fig = plt.figure()
ax = Axes3D(fig)
tmp_planes = ax.zaxis._PLANES
ax.zaxis._PLANES = ( tmp_planes[2], tmp_planes[3],
tmp_planes[0], tmp_planes[1],
tmp_planes[4], tmp_planes[5])
ax.set_xlabel("X" , fontsize=20)
ax.set_ylabel("Y", fontsize=20)
# rotate label
ax.zaxis.set_rotate_label(False) # disable automatic rotation
ax.set_zlabel("Z axis label" , fontsize=20, rotation=90)
x = np.arange(0,10,0.01)
y = np.ones(len(x))
z = np.sin(x)
plt.plot(x,y,z)
import numpy as np
import matplotlib.pyplot as plt
# example data
x = np.arange(0.1, 4, 0.5)
y = np.exp(-x)
yerr = 0.1*np.random.rand(8)
fig, ax = plt.subplots()
ax.errorbar(x, y, linestyle='none', marker='*', yerr=yerr)
plt.show()
Hi, everyone! The goal is to add legend to the chart. y and yerr are labelled as 'mean' and 'std.Dev', respectively.
MWE:
I would like the title position same as in the graph :
Here is my code :
import matplotlib.pyplot as plt
import numpy as np
import random
fig, ax = plt.subplots()
x = random.sample(range(256),200)
y = random.sample(range(256),200)
cor=np.corrcoef(x,y)
plt.scatter(x,y, color='b', s=5, marker=".")
#plt.scatter(x,y, label='skitscat', color='b', s=5, marker=".")
ax.set_xlim(0,300)
ax.set_ylim(0,300)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Correlation Coefficient: %f'%cor[0][1])
#plt.legend()
fig.savefig('plot.png', dpi=fig.dpi)
#plt.show()
But this gives :
How do I fix this title position?
assign two corresponded value to X and Y axis. notice! to have title inside graph, values should be in (0,1) interval. you can see a sample code here:
import matplotlib. pyplot as plt
A= [2,1,4,5]; B = [3,2,-2,1]
plt.scatter(A,B)
plt.title("title", x=0.9, y=0.9)
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.show()
It will be unnecessarily complicated to move the title at some arbitrary position inside the axes.
Instead one would rather create a text at the desired position.
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
x = np.random.randint(256,size=200)
y = np.random.randint(256,size=200)
cor=np.corrcoef(x,y)
ax.scatter(x,y, color='b', s=5, marker=".")
ax.set_xlim(0,300)
ax.set_ylim(0,300)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.text(0.9, 0.9, 'Correlation Coefficient: %f'%cor[0][1],
transform=ax.transAxes, ha="right")
plt.show()
In matplotlib, how can I change the font size of a latex symbol?
I have the following code:
import matplotlib.pyplot as plt
import seaborn as sns
# get x and y from file
plt.plot(x, y, linestyle='--', marker='o', color='b')
plt.xlabel(r'$\alpha$ (distance weighted)', fontsize='large')
plt.ylabel('AUC')
plt.show()
But I get the following graph:
Notice that the $\alpha$ is still small.
To increase the size of the fonts set the desired value to fontsize. One way to mitigate the difference between the "normal" font and the "latex" one is by using \mathrm. The example below shows the behaviour of doing this:
import matplotlib.pyplot as plt
import seaborn as sns
x = np.arange(10)
y = np.random.rand(10)
fig = plt.figure(1, figsize=(10,10))
for i, j in zip(np.arange(4), [10,15,20,30]):
ax = fig.add_subplot(2,2,i+1)
ax.plot(x, y, linestyle='--', marker='o', color='b')
ax.set_xlabel(r'$\mathrm{\alpha \ (distance \ weighted)}$', fontsize=j)
ax.set_ylabel('AUC')
plt.show()