Matplotlib xticks font size if string - python

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

Related

python plt barh does cannot properly display the bars

I have problems when drawing horizontal bar chart
firstly if we draw it in plt.bar
plt.figure(figsize=(8,5))
plt.bar(range_df.range_start, range_df.cum_trade_vol, width=30)
plt.show()
but if we draw it in plt.barh
plt.figure(figsize=(8, 5))
plt.barh(range_df.range_start, range_df.cum_trade_vol)
plt.show()
its either
or like:
The problem, I think, is because the crowded data that left too few gaps.
What can we do to properly draw the graph? (since we cannot set width with barh? or can we?)
Maybe another plot package?
Please do not reset the y axis value as the current value is important
The data can be downloaded at
https://drive.google.com/file/d/1y8fHazEFhVR_u2KL6uUsBqv0qmXOd2xT/view?usp=share_link
the notebook is at:
https://colab.research.google.com/drive/1MbjJE4B-mspDRqCYXnDf8hyFRK_uLmRp?usp=sharing
Thank you
I am somewhat unsure of what you are talking about. When writing the horizontal plot, you drop width parameter. There is an equivalent version for plt.barh which is height. So try
plt.barh(range_df.range_start, range_df.cum_trade_vol, height=30)

Why does the title overlap with the plot when specifying x using constrained_layout?

Solved: This problem occurred with matplotlib 3.4, updating to 3.5 fixed the issue.
I am plotting multiple subplots in a graph, which all have titles, labels and subplot titles. To keep everything visible and the right size, I am using constrained_layout.
I would like to add a title that is aligned to the left. However, when I specify the x position (even as 0.5 which is the default), the title overlaps with the graph.
My plots are much more complex, but this already shows my issue:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(10, 5), constrained_layout=True)
gs = fig.add_gridspec(1,1)
ax1 = fig.add_subplot(gs[0,0])
fig.suptitle('Title', ha='left')
Only changing the last line of code:
fig.suptitle('Title with x-position', x=0.5, ha='left')
I was first using tight layout, but switched to constrained_layout because tight_layout did not keep the specified size of the figure when exporting it.
I have also switched from subplots to gridspec because I read that constrained_layout does not support subplots.
I know I can add extra space with fig.set_constrained_layout_pads(h_pad=0.3), but this also adds space below the plots, which I would like to avoid.
Hopefully someone can tell me why this happens and how I can get a title aligned to the left that does not overlap with the plot!
The problem occurred in Matplotlib 3.4, updating to 3.5 fixed the issue

Unable to change the plot size with matplotlib and seaborn [duplicate]

I'm having trouble increasing the size of my plot figures using Seaborn (imported as sns). I'm using sns.pairplot to plot columns of a data frame against one another.
%matplotlib inline
plt.rcParams['figure.figsize']=10,10
columns=list(df.columns.values)
g=sns.pairplot(df, kind='reg', x_vars=columns,y_vars = ['Column 1'])
The plots populate with data just fine, but the figure size is too small.
I thought plot.rCParams['figure.figsize'] would control how large the figure is, but it doesn't seem to take effect. I've tried a few different suggestions from online boards, but nothing seems to work.
sns.pairplot
"Returns the underlying PairGrid instance for further tweaking"
...for instance changing the figure size:
g=sns.pairplot(df, kind='reg', x_vars=columns,y_vars = ['Column 1'])
g.fig.set_size_inches(15,15)
Try to put the size in parenthesis, this does the trick for me:
plt.rcParams['figure.figsize']=(10,10)
In addition to the well working answer by #MartinAnderson, seaborn itself provides the option to set the height of the subplots of the grid. In combination with the aspect this determines the overall size of the figure in dependence of the number of subplots in the grid.
In seaborn <= 0.8.1:
g = sns.pairplot(..., size=10, aspect=0.6)
In seaborn >= 0.9.0:
g = sns.pairplot(..., height=10, aspect=0.6)
Note that this applies to all seaborn functions which generate a figure level grid, like
pairplot, relplot, catplot, lmplot and the underlying PairGrid or FacetGrid.
For other seaborn plots, which directly plot to axes, the solutions from How do you change the size of figures drawn with matplotlib? will work fine.
If we would like to change only the height or only the width then you can do
g = sns.pairplot(df, kind='reg', x_vars=columns,y_vars = ['Column 1'])
g.fig.set_figheight(6)
g.fig.set_figwidth(10)
You can use set for style control : https://seaborn.pydata.org/generated/seaborn.set.html
sns.set(rc={'figure.figsize':(20,10)})
Reffering to Rahul's question about sns.catplot ( Unable to change the plot size with matplotlib and seaborn )
If you try in jupyter notebook:
plt.figure(figsize=(25,20))
sns.boxplot(x='CriticRating', y='AudienceRating', data=movies)
it is working, but
sns.boxplot(x='CriticRating', y='AudienceRating', data=movies)
plt.figure(figsize=(25,20))
is not working (plot is very small). It's important to add line plt.figure(figsize=(25,20)) before sns.boxplot()and include %matplotlib inline of course in order to display plot in jupyter.
You can use set for style control : https://seaborn.pydata.org/generated/seaborn.set.html
We can accomplish this using sns.set() For example:
sns.set(rc={'figure.figsize':(20,10)})
Though, the preferred syntax is:
sns.set_theme()
NB: sns.set() or sns.set_theme() is a high level command that will set the (parameters) for everything else in your notebook.
sns.set(rc={'figure.figsize':(20,10)}) will set this to be the figure size for everything else in your notebook

