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=["",'','',''])
Related
This question already has answers here:
matplotlib border width
(3 answers)
Closed 2 years ago.
Hello I have this code using matplotlib :
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1],[1])
ax.tick_params(axis=u'both', which=u'both', length=0)
plt.show()
But I would like to have this :
I mean I just want to remove the black border.
It it possible ?
Thank you very much !
use ax.spines["top"].set_visible(False) to control visibility of boarders
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1],[1])
ax.tick_params(axis=u'both', which=u'both', length=0)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.spines["left"].set_visible(False)
plt.show()
output
This question already has answers here:
How to change the font size on a matplotlib plot
(16 answers)
Closed 3 years ago.
I have a pie chart that looks like:
I tried increasing the font size using textprops={'fontsize': 18}). However, it only changes the font-size of the percentage labels inside pie, and the labels outside remain un-affected.
I want to increase fontsize of labels A, B,C etc in above pie chart.
My code:
fig1, ax1 = plt.subplots(figsize=(24,12))
flavor_pie = ax1.pie(data2.Count_Of_labels,labels=['A','B','C','D','E','F'], autopct='%.0f%%', shadow=True, colors=colors,
explode= explode1, startangle= -90, textprops={'fontsize': 18})
centre_circle = plt.Circle((0,0),0.20,fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
ax1.axis('equal')
plt.tight_layout()
plt.show()
Try:
import matplotlib as mpl
mpl.rcParams['font.size'] = 18.0
or,
mpl.rcParams.update({'font.size': 18})
or,
import matplotlib.pyplot as plt
plt.rcParams['font.size'] = 18
You might be using an older version of matplotlib; in any newer version both, the labels and autopercentages have the same size.
The question would hence boil down to how to set different font sizes for labels and autopercentages.
Having a pie chart like this
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
wedges, labels, autopct = ax.pie([1,2,3,4,3,2],labels=['A','B','C','D','E','F'],
autopct='%.0f%%', wedgeprops=dict(width=.7))
you can loop over the labels or autopercentages and set the fontsize like
for lab in labels:
lab.set_fontsize(15)
or set them all at once, like
plt.setp(labels, fontsize=15)
And similar for autopct.
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.
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)
This question already has answers here:
increase the linewidth of the legend lines in matplotlib
(4 answers)
Closed 5 years ago.
What I want to do is a plot of generation and demand in an electricity grid with Matplotlib in Python. This is my code:
fig,ax = plt.subplots(figsize=(14,8))
generation.plot(kind="area", ax=ax, linewidth=1, alpha=0.9)
load.plot(kind="area", ax=ax, linewidth=1, alpha=0.9)
labels = ['Erzeugung', 'Last']
ax.legend(labels, ncol=4, loc="best", markerscale=10)
ax.set_ylabel("GW")
ax.set_xlabel("")
plt.tight_layout()
The result looks like this:
My question is about the markerscale: Why doesn't it work with this kind of plot? The problem is the bad visibility of the marker in the legend, it would be much better with a thicker line or even a box. And this without increasing the line width of the lines. Any ideas?
You can set the line size manually after creation as follows:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
fig, ax = plt.subplots(figsize=(14,8))
generation = pd.DataFrame(np.random.randint(10, 14, 10))
load = pd.DataFrame(np.random.randint(2, 5, 10))
generation.plot(kind="area", ax=ax, linewidth=1, alpha=0.9)
load.plot(kind="area", ax=ax, linewidth=1, alpha=0.9)
labels = ['Erzeugung', 'Last']
legend = ax.legend(labels, ncol=4, loc="best")
for handle in legend.legendHandles:
handle.set_linewidth(3.0)
ax.set_ylabel("GW")
ax.set_xlabel("")
plt.tight_layout()
plt.show()
Giving you something like: