Matplotlib - Setting a tick label's background colour - matplotlib

I have a subplot and its tick labels overlap with the data. I would like to set the x-tick labels to have a background colour (e.g. white). Currently I have only been able to find how to change the label's colour, but not the background. I know how to get the effect using a text object as shown below. (NB - I don't want the whole subplot's margin to be coloured, but just the tick label).
MWE
import matplotlib as mpl
rc_fonts = {
"text.usetex": True,
'text.latex.preview': True,
"font.size": 50,
'mathtext.default': 'regular',
'axes.titlesize': 55,
"axes.labelsize": 55,
"legend.fontsize": 50,
"xtick.labelsize": 50,
"ytick.labelsize": 50,
'figure.titlesize': 55,
'figure.figsize': (10, 6.5), # 15, 9.3
'text.latex.preamble': [
r"""\usepackage{lmodern,amsmath,amssymb,bm,physics,mathtools,nicefrac,letltxmacro,fixcmex}
"""],
"font.family": "serif",
"font.serif": "computer modern roman",
}
mpl.rcParams.update(rc_fonts)
import matplotlib.pylab as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes, InsetPosition, mark_inset
from numpy import linspace, sin
x = linspace(0, 1, 100)
plt.clf()
ax1 = plt.gca()
ax2 = plt.axes([0, 0, 1, 1], label=str(2))
ip = InsetPosition(ax1, [0.08, 0.63, 0.45, 0.3])
ax2.set_axes_locator(ip)
ax1.plot(x, x)
ax1.plot(x, x + 0.3)
ax1.set_xlim(0, 1)
ax1.set_ylim(0, 1)
ax2.xaxis.set_tick_params(labelcolor='r')
ax1.text(0.3, 0.3, '$1$', transform=ax1.transAxes, horizontalalignment='center', verticalalignment='center', color='black', backgroundcolor='white')

To set a label's background color you may use the same property as for a text, essentially because a label is a text.
plt.setp(ax2.get_xticklabels(), backgroundcolor="limegreen")
For more sophisticated backgrounds, you could also use the bbox property.
bbox = dict(boxstyle="round", ec="limegreen", fc="limegreen", alpha=0.5)
plt.setp(ax2.get_xticklabels(), bbox=bbox)

import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
ax.plot(np.linspace(0, 1, 5), np.random.rand(5))
# set xticklabels
xtl = []
for x in ax.get_xticks():
xtl += ['lbl: {:.1f}'.format(x)]
ax.set_xticklabels(xtl)
# modify labels
for tl in ax.get_xticklabels():
txt = tl.get_text()
if txt == 'lbl: 1.0':
txt += ' (!)'
tl.set_backgroundcolor('C3')
tl.set_text(txt)

Related

adjust the location of color bar in subplots containing color and line plots

