How to control facet height and width in plotnine? - python

I have seen this post regarding ggplot2:
How to automatically adjust the width of each facet for facet_wrap?
I was wondering:
how can I achieve the same in plotnine? I haven't found anything similar to grob. I would need it for a plot that is part of a python3 pipeline so I really don't want to convert part of the code to R.
Thanks for the help!

I checked the post you linked and proposed a far simpler solution, which is to use facet_grid rather than facet_wrap. When you use facet_grid, you can specify space='free' to automatically adjust the size of the facets, rather than the size of the scales only with scales=. There does not appear to be an option with facet_wrap to specify space= (only scales=).
I do not have direct experience using plotnine, but a quick check here shows that there exists a similar function that includes both space= and scales= arguments.

Related

How to add Label/text in Bokeh that can be auto rescaled?

I learnt how to add text by using Label in Bokeh in this question.
However, I found that the text doesn't rescale as I zoom in and out.
The ideal behavior is something like Patches, which becomes larger as you zoom in.
How can I configure for this feature?
Related Questions
Selectively show text in Bokeh plot based on zoom level
As of Bokeh 2.3 scalable text is still an open issue:
https://github.com/bokeh/bokeh/issues/9407
There are some potential partial workarounds discussed there, but nothing that concretely works all the time. Depending on your use case, you could potentially use CustomJS callback on the plot ranges to update the text size that you care about in some way.

How to make this chart easier to read?

I want to know how to get my x axis labels to display bigger so that the team labels aren't overlapping. I'm sure it's just a matter of configuring the chart size
My code:
plt.plot(prem_data.Team, prem_data.attack_scored,'o')
plt.plot(prem_data.Team, prem_data.defence_saves)
plt.xlabel("Team")
plt.ylabel("Attack goals scored & Defence tackles")
plt.legend(["attack scored", "defence saved"])
plt.show()
I can imagine there being two mutually non-exclusive solutions.
Directly alter the size of the font. This can be achieved via calling plt.rcParams.update({'font.size': <font_size>}), assuming that you have imported matplotlib.pyplot under the alias plt, as you have done in the source code provided. You would probably want to set the <font_size> to be small to prevent overlapping labels, but this would require some experimentation.
Increase the size of the figure. This can be done in a number of ways, but perhaps the simplest method you can implement with minimal edits to your current code would be to use the command plt.rcParams["figure.figsize"] = <fig_size> where <fig_size> is a tuple specifying the size of the figure in inches, such as (10, 5).
With some trial and error, you should be able to manipulate the size of the font and the figure to produce a plot with improved readability.
Note: The method for altering figure size I introduced above is not the most conventional way to go about this problem. Instead, it is much more common to use matplotlib.pyplot.figure or similar variants. For more information, I recommend that you check out this thread and the documentation.

Resetting default fonts/colours for plots in Python/Matplotlib

First time here and newbie so please bear with me.
I'm following along a data viz tutorial with matplotlib. I'm getting the same results (go me) but the font used (in the plot legend in particular) and the default co lours are different and no parameter has been set to change them. And of course the tutorial's stuff looks much nicer. I don't know why this happens but I'm thinking that maybe I did change some of the stuff in unrelated notebooks and those choices stuck. If so, how do I "reset" please?
I am not allowed to attach screenshots yet. Basically, my plot lines are light blue and orange while his are the traditional discrete dark blue and green. My legend font looks like bad excel while his looks like LateX.
Thank you for your suggestions.
There are a number of ways you can do this and they vary depending on the specific use case but the one I use most often is
from matplotlib import rcParams, rcParamsDefault
rcParams.update(rcParamsDefault)
Although it is likely the case that the tutorial is using a style other than the default so I wouldn't necessarily expect this to make your output look exactly like the tutorial. When using LaTex with matplotlib I use
pgf_with_latex = {
"text.usetex": True,
"pgf.rcfonts": False,
"pgf.preamble": [
r'\usepackage{color}'
]
}
rcParams.update(pgf_with_latex)
You can find more information here regarding customization and here regarding using LaTex with matplotlib.
To find all the built in matplotlib themes, use
plt.style.available
I think the normal one is 'default'. So add this before you make your plots:
plt.style.use(['default'])
But you can make your plots look even better than the tutorial by using some of the other ones.

a Pythonic way to draw a bump chart

I have a csv file that contains data I want to represent using a bump chart (as an example )
http://www.edwardtufte.com/bboard/q-and-a-fetch-msg?msg_id=0003nk
BUT I need to have some control over the formatting and would like to approach this in the most Pythonic way possible ( I am fairly new to Python using 3.5) so wonder if anyone could tell me which graphing package gives me the kind of control that would allow me to:
draw a bump chart
specify the font (Gill Sans)
specify the number of pixels per centimeter squared (about 10000)
If anyone has a package that is in the open source environment that would be the best fit for me I would be grateful.
SO I have accidently found my answer myself in the example I gave. A VERY splendid man called Pascal Schetalat has an EXCELLENT implementation in Python using Numpy and Pandas. Here is the link
http://nbviewer.ipython.org/gist/pascal-schetelat/8382651
thank YOU Pascal

Matplotlib figure changes for publication after making the figure

I have been running into the problem of generating publication 'perfect' images in Matplotlib (i.e changing the fontsize, marker size, figuresize, number of ticks etc...).
Essentially, I want to do what is described here: http://www.scipy.org/Cookbook/Matplotlib/LaTeX_Examples except after the figure has already been made. i.e I want to make a function that accepts the Figure object, and makes all the necessary changes, some of which are available as input arguments. That way I don't have to always modify my scripts when I decide to use a figure in a paper.
Is there an efficient way to achieve this?
Note I'm usually using Ipython.
Well, we are doing this all the time to create good figures. In our case we wanted something that would automatically choose the linestyle and marker because we always have to plot many lines in the same figure, and this would avoid manually specifying these things.
We programmed some functions so that at the end you need a plot.py script, which is very simple, and an input.txt with all the info. In the input file you can also use any parameter like linewidth, label, and so forth.... but by default it will follow the pre-defined order for linestyles, markers and so on.
Also, you need a good matplotlibrc file. You can have many different matplotlibrc files since the highest priority is given to the one at the local directory, as explained here. This will allow you to customize keeping your plotting code cleaner.
The functions commented above (with examples) are available in this link, maybe they can give you some insights.
They solved one problem I had with subplots, described here.
Maybe you can make use of matplotlib pickling available in vs 1.2.1.
As said in the above link:
Philip Elson added an experimental feature to make figures picklable
for quick and easy short-term storage of plots. Pickle files are not
designed for long term storage, are unsupported when restoring a
pickle saved in another matplotlib version and are insecure when
restoring a pickle from an untrusted source. Having said this, they
are useful for short term storage for later modification inside
matplotlib.
No personal experience, however

Categories