Matplotlib logarithmic x-axis and padding - python

I am struggling with matplotlib and padding on the x-axis together with a logarithmic scale (see the first picture).
Without a logarithmic scale, the padding applies nicely (see the second one).
Any suggestations how to get a padding between plot lines and the axis line in the bottom left corner so that one can see the points on the line?
Thanks.
The code:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import *
from matplotlib.ticker import ScalarFormatter
style.use('fivethirtyeight')
fig, ax = plt.subplots()
T = np.array([2**x for x in range(0,7+1)])
opt1 = np.array([x for x in range(0,7+1)])
opt2 = np.array([x*2 for x in range(0,7+1)])
opt3 = np.array([x*4 for x in range(0,7+1)])
ax.grid(True)
xlabel("#nodes")
ylabel("time(s)")
legend(loc="best")
title(r"Node start times")
plt.xticks([2**x for x in range(0,7+1)])
plt.plot(T,opt1,"o-", label="opt1")
plt.plot(T,opt2, "s-", label="opt2")
plt.plot(T,opt3, "d-", label="opt2")
plt.legend(loc="upper left")
# This should be called after all axes have been added
plt.tight_layout()
plt.margins(0.05, 0.05)
# 1, 2, 4, ...
ax.set_xscale('log', basex=2)
ax.xaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter("%d"))
plt.show()
#savefig("plot_1.pdf")

This does not address your padding issue, but you could use clip_on=False to prevent the points from being cut off. It seems you also need to make sure they're above the axes using zorder
plt.plot(T,opt1,"o-", label="opt1", clip_on=False, zorder=10)
plt.plot(T,opt2, "s-", label="opt2", clip_on=False, zorder=10)
plt.plot(T,opt3, "d-", label="opt2", clip_on=False, zorder=10)

Related

How to increase plottable space above a subplot in matplotlib?

I am currently making a plot on matplotlib, which looks like below.
The code for which is:
fig, ax1 = plt.subplots(figsize=(20,5))
ax2 = ax1.twinx()
# plt.subplots_adjust(top=1.4)
ax2.fill_between(dryhydro_df['Time'],dryhydro_df['Flow [m³/s]'],0,facecolor='lightgrey')
ax2.set_ylim([0,10])
AB = ax2.fill_between(dryhydro_df['Time'],[12]*len(dryhydro_df['Time']),9.25,facecolor=colors[0],alpha=0.5,clip_on=False)
ab = ax2.scatter(presence_df['Datetime'][presence_df['AB']==True],[9.5]*sum(presence_df['AB']==True),marker='X',color='black')
# tidal heights
ax1.plot(tide_df['Time'],tide_df['Tide'],color='dimgrey')
I want the blue shaded region and black scatter to be above the plot. I can move the elements above the plot by using clip_on=False but I think I need to extend the space above the plot to do visualise it. Is there a way to do this? Mock-up of what I need is below:
You can use clip_on=False to draw outside the main plot. To position the elements, an xaxis transform helps. That way, x-values can be used in the x direction, while the y-direction uses "axes coordinates". ax.transAxes() uses "axes coordinates" for both directions.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
dates = pd.date_range('2018-07-01', '2018-07-31', freq='H')
xs = dates.to_numpy().astype(float)
ys = np.sin(xs * .091) * (np.sin(xs * .023) ** 2 + 1)
fig, ax1 = plt.subplots(figsize=(20, 5))
ax1.plot(dates, ys)
ax1.scatter(np.random.choice(dates, 10), np.repeat(1.05, 10), s=20, marker='*', transform=ax1.get_xaxis_transform(),
clip_on=False)
ax1.plot([0, 1], [1.05, 1.05], color='steelblue', lw=20, alpha=0.2, transform=ax1.transAxes, clip_on=False)
plt.tight_layout() # fit labels etc. nicely
plt.subplots_adjust(top=0.9) # make room for the additional elements
plt.show()

