How to show contents of axes object when using pandas - python-2.7

I'm trying to wrap my head around using axes-objects in conjunction with a Pandas-dataframe (object df in the following snippet):
import matplotlib.pyplot as plt
fig=plt.figure()
axes=fig.add_subplot(111)
for key, grp in df.groupby(['bondYear', 'bondQuarter']):
axes = grp['priceAvg'].plot(label=key, ax=axes)
This runs through without any complaints, but now I'm looking for a way to actually make my plot show (and later save to *.png like with using savefig()). plt.show() works but opens two windows, one showing my plot and the other one being empty, so that's not it.
Apparently I'm missing something regarding the use of matplotlib, can you help me out?
Many thanks in advance for your help!

You shouldn't reassign your plot to axes, although that should not matter. Try this.
fig, ax = plt.subplots(figsize=(8,6))
for key, grp in df.groupby(['bondYear', 'bondQuarter']):
grp['priceAvg'].plot(label=key, ax=ax)
plt.legend()
plt.show()
plt.savefig('figname.png')
You can also plot them all at once without using iteration doing the following but you are easily able to control the legend.
fig, ax = plt.subplots(figsize=(8,6))
df.groupby(['col1', 'col2']).plot(ax=ax)

Related

PyMC3 autocorrplot() for any given array

The autocorrplot() function gives the autocorrelation plot for the sampled data from the trace.
If I already have a sample of data in the form of an array or list, can I use autocorrplot() to do the same?
Is there any alternative to generate autocorrelation plots given a sequence of data?
Please help.
autocorrplot is a wrapper around matplotlib's acorr. To get a similar look to pymc3's version, you can use something like
import numpy as np
import matplotlib.pyplot as plt
my_array = np.random.normal(size=1000)
plt.acorr(my_array, detrend=plt.mlab.detrend_mean, maxlags=100)
plt.xlim(0, 100)
Note the call to xlim at the end, since by default PyMC3 does not show negative correlations.

Edit tick labels in logarithmic axis

I'm trying to edit the tick labels but I keep getting scientific notation, even after setting the ticks. Here is a MWE:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(9, 7))
fig.subplots_adjust(left=0.11, right=0.95, top=0.94)
ax.ticklabel_format(style='plain')
plt.plot([1,4],[3,6] )
ax.set_yscale('log')
ax.set_xscale('log')
ax.set_xticks([0.7,1,1.5,2,2.5,3,4,5])
ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
which produces this plot
As you can see ax.ticklabel_format(style='plain') doesn't seem to work as I keep getting tick labels in scientific notation, and when using ax.set_xticks the old tick labels are still present. I took a look at this topic and it seems like the problem is in the choose of the ticks, if I use for example 0.3 instead of 0.7 as the first tick it works, however I need to do a plot in this specific range and using log scale.
Any work around?
Actually, your code is doing what you need, the problem is the labels from the minor ticks that remain unaffected and overlap with the major ticks
you can simply add the line:
ax.get_xaxis().set_minor_formatter(matplotlib.ticker.NullFormatter())
full code:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(9, 7))
fig.subplots_adjust(left=0.11, right=0.95, top=0.94)
ax.ticklabel_format(style='plain')
plt.plot([1,4],[3,6] )
ax.set_yscale('log')
ax.set_xscale('log')
ax.set_xticks([0.7,1,1.5,2,2.5,3,4,5])
ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
ax.get_xaxis().set_minor_formatter(matplotlib.ticker.NullFormatter())

Use colorbar for non type object

