Looping through a function to plot several subplots, Python - python

I have variables x and y
def function(a,b):
x = x[(x>a)*(x<b)]
y = y[(y<a)*(y>b)]
# perform some fitting routine using curve_fit on x and y
fig = plt.figure()
ax = fig.add_subplot(111)
phist,xedge,yedge,img = ax.hist2d(x,y,bins=20,norm=LogNorm())
im = ax.imshow(phist,cmap=plt.cm.jet,norm=LogNorm(),aspect='auto')
fig.colorbar(im,ax=ax)
fig.show()
All works fine. But I have 6 pairs of different input parameters a and b. I would like to somehow call function(a,b) using a loop and plot the six different x and y (corresponding to the 6 input pairs) as 6 subplots.
like we do
ax1 = fig.add_subplot(231) # x vs y for a1,b1
ax2 = fig.add_subplot(232) # x vs y for a2,b2
....
ax6 = fig.add_subplot(236) # x vs y for a6,b6
I would like to get an idea of how to proceed to get the final subplot!
I know that it can be done manually by specifying different variables, like x1 and y1 for the first input pair a and b and so on for the other 6 pairs (x2,y2...,x6,y6). But it will be a very lengthy and confusing code.

The key is using the three parameter form of subplot:
import matplotlib.pyplot as plt
# Build a list of pairs for a, b
ab = zip(range(6), range(6))
#iterate through them
for i, (a, b) in enumerate(ab):
plt.subplot(2, 3, i+1)
#function(a, b)
plt.plot(a, b)
plt.show()
You'll just have to take the call to figure out of the function.

Use plt.subplots instead of plt.subplot (note the "s" at the end). fig, axs = plt.subplots(2, 3) will create a figure with 2x3 group of subplots, where fig is the figure, and axs is a 2x3 numpy array where each element is the axis object corresponding to the axis in the same position in the figure (so axs[1, 2] is the bottom-right axis).
You can then either use a pair of loops to loop over each row then each axis in that row:
fig, axs = plt.subplots(2, 3)
for i, row in enumerate(axs):
for j, ax in enumerate(row):
ax.imshow(foo[i, j])
fig.show()
Or you can use ravel to flatten the rows and whatever you want to get the data from:
fig, axs = plt.subplots(2, 3)
foor = foo.ravel()
for i, ax in enumerate(axs.ravel()):
ax.imshow(foor[i])
fig.show()
Note that ravel is a view, not a copy, so this won't take any additional memory.

Related

Matplotlib subplot object being classified/recoqnized as numpy array. Hence not able to use twinx() function and etc