Show axis at center, but keep labels on the left [duplicate]

This question already has answers here:
How to move tick labels off left spine
(2 answers)
Closed 3 years ago.
I am attempting to plot a distribution which is centred around zero, and as such I want to show the y-axis spine at 0, but I want to keep the tick labels themselves to the left of the graph (i.e. outside the plot area). I thought this might be achievable through tick_params, but the labelleft option seems to keep the labels in the centre. A short example is as follows:
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1)
vals = np.random.normal(loc=0, scale=10, size=300)
bins = range(int(min(vals)), int(max(vals))+1)
fig, ax = plt.subplots(figsize=(15,5))
ax.hist(vals, bins=bins)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.grid(axis='y', which='major', alpha=0.5)
plt.show()
This gives you:
I would like the labels to be at the left end of the gridlines, rather than the centre of the plot.
Probably not the best solution, but you can set left spines invisible and draw a straight line at 0:
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.plot((0,0), (0,ax.get_ylim()[-1]),color='k',linewidth=1)
ax.grid(axis='y', which='major', alpha=0.5)
plt.show()
Output:
On possibility is to instruct the tick labels to use the "Axes coordinates" for their x position, and the "Data coordinates" for their y position. This implies changing their tranform property.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.transforms as transforms
np.random.seed(1)
vals = np.random.normal(loc=0, scale=10, size=300)
bins = range(int(min(vals)), int(max(vals))+1)
fig, ax = plt.subplots()
ax.hist(vals, bins=bins)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.grid(axis='y', which='major', alpha=0.5)
trans = transforms.blended_transform_factory(ax.transAxes,ax.transData)
plt.setp(ax.get_yticklabels(), 'transform', trans)
plt.show()

Color axis spine with multiple colors using matplotlib

Is it possible to color axis spine with multiple colors using matplotlib in python?
Desired output style:
You can use a LineCollection to create a multicolored line. You can then use the xaxis-transform to keep it fixed to the xaxis, independent of the y-limits. Setting the actual spine invisible and turning clip_on off makes the LineCollection look like the axis spine.
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
import numpy as np
fig, ax = plt.subplots()
colors=["b","r","lightgreen","gold"]
x=[0,.25,.5,.75,1]
y=[0,0,0,0,0]
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
lc = LineCollection(segments,colors=colors, linewidth=2,
transform=ax.get_xaxis_transform(), clip_on=False )
ax.add_collection(lc)
ax.spines["bottom"].set_visible(False)
ax.set_xticks(x)
plt.show()
Here is a slightly different solution. If you don't want to recolor the complete axis, you can use zorder to make sure the colored line segments are visible on top of the original axis.
After drawing the main plot:
save the x and y limits
draw a horizontal line at ylims[0] between the chosen x-values with the desired color
clipping should be switched off to allow the line to be visible outside the strict plot area
zorder should be high enough to put the new line in front of the axes
the saved x and y limits need to be put back, because drawing extra lines moved them (alternatively, you might have turned off autoscaling the axes limits by calling plt.autoscale(False) before drawing the colored axes)
from matplotlib import pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)
for i in range(10):
plt.plot(x, np.sin(x*(1-i/50)), c=plt.cm.plasma(i/12))
xlims = plt.xlim()
ylims = plt.ylim()
plt.hlines(ylims[0], 0, 10, color='limegreen', lw=1, zorder=4, clip_on=False)
plt.hlines(ylims[0], 10, 20, color='crimson', lw=1, zorder=4, clip_on=False)
plt.vlines(xlims[0], -1, 0, color='limegreen', lw=1, zorder=4, clip_on=False)
plt.vlines(xlims[0], 0, 1, color='crimson', lw=1, zorder=4, clip_on=False)
plt.xlim(xlims)
plt.ylim(ylims)
plt.show()
To highlight an area on the x-axis, also axvline or axvspan can be interesting. An example:
from matplotlib import pyplot as plt
import numpy as np
x = np.linspace(0, 25, 100)
for i in range(10):
plt.plot(x, np.sin(x)*(1-i/20), c=plt.cm.plasma(i/12))
plt.axvspan(10, 20, color='paleturquoise', alpha=0.5)
plt.show()

