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
Related
This question already has answers here:
How to change Border width in MatPlotLib?
(1 answer)
How to add border or frame around individual subplots
(2 answers)
matplotlib border width
(3 answers)
How to add black border to matplotlib 2.0 `ax` object In Python 3?
(3 answers)
Closed 19 days ago.
How can I make a plot surrounded with a bold line, like so?
I would use the set_linewidth and set_color parameters from matplotlib spines :
An axis spine -- the line noting the data area boundaries.
import matplotlib.pyplot as plt
C, W, L, T = "black", 4, 2, 7 # <- adjust here
#Color, Width, Length, Tickness --------------
fig, ax = plt.subplots(figsize=(W, L))
list_of_spines = ["left", "right", "top", "bottom"]
for sp in list_of_spines:
ax.spines[sp].set_linewidth(T)
ax.spines[sp].set_color(C)
ax.set_xticks([])
ax.set_yticks([])
plt.show();
Output :
You could use the following code that was the
answer on a similar question
import matplotlib.pyplot as plt
import matplotlib as mpl
plt.rcParams["axes.edgecolor"] = "black"
plt.rcParams["axes.linewidth"] = 2.50
fig = plt.figure(figsize = (4.1, 2.2))
ax = fig.add_subplot(111)
You can set the width and colour of a border around the image like so:
from matplotlib import pyplot as plt
# some data for demonstration purposes
import numpy as np
x = np.random.randn(100)
# create figure
fig, ax = plt.subplots()
ax.plot(x)
# set the border width
fig.set_linewidth(10)
# set the border colour (to black in this case)
fig.set_edgecolor("k")
# show the figure
fig.show()
This gives:
This question already has answers here:
Matplotlib different size subplots
(6 answers)
Closed 1 year ago.
I was trying to achieve this
But i ended with this
The main idea was to manage axes to include the third subplot in the figure, but i can't find a way to do it. Can somebody help with that please.
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-4*np.pi,4*np.pi,0.25)
np.sincx=np.sin(x)/x
plt.figure(num=3, figsize=(7,5))
plt.subplot(3,2,1)
plt.plot(x,np.sincx)
plt.subplot(3,2,2)
plt.plot(x,np.sincx,"ro")
fig = plt.figure()
ax = fig.add_axes((0.125,0.1,0.775,0.45))
plt.plot(x,np.sincx**2)
In your code, you are creating two figure, one with the two tops plots, and one with the bottom one. You need to only create one with multiple subplots!
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-4*np.pi,4*np.pi,0.25)
np.sincx = np.sin(x)/x
ax1 = plt.subplot(221)
ax1.plot(x,np.sincx)
ax2 = plt.subplot(222)
ax2.plot(x,-np.sincx,"ro")
ax3 = plt.subplot(212)
ax3.plot(x, np.sincx**2)
plt.show()
Output
With add_axes()
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-4*np.pi,4*np.pi,0.25)
np.sincx = np.sin(x)/x
fig = plt.figure()
ax1 = plt.subplot(221)
ax1.plot(x,np.sincx)
ax2 = plt.subplot(222)
ax2.plot(x,-np.sincx,"ro")
ax3 = fig.add_axes((0.125,0.1,0.775,0.45))
ax3.plot(x, np.sincx**2)
plt.show()
Output add_axes()
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=["",'','',''])
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:
This question already has answers here:
Partially transparent scatter plot, but with a solid color bar
(2 answers)
Closed 7 years ago.
The colorbar takes the same transparency as the contourf-plot which I've placed over an image.
I can achieve a non-transparent colorbar by adding a fake contourf plot without transparency at the lowest "zorder"-location, but I guess there is a proper way?
This gives me a semi-transparent colorbar:
cs = m.contourf(xv,yv,zi,zorder=4,alpha=0.7,origin="lower")
cbar = m.colorbar(cs,location='right',pad="5%")
You can do it, but there's no particular method devoted to just that. (Perhaps there should be.) Instead, you can manually modify the alpha value of cbar.solids.
For example, let's demonstrate the general problem:
import numpy as np
import matplotlib.pyplot as plt
data = np.random.random((10,10))
fig, ax = plt.subplots()
im = ax.imshow(data, cmap='gist_earth', alpha=0.5)
cbar = fig.colorbar(im)
plt.show()
And then if we change the transparency of cbar.solids:
import numpy as np
import matplotlib.pyplot as plt
data = np.random.random((10,10))
fig, ax = plt.subplots()
im = ax.imshow(data, cmap='gist_earth', alpha=0.5)
cbar = fig.colorbar(im)
cbar.solids.set(alpha=1)
plt.show()
On a side note, if you were working with a transparent contour instead of contourf, you might want to modify the alpha value of all of cbar.lines, as well.