How to hide axes and gridlines in Matplotlib (python) [duplicate] - python

This question already has answers here:
How to remove frame from matplotlib (pyplot.figure vs matplotlib.figure ) (frameon=False Problematic in matplotlib)
(11 answers)
Closed 5 years ago.
I would like to be able to hide the axes and gridlines on a 3D matplotlib graph. I want to do this because when zooming in and out the image gets pretty nasty. I'm not sure what code to include here but this is what I use to create the graph.
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.view_init(30, -90)
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plt.xlim(0,pL)
plt.ylim(0,pW)
ax.set_aspect("equal")
plt.show()
This is an example of the plot that I am looking at:

# Hide grid lines
ax.grid(False)
# Hide axes ticks
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])
Note, you need matplotlib>=1.2 for set_zticks() to work.

Turn the axes off with:
plt.axis('off')
And gridlines with:
plt.grid(b=None)

Related

Problem plotting spectrogram colorbar in matplotlib [duplicate]

This question already has answers here:
How to plot in multiple subplots
(12 answers)
Closed 1 year ago.
I want to make a subplot using the input data
I think this is just a question of passing the spectrogram's "mappable" to plt.colorbar() so that it knows what to make a colourbar for. The tricky thing is that it's a bit buried in an attribute of the spectrogram Axes:
fig, (ax1, ax2, ax3) = plt.subplots(3, sharex=True)
ax1.plot(time, data1[0].data)
ax2.plot(time, data2.data)
spec = data2.spectrogram(axes=ax3, # <-- Assign a name.
show=True,
samp_rate=20,
per_lap=0.5,
wlen=30,
log=True,
cmap='plasma', # <-- Don't use jet :)
clip=(0.05, 0.2),
)
plt.xlabel('Time')
plt.ylabel('Frequency')
# More flexibility with the positioning:
cbar_ax = fig.add_axes([0.2, 0.0, 0.6, 0.05]) # Left, bottom, width, height.
cbar = fig.colorbar(spec.collections[0], # <-- Get the mappable.
cax=cbar_ax,
orientation='horizontal')
cbar.set_label('Colorbar label')
plt.show()
This also shows how to position the colorbar where you want. And I changed your colourmap to plasma because you shouldn't use jet.

Matplotlib display grid line at bottom instead of spine

I'm trying to reproduce the following image using matplotlib
I figured I have two options to deal with the top and bottom grid lines: format the top/bottom spine to match the formatting of the grid lines, or turn off all spines and just display grid lines. I've gone with the latter, as it seems more straightforward:
ax.spines[:].set_visible(False)
ax.set_axisbelow(True)
ax.grid(True, axis='y', color='#9E9E9E')
This works for the top grid line, but the bottom of the plot displays the tick marks but not the bottom grid line:
Is it possible to make a grid line also appear at the bottom without changing the y-limits?
ax.grid() has a parameter clip_on= that can be set to False to avoid clipping by the axes borders.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
bars = ax.bar(['left', 'right'], [13160, 11569], color=['dimgrey', 'goldenrod'])
ax.bar_label(bars)
ax.spines[:].set_visible(False)
ax.set_axisbelow(True)
ax.grid(True, axis='y', color='#9E9E9E', clip_on=False)
ax.set_ylim(ymin=10000)
ax.tick_params(length=0) # hide tick marks
ax.axhline(10968, color='dodgerblue', lw=1.5)
ax.set_yticks([10000, 10968, 12000, 13000, 14000])
ax.get_yticklabels()[1].set_color('dodgerblue')
plt.show()

Why can't I remove pyplot tick labels? [duplicate]