So I'm trying to plot two plots on the same graph, one Y axis on either sides sharing the same X axis. I've done this earlier and hence knew how to do it (or I though so). Anyways now I was trying to implement it under a function since I need to make a lot of plots and hence wanted a more modular solution.
Now when trying to run the same thing under a fucntion it throws error 'numpy.ndarray' object has no attribute 'twinx'. This is because for some reason ax1 is being shown of class numpy.ndarray which should actually be matplotlib.axes._subplots.AxesSubplot.
Please Help.
def pumped_up_plotting(data, colname1, colname2):
fig, ax1 = plt.subplots(13, 4, figsize=(5*4, 5*13))
print("Look here lil bitch: ", type(ax1))
ax2 = ax1.twinx()
plt.subplots_adjust(hspace=0.3)
for i in range(0, 13):
for j in range(0,4):
id_ = profiles[i*4+j]
samp = data[data["profile_id"] == id_]
ax1[i, j].plot(samp["time"], samp[colname1], label=colname1, color="blue")
ax2[i, j].plot(samp["time"], samp[colname2], label=colname2, color="yellow")
ax1[i, j].set_xlabel("Time")
lines_1, labels_1 = ax1.get_legend_handles_labels()
lines_2, labels_2 = ax2.get_legend_handles_labels()
lines = lines_1 + lines_2
labels = labels_1 + labels_2
ax1[i, j].legend(lines, labels, loc=0)
pumped_up_plotting(df, "rotor", "motor_work")
Screenshot of error message
ax1 is a numpy array because you are creating a grid of 13 by 4 axes instead of a single axes. You do this by supplying 13 and 4 as the first two arguments to plt.subplots. I'm not sure what you intended those numbers to do, but if you delete them, it should work.
As of now, ax1 is a numpy array with 13 columns an 4 rows containing the individual axis objects.
I'll try to explain what went wrong. To simplify, I'm gonna use a 2 x 2 grid instead of 13 x 4. So fig, ax1 = plt.subplots(2, 2).
Then, ax1 will look like this:
array([[<AxesSubplot:>, <AxesSubplot:>],
[<AxesSubplot:>, <AxesSubplot:>]], dtype=object)
If you try to call ax1.twinx(), it won't work because ax1 is actually not an axis, but the array containing all your 4 axes of the grid.
So what you would have to call if you wanted to create a twin axis of the first axis, would be ax1[0,0].twinx(). Since you want to do it for every axis and not just the first one, you can do it inside a nested loop where you loop over the rows and columns of the numpy array. Since you are already doing this, you can justput that line inside your already existing loop.
This looks like that line.
ax2 = ax1[i, j].twinx()
Here, we are taking the individual axis object from the numpy array by indexing (as you were already doing before) and calling twinx on it. This returns a twin axis which we are saving as ax2. Note that this is kind of confusing, since ax2 is a single axis object while ax1 is an array containg axis objects. I personally would rename ax1 to axs so it's clear this variable contains multiple axes.
Because ax2 is already a single axis object, we can call the plotting functions directly on it, and don't have to index it.
def pumped_up_plotting(data, colname1, colname2):
fig, ax1 = plt.subplots(13, 4, figsize=(5*4, 5*13))
print("Type of ax1 ", type(ax1))
plt.subplots_adjust(hspace=0.3)
for i in range(0, 13):
for j in range(0,4):
ax2 = ax1[i, j].twinx()
id_ = profiles[i*4+j]
samp = data[data["profile_id"] == id_]
ax1[i, j].plot(samp["time"], samp[colname1], label=colname1, color="blue")
ax2.plot(samp["time"], samp[colname2], label=colname2, color="yellow")
ax1[i, j].set_xlabel("Time")
lines_1, labels_1 = ax1.get_legend_handles_labels()
lines_2, labels_2 = ax2.get_legend_handles_labels()
lines = lines_1 + lines_2
labels = labels_1 + labels_2
ax1[i, j].legend(lines, labels, loc=0)
pumped_up_plotting(df, "rotor", "motor_work")
My way of doing this more clearly would be:
def pumped_up_plotting(data, colname1, colname2):
fig, axs = plt.subplots(13, 4, figsize=(5*4, 5*13))
plt.subplots_adjust(hspace=0.3)
for i, row in enumerate(axs):
for j, ax in enumerate(row):
ax2 = ax.twinx()
id_ = profiles[i*4+j]
samp = data[data["profile_id"] == id_]
ax.plot(samp["time"], samp[colname1], label=colname1, color="blue")
ax2.plot(samp["time"], samp[colname2], label=colname2, color="yellow")
ax.set_xlabel("Time")
lines_1, labels_1 = ax.get_legend_handles_labels()
lines_2, labels_2 = ax2.get_legend_handles_labels()
lines = lines_1 + lines_2
labels = labels_1 + labels_2
ax.legend(lines, labels, loc=0)
pumped_up_plotting(df, "rotor", "motor_work")

How to create grid plot with inner subplots?

