I am doing a plot with matplotlib and creating a legend for this (see code below). I want the legends rows be aligned horizontally such that the relations > and < are aligned. Trying to adapt this and this code of similar problems, i got stuck.
I understand the basic idea: use \makebox[width][alignment]{math expression before aligment}<math expression after alignment as label, such that the space used by that epsilon-expression always uses the same space and is aligned to the right, hence there is free space to the left.
But the \hfill-methods used in the links only work if there is text before it the hfill, or if the alignment is standard (left). The solution must be quite near and any help would be appreciated.
This is how the text of the legend should look like
import numpy
from matplotlib import pyplot as plt
plt.rc('text', usetex=True) # needed for interpeting tex strings, but changes appearence of axis-tick labels also
fig = plt.figure(1,figsize=(12.0, 8.0))
plt.ion()
# does not align the '<', '<' and '>' in the legend
# plt.plot(numpy.random.rand(10), label=r'\makebox[2cm][r]{$\varepsilon_i$}$< -\xi$')
# plt.plot(numpy.random.rand(10), label=r'\makebox[2cm][r]{$|\varepsilon_i|$}$< \xi$')
# plt.plot(numpy.random.rand(10), label=r'\makebox[2cm][r]{$\varepsilon_i$}$ > \xi$')
# \hfill doesnt change anything
# plt.plot(numpy.random.rand(10), label=r'\makebox[2cm][r]{\hfill$\varepsilon_i$}$< -\xi$')
# plt.plot(numpy.random.rand(10), label=r'\makebox[2cm][r]{\hfill$|\varepsilon_i|$}$< \xi$')
# plt.plot(numpy.random.rand(10), label=r'\makebox[24cm][r]{\hfill$\varepsilon_i$}$ > \xi$')
# the relations are aligned, but i do not want to plot the 'bla' for this
plt.plot(numpy.random.rand(10), label=r'\makebox[2cm][r]{bla\hfill$\varepsilon_i$}$< -\xi$')
plt.plot(numpy.random.rand(10), label=r'\makebox[2cm][r]{bla\hfill$|\varepsilon_i|$}$< \xi$')
plt.plot(numpy.random.rand(10), label=r'\makebox[2cm][r]{bla\hfill$\varepsilon_i$}$ > \xi$')
plt.legend(loc='upper right')
plt.show()
Here's a solution where LaTeX perfectly alignes math, but the user has to take the pain to position it inside the legend. The idea is to
draw legend box in a given position with a placeholder
put an amsmath's array into it manually
Here's the code:
#!/usr/bin/python3
from numpy import arange
import matplotlib
from matplotlib import pyplot as plt
custom_preamble = {
"text.usetex": True,
"text.latex.preamble": [
r"\usepackage{amsmath}", # for the array macros
],
}
matplotlib.rcParams.update(custom_preamble)
x = arange(5)
y = arange(5)
fig = plt.figure()
ax = fig.add_subplot(111)
l1, = ax.plot(x, y)
l2, = ax.plot(x * 2, y)
l3, = ax.plot(x * 3, y)
leg = ax.legend(
[l1, l2, l3],
["", "", ""],
bbox_to_anchor = (0.98, 0.25),
handletextpad = 4, # space between lines and text -- used here as a placeholder
labelspacing = 0.1, # space between lines in a legend
)
leg.set_zorder(1)
ax.text(0.955, 0.21,
r"\begin{array}{rcl}"
r" \varepsilon_i & < & -\xi"
r"\\ |\varepsilon_i| & < & \xi"
r"\\ \varepsilon_i & > & \xi"
r"\end{array}",
transform = ax.transAxes,
horizontalalignment = 'right',
verticalalignment = 'top',
zorder = 5,
)
fig.savefig("mwe.png")
Result:
You might want to compile it twice: on the first compilation it might give You error, but all other tries would go fine.
As to a space between < sign in a legend -- it might be reduced with say:
ax.text(0.94, 0.21,
r"\begin{array}{r#{}c#{}l}"
r" \varepsilon_i \,\,& < &\,\, -\xi"
r"\\ |\varepsilon_i| \,\,& < &\,\, \xi"
r"\\ \varepsilon_i \,\,& > &\,\, \xi"
r"\end{array}",
(everything else the same). This gives:
Related
The following sample code will generate the donut chart I'll use as my example:
import matplotlib.pyplot as plt
%matplotlib inline
# Following should supposedly set the font correctly:
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = ['Muli'] + plt.rcParams['font.sans-serif']
plt.rcParams['font.weight'] = 'extra bold'
size_of_groups=[12,11,30,0.3]
colors = ['#a1daaa','#bbbbb4','#444511','#1afff2']
import matplotlib as mpl
mpl.rcParams['text.color'] = '#273859'
# Create a pieplot
my_pie,texts,_ = plt.pie(size_of_groups,radius = 1.2,colors=colors,autopct="%.1f%%",
textprops = {'color':'w',
'size':15 #, 'weight':"extra bold"
}, pctdistance=0.75, labeldistance=0.7) #pctdistance and labeldistance change label positions.
labels=['High','Low','Normal','NA']
plt.legend(my_pie,labels,loc='lower center',ncol=2,bbox_to_anchor=(0.5, -0.2))
plt.setp(my_pie, width=0.6, edgecolor='white')
fig1 = plt.gcf()
fig1.show()
The above outputs this:
Mostly, this is great. Finally I got a nice looking donut chart!
But there is just one last thing to finesse - when the portion of the donut chart is very small (like the 0.6%), I need the labels to be moved out of the chart, and possibly colored black instead.
I managed to do something similar for bar charts using plt.text, but I don't think that will be feasible with pie charts at all. I figure someone has definitely solved a similar problem before, but I can't readily fine any decent solutions.
Here is a way to move all percent-texts for patches smaller than some given amount (5 degrees in the code example). Note that this will also fail when there would be multiple small pieces close to each other.
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
size_of_groups = [12, 11, 30, 0.3]
colors = ['#a1daaa', '#bbbbb4', '#444511', '#1afff2']
my_pie, texts, pct_txts = plt.pie(size_of_groups, radius=1.2, colors=colors, autopct="%.1f%%",
textprops={'color': 'w', 'size': 15}, pctdistance=0.75,
labeldistance=0.7)
labels = ['High', 'Low', 'Normal', 'NA']
plt.legend(my_pie, labels, loc='lower center', ncol=2, bbox_to_anchor=(0.5, -0.2))
plt.setp(my_pie, width=0.6, edgecolor='white')
for patch, txt in zip(my_pie, pct_txts):
if (patch.theta2 - patch.theta1) <= 5:
# the angle at which the text is normally located
angle = (patch.theta2 + patch.theta1) / 2.
# new distance to the pie center
x = patch.r * 1.2 * np.cos(angle * np.pi / 180)
y = patch.r * 1.2 * np.sin(angle * np.pi / 180)
# move text to new position
txt.set_position((x, y))
txt.set_color('black')
plt.tight_layout()
plt.show()
I attempted a solution by tweaking the solution of ImportanceOfBeingErnest on a different problem given here. For some reason, the percentage sign is not being displayed in my system but you can figure that out
rad = 1.2 # Define a radius variable for later use
my_pie, texts, autotexts = plt.pie(size_of_groups, radius=rad, colors=colors, autopct="%.1f%%",
pctdistance=0.75, labeldistance=0.7, textprops={'color':'white', 'size':20})
# Rest of the code
cx, cy = 0, 0 # Center of the pie chart
for t in autotexts:
x, y = t.get_position()
text = t.get_text()
if float(text.strip('%')) < 1: # Here 1 is the target threshold percentage
angle = np.arctan2(y-cy, x-cx)
xt, yt = 1.1*rad*np.cos(angle)+cx, 1.1*rad*np.sin(angle)+cy
t.set_color("k")
t.set_position((xt,yt))
I am trying to recreate the look of figure below using matplotlib (source).
However, I am having issues with the placement of the ylabel. I want it at the top of the y-axis, as it is on the figure. I have tried setting its position with ax.yaxis.set_label_position(), but this only accepts left or right for the y-axis. Is there an option to control the position of the ylabel, or should I just use ax.text and set the text's position manually?
EDIT: As it turns out, the ax.set_ylabel(position=(x,y)) sets the position of the label relative to the graph coordinates. However, because of its horizontal rotation, the label is a little too much to the right, and position(x,y) does not seem to accept negative inputs. Is there a way to move the label a little to the left?
I include the code used to generate the skeleton of the figure here, even though it's rather messy.
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.preamble'] = [r"\usepackage[charter]{mathdesign}"]
mpl.rcParams['font.family'] = ['serif']
mpl.rcParams['font.size'] = 10
nb_procs = np.array([1, 2, 4, 12, 24, 48, 96, 192, 384])
def adjust_spines(ax, spines):
for loc, spine in ax.spines.items():
if loc in spines:
spine.set_position(('outward', 10)) # outward by 10 points
spine.set_smart_bounds(True)
else:
spine.set_color('none') # don't draw spine
# turn off ticks where there is no spine
if 'left' in spines:
ax.yaxis.set_ticks_position('left')
else:
# no yaxis ticks
ax.yaxis.set_ticks([])
if 'bottom' in spines:
ax.xaxis.set_ticks_position('bottom')
else:
# no xaxis ticks
ax.xaxis.set_ticks([])
# -- We create the figure.
figPres = plt.figure(figsize=(3,1.75))
axPres = figPres.add_subplot(111)
# -- We remove any superfluous decoration.
# Remove the axis decorations on the right and on the top.
axPres.spines['top'].set_visible(False)
axPres.spines['right'].set_visible(False)
# Make the remaining spines a light gray.
axPres.spines['bottom'].set_color('gray')
axPres.spines['left'].set_color('gray')
adjust_spines(axPres, ['left', 'bottom'])
# -- Set the x ticks.
axPres.set_xscale('log')
axPres.set_xlim((0.75,500))
axPres.set_xticks((nb_procs))
axPres.set_xticklabels( (r'1', r'2', r'4', r'12', r'24', r'48', r'96', r'192', r'384'), color='gray' )
axPres.xaxis.set_ticks_position('bottom')
for tic in axPres.xaxis.get_major_ticks():
tic.tick1On = tic.tick2On = False
# -- Set the y ticks.
axPres.set_ylim((0,1))
axPres.set_yticks((0.0,0.5,1.0))
axPres.set_yticklabels((r'0', '', r'1'))
axPres.yaxis.set_ticks_position('left')
axPres.tick_params(axis='y', colors='gray')
#for tac in axPres.yaxis.get_major_ticks():
# tac.tick1On = tac.tick2On = False
for toc in axPres.xaxis.get_minor_ticks():
toc.tick1On = toc.tick2On = False
# -- Set the titles of the axes.
axPres.set_ylabel(r"Efficacit\'e", color='gray', rotation='horizontal')
axPres.yaxis.set_label_position('right')
axPres.set_xlabel(r"Nombre de processeurs", color='gray')
plt.show()
You can move the ylabel using ax.yaxis.set_label_coords, which does accept negative numbers. For your example, I removed the line with set_label_position, and added:
axPres.yaxis.set_label_coords(-0.1,1.02)
It seems like the 3.5 version of matplotlib doesn't support the yaxis any more. I have found a workaround that gives similar result
axPres.set_ylabel(r"Efficacit\'e", loc="top", rotation="horizontal")
Some methods have meanwhile been deprecated. Here is a more recent approach.
I moved most of the style-options to the global style parameters.
You can find a list of available parameters with descriptions here.
I hope the rest is self-explanatory.
import matplotlib.pyplot as plt
import numpy as np
# Alternative: plt.rc_context()
plt.rcParams.update({
'figure.constrained_layout.use': True,
'font.size': 12,
'axes.edgecolor': 'gray',
'xtick.color': 'gray',
'ytick.color': 'gray',
'axes.labelcolor':'gray',
'axes.spines.right':False,
'axes.spines.top': False,
'xtick.direction': 'in',
'ytick.direction': 'in',
'xtick.major.size': 6,
'xtick.minor.size': 4,
'ytick.major.size': 6,
'ytick.minor.size': 4,
'xtick.major.pad': 15,
'xtick.minor.pad': 15,
'ytick.major.pad': 15,
'ytick.minor.pad': 15,
})
X = np.linspace(-2,2,500)
Y = np.exp(-X**2)
# Generate Sample Points
Sx = np.random.choice(X, 31)
Sy = np.exp(-Sx**2) + np.random.normal(scale=.02, size=31)
fig, ax = plt.subplots( figsize=(6,4) )
# Disjoin bottom / left spines by moving them outwards
ax.spines[['bottom', 'left']].set_position(('outward', 20))
# Set axis / spine lengths
ax.spines['bottom'].set_bounds(Sx.min(), Sx.max())
ax.spines['left'].set_bounds(0, Sy.max())
ax.set_yticks( ticks=[0, Sy.max()], labels=['0', '650 mW'])
ax.set_yticks( ticks=[(Sy.max()+Sy.min())/2], labels=[''], minor=True )
ax.set_xticks( ticks=[Sx.min(), Sx.max()], labels=['16', '19'])
ax.set_xticks( ticks=[0], labels=['17.2 GHz'], minor=True)
ax.set_ylabel('Output power', ha='left', y=1, rotation=0, labelpad=0)
ax.plot(X,Y, color='orange')
ax.plot(Sx, Sy, marker='o', markersize=3, linestyle='', color='black')
fig.savefig('so.png', dpi=300, bbox_inches='tight')
I am preparing a graph of latency percentile results. This is my pd.DataFrame looks like:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
result = pd.DataFrame(np.random.randint(133000, size=(5,3)), columns=list('ABC'), index=[99.0, 99.9, 99.99, 99.999, 99.9999])
I am using this function (commented lines are different pyplot methods I have already tried to achieve my goal):
def plot_latency_time_bar(result):
ind = np.arange(4)
means = []
stds = []
for index, row in result.iterrows():
means.append(np.mean([row[0]//1000, row[1]//1000, row[2]//1000]))
stds.append(np .std([row[0]//1000, row[1]//1000, row[2]//1000]))
plt.bar(result.index.values, means, 0.2, yerr=stds, align='center')
plt.xlabel('Percentile')
plt.ylabel('Latency')
plt.xticks(result.index.values)
# plt.xticks(ind, ('99.0', '99.9', '99.99', '99.999', '99.99999'))
# plt.autoscale(enable=False, axis='x', tight=False)
# plt.axis('auto')
# plt.margins(0.8, 0)
# plt.semilogx(basex=5)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
fig = plt.gcf()
fig.set_size_inches(15.5, 10.5)
And here is the figure:
As you can see bars for all percentiles above 99.0 overlaps and are completely unreadable. I would like to set some fixed space between ticks to have a same space between all of them.
Since you're using pandas, you can do all this from within that library:
means = df.mean(axis=1)/1000
stds = df.std(axis=1)/1000
means.plot.bar(yerr=stds, fc='b')
# Make some room for the x-axis tick labels
plt.subplots_adjust(bottom=0.2)
plt.show()
Not wishing to take anything away from xnx's answer (which is the most elegant way to do things given that you're working in pandas, and therefore likely the best answer for you) but the key insight you're missing is that, in matplotlib, the x positions of the data you're plotting and the x tick labels are independent things. If you say:
nominalX = np.arange( 1, 6 ) ** 2
y = np.arange( 1, 6 ) ** 4
positionalX = np.arange(len(y))
plt.bar( positionalX, y ) # graph y against the numbers 1..n
plt.gca().set(xticks=positionalX + 0.4, xticklabels=nominalX) # ...but superficially label the X values as something else
then that's different from tying positions to your nominal X values:
plt.bar( nominalX, y )
Note that I added 0.4 to the x position of the ticks, because that's half the default width of the bars bar( ..., width=0.8 )—so the ticks end up in the middle of the bar.
Below is a figure I created with matplotlib. The problem is pretty obvious -- the labels overlap and the whole thing is an unreadable mess.
I tried calling tight_layout for each subplot, but this crashes my ipython-notebook kernel.
What can I do to fix the layout? Acceptable approaches include fixing the xlabel, ylabel, and title for each subplot, but another (and perhaps better) approach would be to have a single xlabel, ylabel and title for the entire figure.
Here's the loop I used to generate the above subplots:
for i, sub in enumerate(datalist):
subnum = i + start_with
subplot(3, 4, i)
# format data (sub is a PANDAS dataframe)
xdat = sub['x'][(sub['in_trl'] == True) & (sub['x'].notnull()) & (sub['y'].notnull())]
ydat = sub['y'][(sub['in_trl'] == True) & (sub['x'].notnull()) & (sub['y'].notnull())]
# plot
hist2d(xdat, ydat, bins=1000)
plot(0, 0, 'ro') # origin
title('Subject {0} in-Trial Gaze'.format(subnum))
xlabel('Horizontal Offset (degrees visual angle)')
ylabel('Vertical Offset (degrees visual angle)')
xlim([-.005, .005])
ylim([-.005, .005])
# tight_layout # crashes ipython-notebook kernel
show()
Update:
Okay, so ImageGrid seems to be the way to go, but my figure is still looking a bit wonky:
Here's the code I used:
fig = figure(dpi=300)
grid = ImageGrid(fig, 111, nrows_ncols=(3, 4), axes_pad=0.1)
for gridax, (i, sub) in zip(grid, enumerate(eyelink_data)):
subnum = i + start_with
# format data
xdat = sub['x'][(sub['in_trl'] == True) & (sub['x'].notnull()) & (sub['y'].notnull())]
ydat = sub['y'][(sub['in_trl'] == True) & (sub['x'].notnull()) & (sub['y'].notnull())]
# plot
gridax.hist2d(xdat, ydat, bins=1000)
plot(0, 0, 'ro') # origin
title('Subject {0} in-Trial Gaze'.format(subnum))
xlabel('Horizontal Offset\n(degrees visual angle)')
ylabel('Vertical Offset\n(degrees visual angle)')
xlim([-.005, .005])
ylim([-.005, .005])
show()
You want ImageGrid (tutorial).
First example lifted directly from that link (and lightly modified):
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np
im = np.arange(100)
im.shape = 10, 10
fig = plt.figure(1, (4., 4.))
grid = ImageGrid(fig, 111, # similar to subplot(111)
nrows_ncols = (2, 2), # creates 2x2 grid of axes
axes_pad=0.1, # pad between axes in inch.
aspect=False, # do not force aspect='equal'
)
for i in range(4):
grid[i].imshow(im) # The AxesGrid object work as a list of axes.
plt.show()
I am plotting a piechart with matplotlib using the following code:
ax = axes([0.1, 0.1, 0.6, 0.6])
labels = 'Twice Daily', 'Daily', '3-4 times per week', 'Once per week','Occasionally'
fracs = [20,50,10,10,10]
explode=(0, 0, 0, 0,0.1)
patches, texts, autotexts = ax.pie(fracs, labels=labels, explode = explode,
autopct='%1.1f%%', shadow =True)
proptease = fm.FontProperties()
proptease.set_size('xx-small')
setp(autotexts, fontproperties=proptease)
setp(texts, fontproperties=proptease)
rcParams['legend.fontsize'] = 7.0
savefig("pie1")
This produces the following piechart.
However, I want to start the pie-chart with the first wedge on top, the only solution I could find for this was using this code
However on using this as below,
from pylab import *
from matplotlib import font_manager as fm
from matplotlib.transforms import Affine2D
from matplotlib.patches import Circle, Wedge, Polygon
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
labels = 'Twice Daily', 'Daily', '3-4 times per week', 'Once per week','Occasionally'
fracs = [20,50,10,10,10]
wedges, plt_labels = ax.pie(fracs, labels=labels)
ax.axis('equal')
starting_angle = 90
rotation = Affine2D().rotate(np.radians(starting_angle))
for wedge, label in zip(wedges, plt_labels):
label.set_position(rotation.transform(label.get_position()))
if label._x > 0:
label.set_horizontalalignment('left')
else:
label.set_horizontalalignment('right')
wedge._path = wedge._path.transformed(rotation)
plt.savefig("pie2")
This produces the following pie chart
However, this does not print the fracs on the wedges as in the earlier pie chart. I have tried a few different things, but I am not able to preserve the fracs. How can I start the first wedge at noon and display the fracs on the wedges as well??
Ordinarily I wouldn't recommend changing the source of a tool, but it's hacky to fix this outside and easy inside. So here's what I'd do if you needed this to work Right Now(tm), and sometimes you do..
In the file matplotlib/axes.py, change the declaration of the pie function to
def pie(self, x, explode=None, labels=None, colors=None,
autopct=None, pctdistance=0.6, shadow=False,
labeldistance=1.1, start_angle=None):
i.e. simply add start_angle=None to the end of the arguments.
Then add the five lines bracketed by "# addition".
for frac, label, expl in cbook.safezip(x,labels, explode):
x, y = center
theta2 = theta1 + frac
thetam = 2*math.pi*0.5*(theta1+theta2)
# addition begins here
if start_angle is not None and i == 0:
dtheta = (thetam - start_angle)/(2*math.pi)
theta1 -= dtheta
theta2 -= dtheta
thetam = start_angle
# addition ends here
x += expl*math.cos(thetam)
y += expl*math.sin(thetam)
Then if start_angle is None, nothing happens, but if start_angle has a value, then that's the location that the first slice (in this case the 20%) is centred on. For example,
patches, texts, autotexts = ax.pie(fracs, labels=labels, explode = explode,
autopct='%1.1f%%', shadow =True, start_angle=0.75*pi)
produces
Note that in general you should avoid doing this, patching the source I mean, but there are times in the past when I've been on deadline and simply wanted something Now(tm), so there you go..