Relative size of subplots in matplotlib

I am trying to achieve generate plot made of subplots: plt.plot() and plt.matshow(), in which two plots had exactly the same size. What I mean is that lower border of one plot and lower borders of second plots were located on same "height". Similarly with the top border line. Current effect is presented on the plot below.
I haven't found any way in the available resources which would help me to achieve this effect. I would be grateful if you could help me.
shape=(2500, 2500)
matrix=np.zeros(shape)
print "Start of computing"
for x in range(shape[0]) :
for y in range(shape[1]) :
matrix[x, y]=shapeFuction((x-shape[0]/2)/13.0, (y-shape[1]/2)/13.0, 2.0e-4, 9e-5, 1.0)
print "Start of plotting"
fig=plt.figure()
ax = fig.add_subplot(1,2,2, aspect=1)
ax.matshow(matrix, cmap="autumn") #data[250:501,150:351])
ax.set(adjustable='datalim', aspect=1)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.xaxis.set_ticks_position('bottom')
ax.set(adjustable='box-forced') #adjustable='datalim'
ax.grid(b=False)
print "Start of plotting part 2"
ax = fig.add_subplot(1,2,1)
phase=(9.0e-5*np.power(np.arange(0, shape[1])-shape[1]/2,3 ))/7
g=ax.get_ylim()
asp=shape[1]/float(abs(g[0]-g[1]))
ax.plot(phase) #data[250:501,150:351])
ax.set(adjustable='box-forced')#, aspect=1.06/6.0) #adjustable='datalim''box-forced'
ax.set_xlabel("x")
ax.set_ylabel("Phase")
plt.savefig('testData-x3.png')
# plt.show()
One option you have is to set the aspect of the imshow plot (which is usually 1, such that pixels are squared), to "auto", ax2.imshow(z, cmap="autumn", aspect="auto").
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3,3)
y = np.tan(x)
z = np.random.rand(30,30)
fig, (ax, ax2) = plt.subplots(ncols=2)
ax.plot(x,y)
ax2.imshow(z, cmap="autumn", aspect="auto")
plt.show()
If instead you want to keep the aspect ratio of the image plot, you can change the aspect of the line plot by comparing the different axis limits,
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3,3)
y = np.tan(x)
z = np.random.rand(30,30)
fig, (ax, ax2) = plt.subplots(ncols=2)
ax.plot(x,y)
ax2.imshow(z, cmap="autumn")
ratio = np.diff(ax.get_ylim())[0]/np.diff(ax.get_xlim())[0]
ratio2 = np.diff(ax2.get_ylim())[0]/np.diff(ax2.get_xlim())[0]
aspect = ratio2/ratio
ax.set_aspect(float(np.abs(aspect)))
plt.show()

How to remove gaps between subplots in matplotlib