I have configured subplots of (5 x 1) format shown in Fig. 1 as given by Figure block A in the MWE. I am trying to repeat them n times such that they appear in a grid format with number of rows and columns given by the function fitPlots as mentioned here; to give output as shown in Fig. 2.
Fig. 1 Initial plot
Fig. 2 Repeated plot (desired output)
What would be the best way to repeat the code block to create a grid plot with inner subplots? The MWE creates multiple pages, I want all of them on a single page.
MWE
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
import numpy as np
import math
x = np.arange(1, 100, 0.2)
y_a = np.sqrt(x)
y_b = np.sin(x)
y_c = np.sin(x)
y_d = np.cos(x) * np.cos(x)
y_e = 1/x
########## Figure block A #####################
with PdfPages('./plot_grid.pdf') as plot_grid_loop:
fig, (a, b, c, d, e) = plt.subplots(5, 1, sharex=True, gridspec_kw={'height_ratios': [5, 1, 1, 1, 1]})
a.plot(x, y_a)
b.plot(x, y_b)
c.plot(x, y_c)
d.plot(x, y_d)
e.plot(x, y_e)
plot_grid_loop.savefig()
plt.close
########## Figure block A #####################
# from https://stackoverflow.com/a/43366784/4576447
def fitPlots(N, aspect=(16,9)):
width = aspect[0]
height = aspect[1]
area = width*height*1.0
factor = (N/area)**(1/2.0)
cols = math.floor(width*factor)
rows = math.floor(height*factor)
rowFirst = width < height
while rows*cols < N:
if rowFirst:
rows += 1
else:
cols += 1
rowFirst = not(rowFirst)
return rows, cols
n_plots = 15
n_rows, n_cols = fitPlots(n_plots)
with PdfPages('./plot_grid.pdf') as plot_grid_loop:
for m in range(1, n_plots+1):
fig, (a, b, c, d, e) = plt.subplots(5, 1, sharex=True, gridspec_kw={'height_ratios': [5, 1, 1, 1, 1]})
a.plot(x, y_a)
b.plot(x, y_b)
c.plot(x, y_c)
d.plot(x, y_d)
e.plot(x, y_e)
plot_grid_loop.savefig()
plt.close
This can be done by generating a GridSpec object with gs_fig = fig.add_gridspec() that contains enough rows and columns to fit the five figure blocks (note that when you use plt.subplots a GridSpec is also generated and can be accessed with ax.get_gridspec()). Each empty slot in the GridSpec can then be filled with a sub-GridSpec with gs_sub = gs_fig[i].subgridspec() to hold the five subplots. The trickier part is sharing the x-axis. This can be done by generating an empty first Axes with which the x-axis of all the subplots can be shared.
The following example illustrates this with only three figure blocks, based on the code sample you have shared but with some differences regarding the figure design: the number of rows is computed based on the chosen number of columns, and the figure dimensions are set based on a chosen figure width and aspect ratio. The code for saving the figure to a pdf file is not included.
import numpy as np # v 1.19.2
import matplotlib.pyplot as plt # v 3.3.4
# Create variables to plot
x = np.arange(1, 100, 0.2)
y_a = np.sqrt(x)
y_b = np.sin(x)
y_c = np.sin(x)
y_d = np.cos(x)*np.cos(x)
y_e = 1/x
# Set parameters for figure dimensions
nplots = 3 # random number of plots for this example
ncols = 2
nrows = int(np.ceil(nplots/ncols))
subp_w = 10/ncols # 10 is the total figure width in inches
subp_h = 1*subp_w # set subplot aspect ratio
# Create figure containing GridSpec object with appropriate dimensions
fig = plt.figure(figsize=(ncols*subp_w, nrows*subp_h))
gs_fig = fig.add_gridspec(nrows, ncols)
# Loop through GridSpec to add sub-GridSpec for each figure block
heights = [5, 1, 1, 1, 1]
for i in range(nplots):
gs_sub = gs_fig[i].subgridspec(len(heights), 1, height_ratios=heights, hspace=0.2)
ax = fig.add_subplot(gs_sub[0, 0]) # generate first empty Axes to enable sharex
ax.axis('off') # remove x and y axes because it is overwritten in the loop below
# Loop through y variables to plot all the subplots with shared x-axis
for j, y in enumerate([y_a, y_b, y_c, y_d, y_e]):
ax = fig.add_subplot(gs_sub[j, 0], sharex=ax)
ax.plot(x, y)
if not ax.is_last_row():
ax.tick_params(labelbottom=False)
Reference: matplotlib tutorial GridSpec using SubplotSpec

Subplot legend showing wrong colors [duplicate]