I am new to python programming. I was trying to make two subplots using matplotlib containing a line plot (panel-a) and 2-D color plot using imshow() (panel-b). I want the colorbar to be shown on the right side with same size as the color plot and it should not be within the subplot box limit.
`
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime as dt
from mpl_toolkits.axes_grid1 import make_axes_locatable
# Panel (a)
x1 = np.linspace(2, -2, 5)
y1 = np.linspace(-2, 2, 5)
# Panel (b)
N = 10
arr = np.random.random((N, N))
x_lims = list(map(dt.datetime.fromtimestamp, [982376726, 982377321]))
x_lims = mdates.date2num(x_lims)
y_lims = [0, 40]
fig, ax = plt.subplots(2, 1, figsize=(14, 10))
ax[0].plot(x1, y1)
ax[0].set_ylim(-2, 2)
ax[0].set_xlim(2, -2)
ax[0].set_xticks([2, 1, 0, -1, -2])
ax[0].set_yticks([-2, -1, 0, 1, 2])
im = ax[1].imshow(arr, extent=[x_lims[0], x_lims[1], y_lims[0],
y_lims[1]],
aspect='auto')
divider = make_axes_locatable(ax[1])
cax = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(im, cax=cax, label="diff. en. flux")
ax[1].xaxis_date()
date_format = mdates.DateFormatter('%H:%M:%S')
ax[1].xaxis.set_major_formatter(date_format)

Save 3D plot in the correct position in python

I am trying to export my surface plot into a .png file. For some reason, the saving plot does not correspond to the 3D orientation of the plot showed in spyder. Here is my code:
import csv
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator
import matplotlib as mpl
import numpy as np
with open(r'path', 'r') as f:
voltpertime = list(csv.reader(f, delimiter=","))
voltpertime = np.array(voltpertime[0:], dtype=np.float)
Z= np.flipud(voltpertime)
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
# Make data.
X = np.arange(1, 36, 1)
Y = np.arange(-4, 8, 0.1)
X, Y = np.meshgrid(X, Y)
# Plot the surface.
norm = mpl.colors.Normalize(vmin=-0.5, vmax=7)
surf = ax.plot_surface(X, Y, Z, cmap=cm.jet, linewidth=1, antialiased=False, norm=norm)
# Customize the z axis.
ax.set_zlim(-3, 7)
ax.zaxis.set_major_locator(LinearLocator(4))
ax.zaxis.set_major_formatter('{x:.02f}')
plt.colorbar(surf, shrink=0.5, aspect=5, label='current (nA)', pad = 0.1)
plt.yticks((-4, -2, 0, 2, 4, 6, 8), ("8", "6", "4", "2", "0", "-2", "-4"))
# rotate the axes and update
for angle in range(160, 360):
ax.view_init(35, angle)
plt.draw()
plt.pause(.001)
fig.savefig(r'path',
transparent = True, bbox_inches= 'tight', dpi=600, edgecolor= None)
plt.show()
Here is the plot in spyder:
and here is the plot when I save it:
I want to export the plot exactly how it appears in spyder.
Any idea?
Thanks

How to change the tick length of 3D plot in matplotlib

A test code is in the following. When I add the length=10, the tick length does not change at all. Anyone can help?
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter((0, 0, 1), (0, 1, 0), (1, 0, 0))
ax.w_xaxis.line.set_color('red')
ax.w_yaxis.line.set_color('red')
ax.w_zaxis.line.set_color('red')
ax.w_zaxis.line.set_color('red')
ax.xaxis.label.set_color('red')
ax.yaxis.label.set_color('red')
ax.zaxis.label.set_color('red')
ax.tick_params(axis='x', colors='red')
ax.tick_params(axis='y', colors='red')
ax.tick_params(axis='z', colors='red') # add length = 10 does not change the length of ticks, why?
plt.show()

How to use same labels for shared x axes in Matplotlib?

I'm trying to translate two separate figure into one figure with sharing x axis. But there are something that I'm missing. For styling and ticks I normally use the codes
plt.xlabel(r'$\mathrm{2\theta\ (deg)}$')
plt.ylabel(r'$\mathrm{Intensity\ (a.u)}$')
plt.xlim(15,80)
plt.legend(loc=4,prop={'size':10})
params = {'legend.fontsize': 18,
'axes.labelsize': 18,
'axes.titlesize': 18,
'xtick.labelsize' :12,
'mathtext.fontset': 'cm',
'mathtext.rm': 'serif', }
matplotlib.rcParams.update(params)
plt.tick_params(
axis='both', which='both',
right='off', left='off',
top = 'off', bottom = 'off',
labelleft='off')
Now, I need to apply it for this shared figure. These includes following:
No ticks will be on figure.
Axes labels will be shared.
It would be better to load text files inside a loop.
What do I need to know for these improvements?
import matplotlib.pyplot as plt
from numpy import loadtxt
import matplotlib
f = plt.figure()
plt.subplots_adjust(hspace=0.001)
data = loadtxt("ES1.txt",float)
POS = data[:,0]
ESD = data[:,1]
ax1 = plt.subplot(311)
ax1.plot(POS, ESD, color="blue")
data = loadtxt("ES2.txt",float)
POS = data[:,0]
ESD = data[:,1]
ax2 = plt.subplot(312, sharex=ax1)
ax2.plot(POS, ESD, color="red")
yticklabels = ax1.get_yticklabels() + ax2.get_yticklabels()
plt.setp(yticklabels, visible=False)
plt.savefig('shared_xrd' + '.png', dpi=600, bbox_inches='tight')
It could be that the following code is more like you want it.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
params = {'legend.fontsize': 18,
'axes.labelsize': 18,
'axes.titlesize': 18,
'xtick.labelsize' :12,
'mathtext.fontset': 'cm',
'mathtext.rm': 'serif',
"xtick.bottom" : False,
"ytick.left" : False,
}
matplotlib.rcParams.update(params)
f, axes = plt.subplots(nrows=2, sharex=True)
plt.subplots_adjust(hspace=0.001, bottom=0.2)
colors=["blue", "red"]
for i in [0,1]:
data = np.loadtxt("ES{}.txt".format(i+1))
POS = data[:,0]
ESD = data[:,1]
axes[i].plot(POS, ESD, color=colors[i], label="data{}".format(i))
axes[i].legend(loc=4,prop={'size':10})
# make ticks invisble
axes[0].set_yticks([])
axes[1].set_yticks([])
plt.xlabel(r'$\mathrm{2\theta\ (deg)}$')
plt.xlim(15,80)
#create subplot just for placing the ylabel centered on all plots
shadowaxes = f.add_subplot(111, xticks=[], yticks=[], frame_on=False)
shadowaxes.set_ylabel(r'$\mathrm{Intensity\ (a.u)}$')
plt.savefig(__file__ + '.png', dpi=600, bbox_inches='tight')
plt.show()

3d tick labels do not display correctly

I am plotting 3d bar plots using mplot3d:
import numpy as np
import matplotlib
matplotlib.use("Qt4Agg")
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
result=[[0, 0, 5, 5, 14,40,50],
[0, 1, 8, 9, 20,50,70],
[0, 2, 8, 10, 25,60,80],
[0, 5, 10, 20, 40,75,100]]
result = np.array(result, dtype=np.int)
fig=plt.figure()
fig.set_size_inches(6, 4)
ax1=fig.add_subplot(111, projection='3d')
ax1.view_init(25, 280)
matplotlib.rcParams.update({'font.size': 12})
matplotlib.rcParams['font.weight']='normal'
xlabels = np.array(["Count1", "Count3","Count5", "Count6","Count7","Count8","Count9"])
xpos = np.arange(xlabels.shape[0])
ylabels = np.array(["5%","10%","20%","100%"])
ypos = np.arange(ylabels.shape[0])
xposM, yposM = np.meshgrid(xpos, ypos, copy=False)
zpos=result
zpos = zpos.ravel()
dx=0.75
dy=0.5
dz=zpos
ax1.w_xaxis.set_ticks(xpos + dx/2.)
ax1.w_xaxis.set_ticklabels(xlabels)
ax1.w_yaxis.set_ticks(ypos + dy/2)
ax1.set_yticklabels(ylabels)
ax1.w_zaxis.set_ticklabels(["","20%","40%","60%","80%","100%"])
colors = ['b','b','b','b','b','b','b','r','r','r','r','r','r','r','y','y','y','y','y','y','y','g','g','g','g','g','g','g']
ax1.bar3d(xposM.ravel(), yposM.ravel(), dz*0, dx, dy, dz, color=colors)
fig.savefig('tmp.tiff', dpi=300)
plt.close()
and here is what i got:
There are two problems here actually:
1) the y tick labels do not display correctly, they are supposed to be in the middle of the ticks but instead below the ticks. z tick labels are too close to the z ticks.
2) I suppose to use the font size 12 and the dpi should be higher than 300. I could not scale x axis such that the x tick labels fit nicely and do not overlap. I have tried multiply the xpos by 2. However the tick labels still overlap.