I currently try to modify a plot in a python2.7 code. This plot come from a radiative transfert code (what I can't modify), and the output is a class type with a method :
plot_image(wavelength0, overplot=False, inclination=None, cmap=None,
ax=None, axes_units='AU', polarization=False, polfrac=False,
psf_fwhm=None, vmin=None, vmax=None, dynamic_range=1000000.0)
more informations here
So, when I put a plt.title() or plt.savefig() just after that, I call the respective method and the title of the plot changes and the plot is saved, as expected!
However, there is two things that I didn't find a way to do it :
Add a colorbar
trace the surface in log-scale
because no arguments in the method allow to do it.
When I try something like plt.colorbar(fig) with fig=res.plot_image(...) (with res the class, output of radiative transfert code) the error said that fig is not a mappable object (seems logic).
Any ideas that could help me ? hope I gave you enough informations.
Sorry for my English
Lo_du
If res.plot_image(...) only plots one image to one axes, the solution to obtain a colorbar might be pretty easy.
import numpy as np
import matplotlib.pyplot as plt
#.... some code
res.plot_image(...)
im, = plt.gca().get_images()
plt.colorbar(im)
plt.show()

How to add interrupted time series in matplotlib

Since I am just a python beginner I hope, that I bother you not too much with my question. I'd like to plot only parts of a time series from a dataset such as this:
Code:
import numpy as np
import matplotlib.pyplot as plt
a=np.arange(10)
b=np.arange(100, 105)
c=np.arange(30, 40)
d=np.arange(55, 60)
e=np.append(a,b)
f=np.append(c,d)
plt.plot(e,f)
Plot:
Then I get a plot with a long diagonal line between the data series. What to do to get rid of that, in other words I want that the x-axis only shows 0,1,2,3,4,5,6,7,8,9,100,101,102,103,104 and the corresponding y-values and nothing in between. Moreover if I have a very long time series (e.g. from an oscilloscope measurement), is it possible to show up that some patterns are repeated with e.g. big dots in the middle of the plot? I've tried to convert the x data into a string and several other things, but it does not seem to work.
Focusing on this part of your question:
Then I get a plot with a long diagonal line between the data series. What to do to get rid of that [...]
It sounds that a scatteplot should meet your needs, so try this:
Add this to your code:
fig, ax = plt.subplots()
ax.scatter(e, f)
for i, txt in enumerate(e):
ax.annotate(txt, (e[i],f[i]))
fig.show()
To get this plot:
Unfortunately, I'm not quite sure what you're trying to accomplish with this part:
Moreover if I have a very long time series (e.g. from an oscilloscope
measurement)
Anyway, I hope my suggestion at least fixes the main part of your problem.

Plot an IPython Notebook figure inline with fig.show()?

I'm calling the inline mode for IPython Notebook using;
%pylab inline
And the following code plots a figure immediately at the cell;
fig = plt.figure()
axes = fig.add_axes([0, 0, 1, 1])
However I would like to create the plot/axes etc. in one cell, and plot later using maybe;
fig.show()
How do I gain more control of the inline mode? If I don't use %pylab inline, it creates the plot in a seperate window which I don't want (and it usually freezes the window).
Versions;
Numpy: 1.7.0
Matplotlib: 1.2.1rc1
Python: 2.7.2 (default, Jun 24 2011, 12:22:14) [MSC v.1500 64 bit (AMD64)]
Pandas: 0.10.1
PyLab: 1.7.0
So I guess what you want is this:
from matplotlib.backends.backend_agg import FigureCanvasAgg as fc
fig = Figure()
canvas = fc(fig)
ax = fig.add_subplot(1, 1, 1)
ax.plot(arange(10))
To display the plot in another cell simply use:
fig
You might be looking for disabling autoclose figure :
InlineBackend options
---------------------
--InlineBackend.close_figures=<CBool>
Default: True
Close all figures at the end of each cell.
When True, ensures that each cell starts with no active figures, but it also
means that one must keep track of references in order to edit or redraw
figures in subsequent cells. This mode is ideal for the notebook, where
residual plots from other cells might be surprising.
When False, one must call figure() to create new figures. This means that
gcf() and getfigs() can reference figures created in other cells, and the
active figure can continue to be edited with pylab/pyplot methods that
reference the current active figure. This mode facilitates iterative editing
of figures, and behaves most consistently with other matplotlib backends,
but figure barriers between cells must be explicit.
still, IPython will show the figure if the last line of a cell return a fig object, you can avoid that by ending it with a ; or add pass as the last line.
With newer jupyter and matplotlib
Jupyter: 4.6
Jupyter notebook: 6.0
Matplotlib: 3.1
ipykernel: 5.1
all you really need is to create your figure with matplotlib.pyplot.Figure (in one cell) and then make that figure the cell value in another cell. For example
In cell [1]
%matplotlib inline
In cell [2]
from matplotlib.pyplot import Figure
from numpy import arange
from numpy.random import normal
fig = Figure()
ax = fig.add_subplot(111)
ax.plot(arange(10),normal(size=10),label='Data')
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
ax.legend();
and finally in cell [3]
fig
That should be enough. See the screenshot below
Note suggestions with matplotlib.pyplot.ioff() and similar does not work