This question already has answers here:
How to disable the minor ticks of log-plot in Matplotlib?
(3 answers)
Closed 2 years ago.
Why doesn't ax1.set_yticks([]), plt.yticks([]), plt.setp(ax1.get_yticklabels(),visible=False)
plt.gca().set_yticks([]), nor
plt.tick_params(left=False,bottom=False,labelleft=False,abelbottom=False) remove the tick numbers in this plot?
import numpy as np
import matplotlib.pyplot as plt
xscalespace=np.logspace(1, 6, num=6)
fig, (ax1, ax2) = plt.subplots(2, sharey=False, figsize=(5,5))
frequenz = np.exp(np.linspace(0,1,11)*20)#dummy lines doesn't matter
amplitude = np.exp(-np.linspace(0,1,11))#they don't matter
plt.sca(ax1)
plt.xscale('log')
plt.yscale('log')
ax1.plot(frequenz,amplitude/15,"*",label="plot",color="green")
plt.legend()
plt.grid()
plt.ylim(0.1, 1)
ax1.set_yticks([])
plt.yticks([])
plt.setp(ax1.get_yticklabels(),visible=False)
plt.gca().set_yticks([])
plt.tick_params(left=False,
bottom=False,
labelleft=False,
labelbottom=False)
The reason I ask is, I am setting my own labels but they create conflict with these irremovable labels. And I've tried everything, but nothing seems to work.
You can try specifying xticks and xticklabels from the setp function to force label deletions
plt.setp(ax1, xticks=[1,2,3,4,5],
xticklabels=['','','','',''],
yticks=[1,2,3,4,5],
yticklabels=["",'','',''])

Increasing the h-size of plots in plt.subplot() inside a loop - Python [duplicate]

This question already has answers here:
How do I change the size of figures drawn with Matplotlib?
(14 answers)
Closed 4 years ago.
I have this code:
for i in ["Dia", "DiaSemana", "Mes", "Año", "Feriado"]:
plt.subplot(1,2,1)
sns.boxplot(x=i, y="Y", data=df)
plt.subplot(1,2,2)
sns.boxplot(x=i, y="Temp", data=df)
plt.tight_layout()
plt.show()
It gives me all the plots I need. Here is one-time loop:
As you can see, the x axis is overlapped and I'm trying to increase the horizontal size of each plot in order to have a better visualization.
You are limited by the width of your figure. You can make your figure wider with the figsize attribute. You can "grab" your figure by either explicitly defining it (plt.figure) or getting the current figure (plt.gcf).
However, I prefer is using plt.subplots to define both figure and axes:
for i in ["Dia", "DiaSemana", "Mes", "Año", "Feriado"]:
fig, axes = plt.subplots(ncols=2, figsize=(15, 5)) # set width of figure and define both figure and axes
sns.boxplot(x=i, y="Y", data=df, ax=axes[0])
sns.boxplot(x=i, y="Temp", data=df, ax=axes[1])
plt.tight_layout()
plt.show()
Alternatively, you could decrease the number of ticks in the x axis.

Matplotlib: Convert the legend to a bitmap [duplicate]

This question already has answers here:
Get legend as a separate picture in Matplotlib
(11 answers)
Closed 4 years ago.
I want the legend as a separate bitmap, is that possible with matplotlib?
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])
axes.plot([1,2,3,4,5], [1,2,3,4,5], 'r')
legend = axes.legend()
How would I save legend to bitmap? Any ideas?
If you want to have a legend in a different figure, you can use axes.get_legend_handles_labels() to get the legend handles and labels and add them to a different figure.
Also you need to provide a legend in the first plot, using the label argument, or to explicitly provide a list of labels to associate with the handles.
The following code should do what I suggested in my comment to the question:
fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])
axes.plot([1,2,3,4,5], [1,2,3,4,5], 'r', label='test')
legend = axes.legend()
fig2 = plt.figure()
ax = fig2.add_subplot(111)
# add the legend from a different axes
ax.legend(*axes.get_legend_handles_labels())
# hide the spines and the x/y labels
ax.axis('off')
If you want more control for hiding stuff you can hide the axis spines only with
ax.set_frame_on(False)
or the x/y labels with
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)

Categories

Resources