How to change a figure's size in Python Seaborn package

I'm having trouble increasing the size of my plot figures using Seaborn (imported as sns). I'm using sns.pairplot to plot columns of a data frame against one another.
%matplotlib inline
plt.rcParams['figure.figsize']=10,10
columns=list(df.columns.values)
g=sns.pairplot(df, kind='reg', x_vars=columns,y_vars = ['Column 1'])
The plots populate with data just fine, but the figure size is too small.
I thought plot.rCParams['figure.figsize'] would control how large the figure is, but it doesn't seem to take effect. I've tried a few different suggestions from online boards, but nothing seems to work.
sns.pairplot
"Returns the underlying PairGrid instance for further tweaking"
...for instance changing the figure size:
g=sns.pairplot(df, kind='reg', x_vars=columns,y_vars = ['Column 1'])
g.fig.set_size_inches(15,15)
Try to put the size in parenthesis, this does the trick for me:
plt.rcParams['figure.figsize']=(10,10)
In addition to the well working answer by #MartinAnderson, seaborn itself provides the option to set the height of the subplots of the grid. In combination with the aspect this determines the overall size of the figure in dependence of the number of subplots in the grid.
In seaborn <= 0.8.1:
g = sns.pairplot(..., size=10, aspect=0.6)
In seaborn >= 0.9.0:
g = sns.pairplot(..., height=10, aspect=0.6)
Note that this applies to all seaborn functions which generate a figure level grid, like
pairplot, relplot, catplot, lmplot and the underlying PairGrid or FacetGrid.
For other seaborn plots, which directly plot to axes, the solutions from How do you change the size of figures drawn with matplotlib? will work fine.
If we would like to change only the height or only the width then you can do
g = sns.pairplot(df, kind='reg', x_vars=columns,y_vars = ['Column 1'])
g.fig.set_figheight(6)
g.fig.set_figwidth(10)
You can use set for style control : https://seaborn.pydata.org/generated/seaborn.set.html
sns.set(rc={'figure.figsize':(20,10)})
Reffering to Rahul's question about sns.catplot ( Unable to change the plot size with matplotlib and seaborn )
If you try in jupyter notebook:
plt.figure(figsize=(25,20))
sns.boxplot(x='CriticRating', y='AudienceRating', data=movies)
it is working, but
sns.boxplot(x='CriticRating', y='AudienceRating', data=movies)
plt.figure(figsize=(25,20))
is not working (plot is very small). It's important to add line plt.figure(figsize=(25,20)) before sns.boxplot()and include %matplotlib inline of course in order to display plot in jupyter.
You can use set for style control : https://seaborn.pydata.org/generated/seaborn.set.html
We can accomplish this using sns.set() For example:
sns.set(rc={'figure.figsize':(20,10)})
Though, the preferred syntax is:
sns.set_theme()
NB: sns.set() or sns.set_theme() is a high level command that will set the (parameters) for everything else in your notebook.
sns.set(rc={'figure.figsize':(20,10)}) will set this to be the figure size for everything else in your notebook

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