I am using the function described here to get some diagnostics on multiple linear regressions.
The last code line reads # Fig and ax can be used to modify axes or plot properties after the fact.
My code:
cls = LRD(lm6_fit)
f,ax = cls();
Now, I have tried so many different ways to change the size of the figure but nothing worked...
if I run f? I some information about f, such as:
Type: Figure
String form: Figure(1000x1000)
Attributes
----------
patch
The `.Rectangle` instance representing the figure background patch.
suppressComposite
For multiple images, the figure will make composite images
depending on the renderer option_image_nocomposite function. If
*suppressComposite* is a boolean, this will override the renderer.
Init docstring:
Parameters
----------
figsize : 2-tuple of floats, default: :rc:`figure.figsize`
Figure dimension ``(width, height)`` in inches.
But I'm not sure how to modify the parameter figsize existing in f. I tried f.set_size_inches(K,L), for different values of K and L, but nothing changes.
Related
I was learning how to make graphs with python pandas. But I couldn't understand how this code works.
fig , ax = plt.subplots( ) ax = tips[['total_bill','tip']].plot.hist(alpha=0.5, bins=20, ax=ax)
I couldn't understand why the code words only when there is fig infront of ax.
Also I have no idea what 'ax=ax' means.
I found everywhere but I couldn't find the answer...
Pandas is using the library matplotlib to do the plotting. Try to read up a bit about how matploltib works, it will help you understand this code a bit.
Generally, plotting with matplotlib involves a figure and one or more axes. A figure can be thought of as a frame where multiple plots can be created inside. Each plot consists of an axes object which contains your x- and y-axis and so on.
With the command plt.subplots(), you create in a single function a figure object and one or more axes objects. If you pass no parameters to the function, just a single axes object will get created that is placed on the figure object. The figure and axes are returned as a tuple by the function in the form of (figure, axes). You are unpacking that tuple with the first line into the variable fig and ax.
Then, when you call the plotting function on your pandas data, you tell the function on which axes object to do the plotting. This is what the parameter ax means in that function. So you are telling the function to use your axes object that your variable ax is assigned to by setting the parameter ax to ax (ax = ax).
Doing ax = tips[['total_bill','tip']].plot... is redundant. The plotting function returns the axes object on which the plotting was performed by pandas. However, you are just overwriting your already existing axes with the returned axes, which in this case are the same object. This would only be needed if you don't pass the ax parameter to the plotting function, in which case pandas would create a brandnew figure and axes object for you and return the axes object in case you want to do any further tweaks to it.
I am working with a package that produces only a matplotlib.Axes object, but it produces exactly what I want for that particular figure.
It would be highly advantageous, however, to take that object and place it directly into a larger array of existing subplots. Something like the following:
fig, ax = plt.subplots(2, 2)
ax[0,0].plot(...stuff...)
ax[0,1].plot(...stuff...)
ax[1,0].plot(...stuff...)
foo = plot_likert.plot_likert(...stuff...); # <----- returns Axes object
ax[1,1] = foo # <----- transplant Axes object
fig.show(()
Obviously, it's not that simple. What happens (in jupyter, anyway) is that ax[1,1] shows up blank, and foo shows up as its own entity, nicely formatted.
Is there a way to do what I'm looking for?
I'm trying to make an animation with matplotlib, in this case a 3D scatter plot. I'm hitting a problem that I absolutely always hit when I try to do this, which is that I don't know what arguments I should pass to set_data, and I don't know how to find out. In this case, it apparently expects two arguments, despite it being a 3d plot.
Since I've experienced related problems often, rather than asking about the specifics of the particular plot I'm trying to animate, I will ask the general question: given an element of a MatPlotLib plot, how can I determine what arguments its set_data method expects, either by interrogating it, or by knowing where it's documented?
From an example for an Animated 3D random walk from the MatPlotLib documentation:
def update_lines(num, dataLines, lines):
for line, data in zip(lines, dataLines):
# NOTE: there is no .set_data() for 3 dim data...
line.set_data(data[0:2, :num])
line.set_3d_properties(data[2, :num])
return lines
So as confusing as you discovered it is set_data by itself is not meant for 3D data, as well as according to the docs it accepts:
2D array (rows are x, y) or two 1D arrays
Looking more at this example we can see that the set_3d_properties has been used altogether.
This whole update_lines was set as a callback parameter for animation.FuncAnimation.
Usually to find the documentation you can either search it up online (e.g doc for set_data) or from a python prompt you can use the help function, which will show you the docstring of the object (can be used on a module/function/class etc) if it has any.
For example if you want to know what the datetime.datetime.now does (I dont have mathplotlib install to use it on it):
>>> import datetime
>>> help(datetime.datetime.now)
Help on built-in function now:
now(tz=None) method of builtins.type instance
Returns new datetime object representing current time local to tz.
tz
Timezone object.
If no tz is specified, uses local timezone.
I'm going through a data visualisation in python course to help with my lab reports and I cant seem to understand the purpose of the second line in this example of creating a scatter plot:
import matplotlib.pyplot as plt
fig,ax = plt.subplots()
ax.scatter([1,2,3,4,5],[1,2,3,4,5])
ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.show()
Can someone explain what that first line is doing here.
I have only started using python and I haven't seen this sort of syntax before(fig,ax = plt.subplots()). I tried to test if it was a way to assign 2 variables to the same thing by writing x,y=1, I ended up getting an error "int object is not iterable".
Another thing I dont understand is where is fig being used anywhere in the body of code? My current understanding is that the top line defined what fig and ax are, I can see that ax is used in the body of the code to define the scatter plot, but where is fig used? I tried to delete it and run the code, but I got this error:
'tuple' object has no attribute 'scatter'
If someone could please explain the above misconceptions.
As per the official docs, subplots creates a figure and a set of subplots. Specifically,
Returns:
fig : Figure
ax : axes.Axes object or array of Axes objects. ax can be either a single Axes object or an array of Axes objects if more than one subplot was created. The dimensions of the resulting array can be controlled with the squeeze keyword, see above.
Now when you do
fig, ax = plt.subplots()
the figure object is assigned to the variable fig and the axis object is assigned to the variable ax.
The fig will then give you access to the attributes on a figure-level, for instance, the figure title. The ax will give you access to the attributes on individual subplot level, such as the legends, axis-labels, ticks, of each individual subplot. It will be as array of Axes objects in case you have more than one subplot.
I tried to test if it was a way to assign 2 variables to the same thing by writing x,y=1, I ended up getting an error "int object is not iterable".
You are almost right. That is syntax to assign multiple variables at the same time, but what you are missing is that plt.subplots() returns a tuple - of two values paired together.
If you want to better understand it you can run:
a, b = (1, 4)
or
a,b = 1, 4
(it's the same as far as python is concerns, it packs/unpacks values to a tuple if multiple values are used or returned)
I tried to delete it and run the code, but I got this error:
'tuple' object has no attribute 'scatter'
This is also related to why you got this error. The figure is indeed not in use in your code snippet, but you need it for python to understand you want to use part of the tuple and not the tuple itself.
For example: a=(1,2) will result in a holding a tuple, but in a, b = 1, 2 each of the created variables will hold an integer.
In your case, the axis object has a method scatter, which the tuple object does not have, hence your error.
The documentation for set_array is very skimpy. What does it do? What range of values can it take? How does it work in conjunction with other color-related routines and data structures?
On the collections docpage it is said to "Set the image array from numpy array A." It is described in the same way in the colormap API. That's all.
I find no mention of set_array() (much less examples) in any of several popular books on matplotlib programming, such as Devert (2014), McGreggor (2015), Root (2015) and Tossi (2009).
Yet, if set_array() is some arcane function that is only needed in rare cases, why does it show up so often both in matplotlib examples and in examples posted on the SciKit Learn website? Seems like a pretty mainstream function, and so it ought to have more mainstream documentation.
For example:
Matplotlib docs: Use of set_array() in creation of a multi-colored line
Matplotlib docs: Line collection with masked arrays
Scikit Learn docs: Visualization of stockmarket structure
Sifting through Stack Overflow posts that mention set_array() I found this one, where a poster states that "set_array() handles mapping an array of data values to RGB", and this one where posters indicate that set_array() must be called in some cases when one is setting up a ScalarMappable object.
I've tried experimenting with the examples I've found on-line, changing the range of values passed in to set_array(), for example, to try to figure out what it is doing. But, at this point, I'm spending way too much time on this one dumb function. Those deeply into color maps have enough context to guess what it does, but I can't afford to take a detour that big, just to understand this one function.
Could someone please offer a quick description and maybe some links?
The set_array method doesn't do much per se. It only defines the content of an array that is internal to the object in which it is defined. See for instance in the source of matplotlib.cm
def set_array(self, A):
"""
Set the image array from numpy array *A*.
Parameters
----------
A : ndarray
"""
self._A = A
self._update_dict['array'] = True
In the multicolored_line example of the matplotlib documentation, this is used to map colors of a cmap.
Let's take a similar example and create a collection of lines and map the segments to indexed colors in a colormap:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.colors import ListedColormap, BoundaryNorm
f, axes = plt.subplots(ncols=3)
y = np.arange(0,1,0.1).repeat(2)
x = np.append(y[1:], [1])
segments = np.array(list(zip(x,y))).reshape(-1, 2, 2)
cmap = ListedColormap(['r', 'g', 'b'])
norm = BoundaryNorm([-0.5, 0.5, 1.5, 2.5], cmap.N)
for ax in axes:
ax.add_collection(LineCollection(segments, cmap=ListedColormap(['r', 'g', 'b']), norm=norm))
axes[1].collections[0].set_array(np.array([0,1]))
axes[2].collections[0].set_array(np.array([0,1,2]))
axes[1].set_title('set_array to [0,1]')
axes[2].set_title('set_array to [0,1,2]')
This gives the following output:
What is does is to map the segment to the indexed colors defined in the cmap (here 0->'r', 1->'g', 2->'b'). This behaviour is specified in the matpotlib.collections source:
Each Collection can optionally be used as its own `.ScalarMappable` by
passing the *norm* and *cmap* parameters to its constructor. If the
Collection's `.ScalarMappable` matrix ``_A`` has been set (via a call
to `.Collection.set_array`), then at draw time this internal scalar
mappable will be used to set the ``facecolors`` and ``edgecolors``,
ignoring those that were manually passed in.