Labels alignmnet pie chart / matplotlib 2.1.0+ - matplotlib

I am having a question regarding a pie-chart labels alignment.
I'd like to have labels outside the pie-chart and centered to each wedge.
According to the documentation page, "labeldistance" parameter enables to place labels outside the pie-chart, and "ha" & "va" parameters supposed to center. However, these two options (ha & va) seems not working on Matplotlib v2.1.0+.
1) With this example (pls see below), you can see that "car" label is not centered properly, it is a bit off the center.
import matplotlib.pyplot as plt
figure = plt.figure()
axes = figure.add_subplot(111)
axes.set_aspect(1) # circular pie
y = [1,2,3, 4,8,16,18]
label = ['car','domino', 'romancical','testing1', 'thisisthelonglabel',
'fffffffffffffffffffffffffffffffffffffffffff', 'as']
wedges, texts = plt.pie(y,
radius=1.2,
labels=label,
labeldistance=1.0,
rotatelabels =True,
startangle = 10,
wedgeprops = {"linewidth": 0.7,
"edgecolor": "white"},
textprops = dict(ha="center",
va="center")) # doesn't work
plt.show()
I added the following lines to force labels to be centered, which works but disabled "labeldistance" parameter. So all my centered correctly, as I want labels are overlapping with the pie-chart circle.
wedges, texts = plt.pie(y,
radius=1.2,
labels=label,
labeldistance=1.0,
rotatelabels =True,
startangle = 10,
wedgeprops = {"linewidth": 0.7,
"edgecolor": "white"},
textprops = dict(ha="center",
va="center"))
for t in texts:
t.set_horizontalalignment("center")
t.set_verticalalignment("center")
plt.show()
So my question is, do "ha" & "va" options work for other users?
And could anyone advice if there is a solution for keeping "labeldistance" while using .set_horizontalalignment("center") and set_verticalalignment("center") ?
Thank you.

In matplotlib 3.0.2 as well as 2.1.2, using
textprops = dict(va="center", rotation_mode = 'anchor')
(and labeldistance=1.05) results in
Note that this leaves out the ha="center" option, because the horizontal alignment is best be automatically set depending on if the label is on the left or right side of the circle.
For an explanation of rotation_mode see e.g. this question or this question.

Related

How do we align marker and text in legends vertically in Matplotlib?

When the marker in a legend is a dot, dot and text are not aligned vertically. To solve this I tried following:
l = ax.legend()
for text in l.texts:
text.set_va('center') # Is there some setting for this in matplotlibrc, too??
plt.show()
The vertical alignment of text in a legend seems to be baseline. But no matter whether I choose center, bottom or baseline, etc., things are off:
Zooming in, this is what Matplotlib gives us out of the box:
What I want is also what other software like Inkscape gives me, when aligning two objects vertically:
Can Matplotlib do this for me/us?
This appears to work:
Set it to display only a single scatterpoint per legend entry by setting scatterpoints=1 in the call to legend()
Set the vertical offset of this point to 0 by setting scatteryoffsets=[0] in the call to legend()
After creating the legend, iterate through its text labels and set their vertical alignment to center_baseline, using for t in l.get_texts(): t.set_va('center_baseline')
figure(figsize=(2,2))
scatter([0],[0],marker='s',s=20,label='Thing 1')
scatter([1],[0],marker='s',s=20,label='t')
scatter([2],[0],marker='s',s=20,label='T¹₁')
l = legend(scatterpoints=1,scatteryoffsets=[0],handletextpad=-0.5)
for t in l.get_texts(): t.set_va('center_baseline')
Here is what I do:
import numpy as np
import matplotlib
matplotlib.use('Agg')
matplotlib.rcParams['text.latex.preamble'] = r'\usepackage{amsmath}'
matplotlib.rc('text', usetex = True)
from matplotlib import pyplot as py
## setup figure
figure = py.figure(figsize = (7.5, 5.0))
axs = [py.subplot(1, 1, 1)]
## make plot
xs = np.linspace(0.0, np.pi, 100)
ys = np.sin(xs)
axs[0].plot(xs, ys, color = 'dodgerblue', label = r'$n = 1$')
ys = np.sin(2.0 * xs)
axs[0].plot(xs, ys, color = 'seagreen', label = r'$n = 2$')
axs[0].axhline(0.0, color = 'gray', linestyle = 'dashed')
## vertical alignment
legends = axs[0].legend(frameon = False, fontsize = 25, loc = 'lower left')
shift = np.average([_.get_window_extent(renderer = figure.canvas.get_renderer()).height for _ in legends.get_texts()])
shift /= 3.6
for _ in legends.get_texts():
_.set_va('center') ## va is alias for verticalalignment
_.set_position((0, shift))
## save figure
name = 'test.pdf'
py.tight_layout()
py.savefig(name)
py.close()
It is, however, complicated and requires manual adjustments,
I am still looking for better solutions.