The code below produces gaps between the subplots. How do I remove the gaps between the subplots and make the image a tight grid?
import matplotlib.pyplot as plt
for i in range(16):
i = i + 1
ax1 = plt.subplot(4, 4, i)
plt.axis('on')
ax1.set_xticklabels([])
ax1.set_yticklabels([])
ax1.set_aspect('equal')
plt.subplots_adjust(wspace=None, hspace=None)
plt.show()
The problem is the use of aspect='equal', which prevents the subplots from stretching to an arbitrary aspect ratio and filling up all the empty space.
Normally, this would work:
import matplotlib.pyplot as plt
ax = [plt.subplot(2,2,i+1) for i in range(4)]
for a in ax:
a.set_xticklabels([])
a.set_yticklabels([])
plt.subplots_adjust(wspace=0, hspace=0)
The result is this:
However, with aspect='equal', as in the following code:
import matplotlib.pyplot as plt
ax = [plt.subplot(2,2,i+1) for i in range(4)]
for a in ax:
a.set_xticklabels([])
a.set_yticklabels([])
a.set_aspect('equal')
plt.subplots_adjust(wspace=0, hspace=0)
This is what we get:
The difference in this second case is that you've forced the x- and y-axes to have the same number of units/pixel. Since the axes go from 0 to 1 by default (i.e., before you plot anything), using aspect='equal' forces each axis to be a square. Since the figure is not a square, pyplot adds in extra spacing between the axes horizontally.
To get around this problem, you can set your figure to have the correct aspect ratio. We're going to use the object-oriented pyplot interface here, which I consider to be superior in general:
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8,8)) # Notice the equal aspect ratio
ax = [fig.add_subplot(2,2,i+1) for i in range(4)]
for a in ax:
a.set_xticklabels([])
a.set_yticklabels([])
a.set_aspect('equal')
fig.subplots_adjust(wspace=0, hspace=0)
Here's the result:
You can use gridspec to control the spacing between axes. There's more information here.
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
plt.figure(figsize = (4,4))
gs1 = gridspec.GridSpec(4, 4)
gs1.update(wspace=0.025, hspace=0.05) # set the spacing between axes.
for i in range(16):
# i = i + 1 # grid spec indexes from 0
ax1 = plt.subplot(gs1[i])
plt.axis('on')
ax1.set_xticklabels([])
ax1.set_yticklabels([])
ax1.set_aspect('equal')
plt.show()
Without resorting gridspec entirely, the following might also be used to remove the gaps by setting wspace and hspace to zero:
import matplotlib.pyplot as plt
plt.clf()
f, axarr = plt.subplots(4, 4, gridspec_kw = {'wspace':0, 'hspace':0})
for i, ax in enumerate(f.axes):
ax.grid('on', linestyle='--')
ax.set_xticklabels([])
ax.set_yticklabels([])
plt.show()
plt.close()
Resulting in:
With recent matplotlib versions you might want to try Constrained Layout. This does (or at least did) not work with plt.subplot() however, so you need to use plt.subplots() instead:
fig, axs = plt.subplots(4, 4, constrained_layout=True)
Have you tried plt.tight_layout()?
with plt.tight_layout()
without it:
Or: something like this (use add_axes)
left=[0.1,0.3,0.5,0.7]
width=[0.2,0.2, 0.2, 0.2]
rectLS=[]
for x in left:
for y in left:
rectLS.append([x, y, 0.2, 0.2])
axLS=[]
fig=plt.figure()
axLS.append(fig.add_axes(rectLS[0]))
for i in [1,2,3]:
axLS.append(fig.add_axes(rectLS[i],sharey=axLS[-1]))
axLS.append(fig.add_axes(rectLS[4]))
for i in [1,2,3]:
axLS.append(fig.add_axes(rectLS[i+4],sharex=axLS[i],sharey=axLS[-1]))
axLS.append(fig.add_axes(rectLS[8]))
for i in [5,6,7]:
axLS.append(fig.add_axes(rectLS[i+4],sharex=axLS[i],sharey=axLS[-1]))
axLS.append(fig.add_axes(rectLS[12]))
for i in [9,10,11]:
axLS.append(fig.add_axes(rectLS[i+4],sharex=axLS[i],sharey=axLS[-1]))
If you don't need to share axes, then simply axLS=map(fig.add_axes, rectLS)
Another method is to use the pad keyword from plt.subplots_adjust(), which also accepts negative values:
import matplotlib.pyplot as plt
ax = [plt.subplot(2,2,i+1) for i in range(4)]
for a in ax:
a.set_xticklabels([])
a.set_yticklabels([])
plt.subplots_adjust(pad=-5.0)
Additionally, to remove the white at the outer fringe of all subplots (i.e. the canvas), always save with plt.savefig(fname, bbox_inches="tight").

Categories