Background:
I have a list_of_x_and_y_list that contains x and y values which looks like:
[[(44800, 14888), (132000, 12500), (40554, 12900)], [(None, 193788), (101653, 78880), (3866, 160000)]]
I have another data_name_list ["data_a","data_b"] so that
"data_a" = [(44800, 14888), (132000, 12500), (40554, 12900)]
"data_b" = [(None, 193788), (101653, 78880), (3866, 160000)]
The len of list_of_x_and_y_list / or len of data_name_list is > 20.
Question:
How can I create a scatter plot for each item (being the same colour) in the data_name_list?
What I have tried:
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax = plt.axes(facecolor='#FFFFFF')
prop_cycle = plt.rcParams['axes.prop_cycle']
colors = prop_cycle.by_key()['color']
print(list_of_x_and_y_list)
for x_and_y_list, data_name, color in zip(list_of_x_and_y_list, data_name_list, colors):
for x_and_y in x_and_y_list,:
print(x_and_y)
x, y = x_and_y
ax.scatter(x, y, label=data_name, color=color) # "label=data_name" creates
# a huge list as a legend!
# :(
plt.title('Matplot scatter plot')
plt.legend(loc=2)
file_name = "3kstc.png"
fig.savefig(file_name, dpi=fig.dpi)
print("Generated: {}".format(file_name))
The Problem:
The legend appears to be a very long list, which I don't know how to rectify:
Relevant Research:
Matplotlib scatterplot
Scatter Plot
Scatter plot in Python using matplotlib
The reason you get a long repeated list as a legend is because you are providing each point as a separate series, as matplotlib does not automatically group your data based on the labels.
A quick fix is to iterate over the list and zip together the x-values and the y-values of each series as two tuples, so that the x tuple contains all the x-values and the y tuple the y-values.
Then you can feed these tuples to the plt.plot method together with the labels.
I felt that the names list_of_x_and_y_list were uneccessary long and complicated, so in my code I've used shorter names.
import matplotlib.pyplot as plt
data_series = [[(44800, 14888), (132000, 12500), (40554, 12900)],
[(None, 193788), (101653, 78880), (3866, 160000)]]
data_names = ["data_a","data_b"]
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax = plt.axes(facecolor='#FFFFFF')
prop_cycle = plt.rcParams['axes.prop_cycle']
colors = prop_cycle.by_key()['color']
for data, data_name, color in zip(data_series, data_names, colors):
x,y = zip(*data)
ax.scatter(x, y, label=data_name, color=color)
plt.title('Matplot scatter plot')
plt.legend(loc=1)
To only get one entry per data_name, you should add data_name only once as a label. The rest of the calls should go with label=None.
The simplest you can achieve this using the current code, is to set data_name to None at the end of the loop:
from matplotlib import pyplot as plt
from random import randint
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_facecolor('#FFFFFF')
# create some random data, suppose the sublists have different lengths
list_of_x_and_y_list = [[(randint(1000, 4000), randint(2000, 5000)) for col in range(randint(2, 10))]
for row in range(10)]
data_name_list = list('abcdefghij')
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
for x_and_y_list, data_name, color in zip(list_of_x_and_y_list, data_name_list, colors):
for x_and_y in x_and_y_list :
x, y = x_and_y
ax.scatter(x, y, label=data_name, color=color)
data_name = None
plt.legend(loc=2)
plt.show()
Some things can be simplified, making the code 'more pythonic', for example:
for x_and_y in x_and_y_list :
x, y = x_and_y
can be written as:
for x, y in x_and_y_list:
Another issue, is that with a lot of data calling scatter for every point could be rather slow. All the x and y belonging to the same list can be plotted together. For example using list comprehension:
for x_and_y_list, data_name, color in zip(list_of_x_and_y_list, data_name_list, colors):
xs = [x for x, y in x_and_y_list]
ys = [y for x, y in x_and_y_list]
ax.scatter(xs, ys, label=data_name, color=color)
scatter could even get a list of colors per point, but plotting all the points in one go, wouldn't allow for labels per data_name.
Very often, numpy is used to store numerical data. This has some advantages, such as vectorization for quick calculations. With numpy the code would look like:
import numpy as np
for x_and_y_list, data_name, color in zip(list_of_x_and_y_list, data_name_list, colors):
xys = np.array(x_and_y_list)
ax.scatter(xys[:,0], xys[:,1], label=data_name, color=color)

How to force equal subplot size [duplicate]