Matplotlib: Multiple plots with same layout (no automatic layout)

I am trying to make several pie charts that I can then transition between in a presentation. For this, it would be very useful for the automatic layouting to... get out of the way. The problem is that whenever I change a label, the whole plot moves around on the canvas so that it fits perfectly. I'd like the plot to stay centered, so it occupies the same area every time. I have tried adding center=(0,0) to ax.pie(), but to no avail.
Two examples:
Image smaller, left
Image larger, right
Instead of that effect, I'd like the pie chart to be in the middle of the canvas and have the same size in both cases (and I'd then manually make sure that the labels are on canvas by setting large margins).
The code I use to generate these two images is:
import matplotlib.pyplot as plt
import numpy as np
# Draw labels, from
# https://matplotlib.org/3.2.2/gallery/pie_and_polar_charts/pie_and_donut_labels.html#sphx-glr-gallery-pie-and-polar-charts-pie-and-donut-labels-py
def make_labels(ax, wedges, labs):
bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72)
kw = dict(arrowprops=dict(arrowstyle="-"),
bbox=bbox_props,
zorder=0, va="center")
for i, p in enumerate(wedges):
if p.theta2-p.theta1 < 5:
continue
ang = (p.theta2 - p.theta1) / 2. + p.theta1
y = np.sin(np.deg2rad(ang))
x = np.cos(np.deg2rad(ang))
horizontalalignment = {-1: "right", 1: "left"}[int(np.sign(x))]
connectionstyle = "angle,angleA=0,angleB={}".format(ang)
kw["arrowprops"].update({"connectionstyle": connectionstyle})
ax.annotate(labs[i], xy=(x, y),
xytext=(1.1*x,1.1*y),
horizontalalignment=horizontalalignment, **kw)
kw=dict(autoscale_on=False, in_layout=False, xmargin=1, ymargin=1)
fig, ax = plt.subplots(figsize=(3, 3), dpi=100, subplot_kw=kw)
wedges, texts = ax.pie(x=[1,2,3], radius=1,
wedgeprops=dict(width=1),
pctdistance=0.7,
startangle=90,
textprops=dict(fontsize=8),
center=(0, 0))
make_labels(ax, wedges, ["long text", "b", "c"])
#make_labels(ax, wedges, ["a", "b", "long text"])
plt.show()
Thanks a lot in advance!
How are you saving your figures? It looks like you may be using savefig(..., bbox_inches='tight') which automatically resized the figure to include all the artists.
If I run your code with fig.savefig(..., bbox_inches=None), I get the following output

Horizontal Alignment of Ipywidgets / Interactive plotting

The goal ist to:
plot data based on a dropdown value
have multiple plots based on the same value
align them in a row (horizontal).
In my case the interactive_plot is the box with all plots in it, therefore I can't style them how I want it. How do I plot the two plots in one line?
def showMADetails(column=filter_unique):
plt.figure(1)
filtered_ma = ma_data[(ma_data['2'] == column)]
plt.bar(column, filtered_ma['GrossTurnoverBudget'], align='center', alpha=0.5)
plt.figure(2)
filtered_ma = ma_data[(ma_data['2'] == column)]
plt.bar(column, filtered_ma['Productive billableDays'], align='center', alpha=0.5)
interactive_plot = interactive(showMADetails)
output = interactive_plot.children[1]
output.layout.height = '400px'
output.layout.width = '200px'
interactive_plot
figured out that I was wrong. The plots aren't widgets, so I followed a wrong approach.
The interactive_plot controls the UI of the widgets.
To control the plots itself I need to control the layout via subplots. That is the solution.

Pandas, Bar Chart Annotations

