How to change the font in a calplot plot? - python

I made a calendar heatmap using calplot. The documentation for calplot is here: https://calplot.readthedocs.io/en/latest/.
I want to change the font of the title and labels, but do not know how to change the label font. I only managed to change the title font. Calplot is built upon matplotlib and it can pass on arguments to matplotlib.
First I exclusively focused on matplotlib and changed its font like this:
csfont = {'fontname':'Open Sans'}
hfont = {'fontname':'Open Sans'}
plt.title('title',**csfont)
plt.xlabel('xlabel', **hfont)
I then looked at the documentation of calplot and noticed the parameter suptitle_kws. I then tried to pass csfont as follows:
calplot.calplot(..., suptitle_kws=csfont)
And this correctly changed the title font. However, I also want to change the other fonts (of the weekdays and the colorbar). I don't know how to do this.

Related

Customising legend in Matplotlib

I’m trying to customise individual legend labels. In the example below, the legend contains two items. I’d like to make the text bold for only the second legend label.
Here’s a general outline of the code:
leg = plt.legend()
for text in leg.get_texts():
text.set_fontweight...
Here’s a runnable example:
import matplotlib.pyplot as plt
X=range(10)
Y=range(100,110)
Z=range(105,115)
plt.plot(X,Y,label='normal')
plt.plot(X,Z,label='bold')
fontweights=['normal','bold']
leg=plt.legend()
for fw,text in zip(fontweights,leg.get_texts()):
text.set_fontweight(fw)
plt.show()
Here's the output:
enter image description here
The plot produced shows that set_fontweight() changes both labels to bold. So is this a bug with set_fontweight(), or am I doing something wrong?
Similar functions, such as text.set_color(), can be used to modify legend labels individually.
Lastly, I’m using matplotlib version 3.2.2.
Thanks!
I have matplotlib 3.3.4 running, and get a bold and unbold legend entry - see attached image - so I think an upgrade should fix the problem.

Legend title pad in matplotlib

I would like the title in the legend of my matplotlib figure to be more distant from the content of the legend. Currently, I have the following:
I see the set_title function of the Legend class accepts a prop dictionary, which should be the one described in the text properties page. This one contains the field bbox, where a pad property could be added. But when I try something like the following
legend.set_title('Legend', prop={'bbox':{'pad':somepad}})
python complains that bbox is not an accepted parameter.
I'm using matplotlib 2.1.0 under Python 3.6.3 on Arch Linux.
An obvious workaround would be add a linebreak, like this:
legend.set_title('Legend\n ')
Although one might like the result, matplotlib has the great advantage that everything can be configured to the slightest detail, so I'm looking for a solution which gives me more fine-grained control over this spacing.
Of course introducing a linebreak in the title text as legend.set_title('Legend\n ') is a valid option.
If you don't like that you can set the separation between title and legend handle box manually as
legend._legend_box.sep = 20
Complete example:
import matplotlib.pyplot as plt
plt.plot([1,2], label="some")
plt.plot([1,3], label="label")
legend = plt.legend(title="Legend title", ncol=2)
legend._legend_box.sep = 20
plt.show()
The default separation is labelspacing * fontsize, hence
plt.rcParams["legend.labelspacing"] * plt.rcParams["font.size"] == 0.5 * 10 == 5

python matplotlib make everything bold

Well , I am making some plots and wants to make everything in bold font. I can can use weight="bold" to make bold font of label to ticks. Can also use the prop={'weight':'bold'} to make the legends lines bold, but I can't make the legend title bold. So, 1st question is there a way to make the legend title bold?
2nd,I tried to used matplotlib latex support to make the title bold, that did it but if I use rc('text', usetex=True) I cant use weight=bold and have to use \textbf{} everytime, also how do I make the ticks bold in this way.
3rd , If I use latex support then the font changes that I don't like. How do use the normal matplotlib font with using latex?
Making everything bold is rather easy. Just add
plt.rcParams["font.weight"] = "bold"
plt.rcParams["axes.labelweight"] = "bold"
at the top of the script.
import matplotlib.pyplot as plt
plt.rcParams["font.weight"] = "bold"
plt.rcParams["axes.labelweight"] = "bold"
plt.plot([2,3,1], label="foo")
plt.plot([3,1,3], label="bar")
plt.legend(title="Legend Title")
plt.xlabel("xLabel")
plt.show()
You should be able to pass parameters into the plt.legend using the prop argument
legend_prop = {'weight':'bold'}
plt.plot(x, y, label='some label')
plt.legend(prop=legend_prop)
This would give you bold labels. Is this not what you're looking for?

Matplotlib xticks font size if string

I am trying to change the font size of my x axis that happens to be strings and not numbers. I have changed other charts that have integers by using :
plt.xticks(size=10)
However, this dose not work for some of my graphs that have months in the place of integers. Keep in mind i am not using subplots.
Try setting the fontsize either by using the the fontsize keyword argument or if you want to globally change it to every plot in your script by updating the rcParams as
import matplotlib
matplotlib.rc('xtick', labelsize=20)
Also look at this answer: How to change the font size on a matplotlib plot
Or here: How can I change the font size of ticks of axes object in matplotlib

How to change legend fontname in matplotlib

I would like to display a font in Times New Roman in the legend of a matplotlib plot.
I have changed all other tick labels/axis labels/titles to Times New Roman, and have searched the documentation but I can only find how to change the font size in a legend using the prop
argument in pyplot.legend()
Of course straight after posting, I found the answer. Solution for anyone else with the same issue:
import matplotlib as mpl
mpl.rc('font',family='Times New Roman')
This wasn't showing up in google results so I'm going to post it as an answer. The rc parameters for font can be used to set a single default font.
Solution for anyone else with the same issue:
import matplotlib as mpl
mpl.rc('font',family='Times New Roman')
The .rc solution given changes the default font for all drawing.
Here is a solution for doing this when you don't want to change all the fonts, but just the font properties of the legend of this particular graph (a legend belonging to a particular axis object):
L = ax.legend()
plt.setp(L.texts, family='Consolas')
This allows you to choose a different font for the legend and the axes. I found this helpful when I needed a monospace font for my legend, but not for the axes -- allowing me to create a neat legend like this:
Note how the title is a different font than the legend - this gives me an alignment of numbers that would otherwise be hard to achieve.
I think this is the better way.
import matplotlib.font_manager as fm
## your font directory
font_path = '/Users/frhyme/Library/Fonts/BMDOHYEON_otf.otf'
## font_name
font_name = fm.FontProperties(fname=font_path).get_name()
plt.legend(prop={'family':font_name, 'size':20})

Categories

Resources