This question already has an answer here:
Set subplot(or gridspec) with same size
(1 answer)
Closed 3 years ago.
I am trying to plot 3 graphs in a single row, but all plots should be the same size (at least the same height).
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.gridspec as gridspec
x = np.random.uniform(size=2000) - 0.5
y = np.random.uniform(size=2000) - 0.5
z = np.random.uniform(size=2000) - 0.5
DPI = 106
fig = plt.figure(figsize=(900 / DPI, 350 / DPI))
gs = gridspec.GridSpec(1, 3, width_ratios=[1,1,1])
# 1D
r = np.abs(x)
# plot
ax1 = fig.add_subplot(gs[0])
plot = ax1.scatter(x, r, s = 1, c=r, cmap='jet', marker='.', alpha = 1, vmax = 0.5)
ax1.set_xlabel('$x$')
ax1.set_ylabel('$y$')
ax1.set_aspect('equal')
# 2D
r = np.sqrt(x * x + y * y)
# plot
ax2 = fig.add_subplot(gs[1])
plot = ax2.scatter(x, y, s = 1, c=r, cmap='jet', marker='.', alpha = 1, vmax = 0.5)
ax2.set_xlabel('$x$')
ax2.set_ylabel('$y$')
ax2.set_aspect('equal')
fig.colorbar(plot, shrink = 1, ax = ax2)
# 3D
r = np.sqrt(x * x + y * y + z * z)
ax3 = fig.add_subplot(gs[2], projection='3d')
plot = ax3.scatter(x, y, z, s = 10, c=r, cmap='jet', marker='.', alpha = 1, vmax = 0.5)
ax3.set_xlabel('$x$')
ax3.set_ylabel('$y$')
ax3.set_zlabel('$z$')
ax3.view_init(30, 240)
ax3.set_aspect('equal', 'box')
fig.colorbar(plot, shrink = 1,ax = ax3)
fig.tight_layout()
The code above does produce three columns of plots, first 1D, than 2D and lastly a 3D plot. However, as you can see from attached image, the plots are not of the same size eventhough I tried using gridspecas suggested here.
Any ideas on how to change the size of subplots?
Matplotlib.pyplot's auto-layout algorithm does not care if you are plotting a 3D object, 2D object or 1D (points). Of course the syntax for defining the object will change and a 3D object will take 3 parameters. But how the objects are placed in a row is not changed. I see a few possible reasons why your specific data may be causing you trouble.
The first image is 2D and has a smaller y-axis scale than the other 2 images. Also the 1st image x-axis scale is twice as wide as y-axis scale is tall. The 2nd and 3rd images include vertical colormaps, which makes these images taller in total.
1) You can change the 1st plot's y-axis to be taller than it currently is.
ax1.set_aspect('equal')
This code in your 1st plot is preventing you from changing the y-axis scale only. You can remove this line and manually set the scale of y-axis scale to be larger.
2) Make your entire row taller, so the vertical colormaps in 2nd and 3rd plots will not determine the overall height of the figure space. Set the figsize's x and y attribute to (12, 12) and see if that fixes the issue. The 2nd number in figsize sets height.
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(12, 12))
3) Alternatively, you can plot 1st graph in it's own 1st row, and 2nd and 3rd graphs in a separate 2nd row. Set nrows to 2 and ncols to 2, then add 1st plot to row 1 and col 1, and 2nd plot to row 2, col 1, and 3rd plot to row 2, col 2.
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(12, 18))
You can refer to Matplotlib documentation for getting details setting layout parameters. Hope one of these will work. :-)
https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.axes.Axes.set_aspect.html

Arrange plots that have subplots called from functions on grid in matplotlib

I am looking for something similar to arrangeGrob in R:
I have a function (say, function FUN1) that creates a plot with subplots. The number of subplots FUN1 creates may vary and the plot itself is quite complex. I have two other functions FUN2 and FUN3 which also create plots of varying structure.
Is there a simple way to define/arrange an overall GRID, for example a simple 3 rows 1 column style and simply pass
FUN1 --> GRID(row 1, col 1)
FUN2 --> GRID(row 2, col 1)
FUN3 --> GRID(row 3, col 1)
afterwards such that the complicated plot generated by FUN1 gets plotted in in row 1, the plot generated by FUN2 in row 2 and so on, without specifying the subplot criteria in the FUNs before?
The usual way to create plots with matplotlib would be to create some axes first and then plot to those axes. The axes can be set up on a grid using plt.subplots, figure.add_subplot, plt.subplot2grid or more sophisticated, using GridSpec.
Once those axes are created, they can be given to functions, which plot content to the axes. The following would be an example where 6 axes are created and 3 different functions are used to plot to them.
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
def func1(ax, bx, cx):
x = np.arange(3)
x2 = np.linspace(-3,3)
y1 = [1,2,4]
y2 = [3,2.5,3.4]
f = lambda x: np.exp(-x**2)
ax.bar(x-0.5, y1, width=0.4)
ax.bar(x, y2, width=0.4)
bx.plot(x,y1, label="lab1")
bx.scatter(x,y2, label="lab2")
bx.legend()
cx.fill_between(x2, f(x2))
def func2(ax, bx):
x = np.arange(1,18)/1.9
y = np.arange(1,6)/1.4
z = np.outer(np.sin(x), -np.sqrt(y)).T
ax.imshow(z, aspect="auto", cmap="Purples_r")
X, Y = np.meshgrid(np.linspace(-3,3),np.linspace(-3,3))
U = -1-X**2+Y
V = 1+X-Y**2
bx.streamplot(X, Y, U, V, color=U, linewidth=2, cmap="autumn")
def func3(ax):
data = [sorted(np.random.normal(0, s, 100)) for s in range(2,5)]
ax.violinplot(data)
gs = gridspec.GridSpec(3, 4,
width_ratios=[1,1.5,0.75,1], height_ratios=[3,2,2] )
ax1 = plt.subplot(gs[0:2,0])
ax2 = plt.subplot(gs[2,0:2])
ax3 = plt.subplot(gs[0,1:3])
ax4 = plt.subplot(gs[1,1])
ax5 = plt.subplot(gs[0,3])
ax6 = plt.subplot(gs[1:,2:])
func1(ax1, ax3, ax5)
func3(ax2)
func2(ax4, ax6)
plt.tight_layout()
plt.show()

Categories