How to properly give Annotations to Pandas Bar Charts?
I'm following Bar Chart Annotations with Pandas and MPL, but somehow I can't make it into my own code -- this is as far as I can go. What's wrong?
I've also found the following code from here:
def autolabel(rects):
# attach some text labels
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
'%d' % int(height),
ha='center', va='bottom')
autolabel(rects1)
autolabel(rects2)
But I don't how to apply that to my code either. Please help.
UPDATE:
Thank you #CT Zhu, for the answer. However, in your horizontal bars, you are still placing the text on top of bars, but I need the text show up within or along them, like this from my referenced article,
where s/he says,
"I am very parital to horizontal bar charts, as I really think they are easier to read, however, I understand that a lot of people would rather see this chart implemented in a regular bar chart. So, here is the code to do that; you will notice that a few things have changed in order to create the annotation"*
It appears your autolabel function is expecting a list of patches, sssuming your plot only those bars as its patches, we could do:
df = pd.DataFrame({'score':np.random.randn(6),
'person':[x*3 for x in list('ABCDEF')]})
def autolabel(rects):
x_pos = [rect.get_x() + rect.get_width()/2. for rect in rects]
y_pos = [rect.get_y() + 1.05*rect.get_height() for rect in rects]
#if height constant: hbars, vbars otherwise
if (np.diff([plt.getp(item, 'width') for item in rects])==0).all():
scores = [plt.getp(item, 'height') for item in rects]
else:
scores = [plt.getp(item, 'width') for item in rects]
# attach some text labels
for rect, x, y, s in zip(rects, x_pos, y_pos, scores):
ax.text(x,
y,
'%s'%s,
ha='center', va='bottom')
ax = df.set_index(['person']).plot(kind='barh', figsize=(10,7),
color=['dodgerblue', 'slategray'], fontsize=13)
ax.set_alpha(0.8)
ax.set_title("BarH")#,fontsize=18)
autolabel(ax.patches)
ax = df.set_index(['person']).plot(kind='bar', figsize=(10,7),
color=['dodgerblue', 'slategray'], fontsize=13)
ax.set_alpha(0.8)
ax.set_title("Bar")#,fontsize=18)
autolabel(ax.patches)

matplotlib: Controlling pie chart font color, line width

I'm using some simple matplotlib functions to draw a pie chart:
f = figure(...)
pie(fracs, explode=explode, ...)
However, I couldn't find out how to set a default font color, line color, font size – or pass them to pie(). How is it done?
Showing up a bit late for the party but I encountered this problem and didn't want to alter my rcParams.
You can resize the text for labels or auto-percents by keeping the text returned from creating your pie chart and modifying them appropriately using matplotlib.font_manager.
You can read more about using the matplotlib.font_manager here:
http://matplotlib.sourceforge.net/api/font_manager_api.html
Built in font sizes are listed in the api;
"size: Either an relative value of ‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’ or an absolute font size, e.g. 12"
from matplotlib import pyplot as plt
from matplotlib import font_manager as fm
fig = plt.figure(1, figsize=(6,6))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
plt.title('Raining Hogs and Dogs')
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15,30,45, 10]
patches, texts, autotexts = ax.pie(fracs, labels=labels, autopct='%1.1f%%')
proptease = fm.FontProperties()
proptease.set_size('xx-small')
plt.setp(autotexts, fontproperties=proptease)
plt.setp(texts, fontproperties=proptease)
plt.show()
Global default colors, line widths, sizes etc, can be adjusted with the rcParams dictionary:
import matplotlib
matplotlib.rcParams['text.color'] = 'r'
matplotlib.rcParams['lines.linewidth'] = 2
A complete list of params can be found here.
You could also adjust the line width after you draw your pie chart:
from matplotlib import pyplot as plt
fig = plt.figure(figsize=(8,8))
pieWedgesCollection = plt.pie([10,20,50,20],labels=("one","two","three","four"),colors=("b","g","r","y"))[0] #returns a list of matplotlib.patches.Wedge objects
pieWedgesCollection[0].set_lw(4) #adjust the line width of the first one.
Unfortunately, I can not figure out a way to adjust the font color or size of the pie chart labels from the pie method or the Wedge object. Looking in the source of axes.py (lines 4606 on matplotlib 99.1) they are created using the Axes.text method. This method can take a color and size argument but this is not currently used. Without editing the source, your only option may be to do it globally as described above.
matplotlib.rcParams['font.size'] = 24
does change the pie chart labels font size