I have code like:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(xs, ys, zs, c='r', marker='o',label='A')
ax.scatter(x, y, z, c='b', marker='^',label='B')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.tick_params(axis='both', which='major', labelsize=6)
ax.tick_params(axis='both', which='minor', labelsize=6)
plt.savefig(outfile)
The tickparams which works in the 2D version is not working.
How do I do this? The documentation is very hard to read on this.
Note, this is the font size of the titles of the axes, not the tick labels.
The code from the question runs fine.
ax.tick_params(..., labelsize=6)
can be used to change the fontsize of the labels.
The complete example
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x,y,z = zip(*np.random.rand(10,3))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, c='b', marker='^',label='B')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.tick_params(axis='both', which='major', labelsize=6)
ax.tick_params(axis='both', which='minor', labelsize=6)
plt.show()
produces this plot:
Related
For some reason, when I create a plot that uses two y-axes I can no longer rotate the xticks using plt.xticks(rotation=45). Are the xticks controlled differently when using two y-axis?
plt.figure()
ax = sns.boxplot(
data=df,
x='x',
y='y',
)
ax2 = ax.twinx()
ax2 = sns.scatterplot(
x='x',
y='y',
ax=ax2,
data=df2,
legend=False,
)
plt.tight_layout()
sns.despine(offset=10, trim=True, bottom=False, right=False)
# seems to have no effect
plt.xticks(rotation=45)
plt.show()
As commented, referencing the first axis solves the issue. The following code does what I was hoping for:
plt.figure()
ax = sns.boxplot(
data=df,
x='x',
y='y',
)
ax2 = ax.twinx()
ax2 = sns.scatterplot(
x='x',
y='y',
ax=ax2,
data=df2,
legend=False,
)
plt.tight_layout()
sns.despine(offset=10, trim=True, bottom=False, right=False)
# now rotates axis labels
ax.set_xticklabels(labels_list, rotation=45)
plt.show()
While I managed to put a plot inside a plot (see the question here), I am finding trouble putting a colorbar to the larger (outside) plot. The code below is as simple as it gets, but for some reason it places the colorbar in the wrong axis:
import numpy as np
from numpy import random
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
# Canvas
fig, ax1 = plt.subplots(figsize=(12, 10))
left, bottom, width, height = [0.65, 0.15, 0.32, 0.30]
ax2 = fig.add_axes([left, bottom, width, height])
# Labels
xlabel = 'x'
ylabel = 'y'
cbarlabel = 'Color'
cmap = plt.get_cmap('turbo')
# Data
x, y, z = np.random.rand(3,200)
# Plotting
sc = ax1.scatter(x, y, marker='o', c=z, cmap=cmap)
ax2.scatter(x, y, c=z, cmap=cmap)
#
ax1.set_xlabel(xlabel)
ax1.set_ylabel(ylabel)
ax1.legend(fontsize=12, loc='upper left')
plt.tight_layout()
# Colormap
ax1 = plt.gca()
divider = make_axes_locatable(plt.gca())
cax = divider.append_axes("right", "2%", pad="1%")
cbar = plt.colorbar(sc, cax=cax) # Colorbar
cbar.set_label(cbarlabel, rotation=270, labelpad=30)
sc.set_clim(vmin=min(z), vmax=max(z))
#
plt.show()
I have also tried inset_axes as in the documentation example, to no avail.
The trick is to actually set active axes with plt.sca(ax1) and then create colorbar. I also simplified a code little bit.
Here is modified code putting colormap to the large plot:
import matplotlib.pyplot as plt
import numpy as np
from numpy import random
# Canvas
fig, ax1 = plt.subplots(figsize=(12, 10))
left, bottom, width, height = [0.45, 0.15, 0.32, 0.30]
ax2 = fig.add_axes([left, bottom, width, height])
# Labels
xlabel = 'x'
ylabel = 'y'
cbarlabel = 'Color'
cmap = plt.get_cmap('turbo')
# Data
x, y, z = np.random.rand(3,200)
# Plotting
sc = ax1.scatter(x, y, marker='o', c=z, cmap=cmap)
ax2.scatter(x, y, c=z, cmap=cmap)
# Set active axes
plt.sca(ax1)
cbar = plt.colorbar(sc) # Colorbar
cbar.set_label(cbarlabel, rotation=270, labelpad=30)
sc.set_clim(vmin=min(z), vmax=max(z))
#
ax1.set_xlabel(xlabel)
ax1.set_ylabel(ylabel)
ax1.legend(fontsize=12, loc='upper left')
plt.tight_layout()
plt.show()
Resulting in:
I have these three regplots side by side, however, I want to do these:
-increase the size of graphs
-separate them a little bit so can see the y axis more clearly
-see the values on y axis for the two right side graphs.
does anyone know how to do it efficiently? thanks
fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, sharey=True)
sns.regplot(x=Dem['Price'], y=Dem['A'], color="g", ax=ax1)
sns.regplot(x=Dem['Price'], y=Dem['B'], color="b", ax=ax2)
sns.regplot(x=Dem['Price'], y=Dem['C'], color="purple", ax=ax3)
You can use: fig.set_figwidth(25) to widen the figure and create space passing whatever numeric value you desire, e.g. 25.
To label the y-axis ticks of all subplots, use:
for ax in fig.axes:
ax.tick_params(axis='y', labelleft=True)
Full reproducible code sample with flights seaborn dataset:
import seaborn as sns
df = sns.load_dataset('flights')
df1 = df[df['year']==1949]
df2 = df[df['year']==1950]
df3 = df[df['year']==1951]
fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, sharey=True)
#1
fig.set_figwidth(25)
sns.regplot(x=df1['year'], y=df1['passengers'], color="g", ax=ax1)
sns.regplot(x=df2['year'], y=df2['passengers'], color="b", ax=ax2)
sns.regplot(x=df3['year'], y=df3['passengers'], color="purple", ax=ax3)
#2
for ax in fig.axes:
ax.tick_params(axis='y', labelleft=True)
Your code:
fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, sharey=True)
fig.set_figwidth(25)
sns.regplot(x=Dem['Price'], y=Dem['A'], color="g", ax=ax1)
sns.regplot(x=Dem['Price'], y=Dem['B'], color="b", ax=ax2)
sns.regplot(x=Dem['Price'], y=Dem['C'], color="purple", ax=ax3)
for ax in fig.axes:
ax.tick_params(axis='y', labelleft=True)
I want my plot to look like the image below, how can I achieve that using Matplotlib?
And thanks
You can use GridSpec similar to this tutorial. Possibly there will be not enough space for the y tick labels, which can be mitigated by increasing the default wspace.
import matplotlib.pyplot as plt
fig, axs = plt.subplots(ncols=4, nrows=2, figsize=(12, 7), gridspec_kw={'wspace': 0.4})
gs = axs[0, 0].get_gridspec()
for ax in axs.ravel():
ax.remove()
ax1 = fig.add_subplot(gs[0, :2])
ax1.set_ylabel('A')
ax2 = fig.add_subplot(gs[0, 2:])
ax2.set_ylabel('B')
ax3 = fig.add_subplot(gs[1, 1:3])
ax3.set_ylabel('C')
for ax in (ax1, ax2, ax3):
ax.set_xlabel('D')
ax.legend(handles=[], title='legend', loc='upper right', frameon=False)
plt.show()
I have three lists xs, ys, zs of data points in Python and I am trying to create a 3d plot with matplotlib using the scatter3d method.
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plt.xlim(290)
plt.ylim(301)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.scatter(xs, ys, zs)
plt.savefig('dateiname.png')
plt.close()
The plt.xlim() and plt.ylim() work fine, but I don't find a function to set the borders in z-direction. How can I do so?
Simply use the set_zlim function of the axes object (like you already did with set_zlabel, which also isn't available as plt.zlabel):
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
xs = np.random.random(10)
ys = np.random.random(10)
zs = np.random.random(10)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.scatter(xs, ys, zs)
ax.set_zlim(-10,10)