Suppose I have a 3x3 array of 9 subplots of identical sizes. Is it possible to make a 4x4 figure and replace the top and left-most subplots with large titles?
I know it's theoretically possible to use some sort of text box, but those don't scale very well and require a lot of tweaking. Suggestions would be much appreciated.
EDIT: I was thinking of something similar to this except with proper graphs inside the array:
Sounds to me a job for GridSpec or subplot2grid.
Besides the above link, you can find some example and code here
You could use large, rotated x and y labels to achieve something similar:
Pandas
from pandas.tools.plotting import scatter_matrix
from pandas import DataFrame
from numpy.random import randn
import matplotlib.pyplot as plt
df = DataFrame(randn(1000, 8), columns=['Label1', 'Label2', 'Label3', 'Label4', 'Label5', 'Label6', 'Label7', 'Label8'])
fig = scatter_matrix(df, alpha=0.2, figsize=(6, 6), diagonal='kde')
for axes in fig:
for ax in axes:
ax.set_ylabel(ax.get_ylabel(), rotation='horizontal', ha='right', fontsize=16)
ax.set_xlabel(ax.get_xlabel(), rotation='vertical', fontsize=16)
ax.set_yticklabels('')
ax.set_xticklabels('')
plt.gcf().set_facecolor('w')
Seaborn
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="white")
df = DataFrame(randn(50, 6), columns=['Label1', 'Label2', 'Label3', 'Label4', 'Label5', 'Label6'])
g = sns.PairGrid(df, diag_sharey=False, size=1.4)
g.map_lower(sns.kdeplot, cmap="Blues_d")
g.map_upper(plt.scatter)
g.map_diag(sns.kdeplot, lw=2)
for axes in g.axes:
for ax in axes:
ax.set_ylabel(ax.get_ylabel(), rotation='horizontal', ha='right', fontsize=20)
ax.set_xlabel(ax.get_xlabel(), rotation='vertical', fontsize=20)
ax.set_yticklabels('')
ax.set_xticklabels('')
ax.set_frame_on(False)
plt.gcf().set_facecolor('w')
Both examples are from their respective tutorials and then tweaked a little.
Related
I would like to draw the following bar plot with annotation and I want to keep the x-label 45 degree so that it is easily readable. I am not sure why my code is not working. I have added the sample data and desired bar plots as a attachment. I appreciate your suggestions! Thanks!
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
#sns.set(rc={"figure.dpi":300, 'savefig.dpi':300})
sns.set_context('notebook')
sns.set_style("ticks")
#sns.set_style('white')
sns.set_context("paper", font_scale = 2)
colors = ['b', 'g', 'r', 'c', 'm']
#sns.set(style="whitegrid")
#sns.set_palette(sns.color_palette(colors))
#fig, (ax1,ax2) = plt.subplots(1, 2, figsize=(16, 8))
#fig.subplots_adjust(wspace=0.3)
plots1 = sns.barplot(x="Model", y="G-mean", data=df_Aussel2014_5features, ax=ax1,palette='Spectral')
# Iterrating over the bars one-by-one
for bar in plots1.patches:
# Using Matplotlib's annotate function and
# passing the coordinates where the annotation shall be done
plots1.annotate(format(bar.get_height(), '.2f'),
(bar.get_x() + bar.get_width() / 2,
bar.get_height()), ha='center', va='center',
size=10, xytext=(0, 5),
textcoords='offset points')
plt.show()
# Save figure
#plt.savefig('Aussel2014_5features.png', dpi=300, transparent=False, bbox_inches='tight')
I got the following image.
You are using the object oriented interface (e.g. axes) so don't mix plt. and axes. methods
seaborn.barplot is an axes-level plot, which returns a matplotlib axes, p1 in this case.
Use the matplotlib.axes.Axes.tick_params to set the rotation of the axis, or a number of other parameters, as shown in the documentation.
Use matplotlib.pyplot.bar_label to add bar annotations.
See this answer with additional details and examples for using the method.
Adjust the nrow, ncols and figsize as needed, and set sharex=False and sharey=False.
Tested in python 3.8.12, pandas 1.3.4, matplotlib 3.4.3, seaborn 0.11.2
import seaborn as sns
import matplotlib.pyplot as plot
import pandas as pd
# data
data = {'Model': ['QDA', 'LDA', 'DT', 'Bagging', 'NB'],
'G-mean': [0.703780, 0.527855, 0.330928, 0.294414, 0.278713]}
df = pd.DataFrame(data)
# create figure and axes
fig, ax1 = plt.subplots(nrows=1, ncols=1, figsize=(8, 8), sharex=False, sharey=False)
# plot
p1 = sns.barplot(x="Model", y="G-mean", data=df, palette='Spectral', ax=ax1)
p1.set(title='Performance Comparison based on G-mean')
# add annotation
p1.bar_label(p1.containers[0], fmt='%0.2f')
# add a space on y for the annotations
p1.margins(y=0.1)
# rotate the axis ticklabels
p1.tick_params(axis='x', rotation=45)
import matplotlib.pyplot as plt. plt.xticks(rotation=45)
Example :
import matplotlib.pyplot as plt
plt.xticks(rotation=45)
How do I increase the figure size for this figure?
This does nothing:
fig.figsize(15, 15)
Use this on a figure object:
fig.set_figheight(15)
fig.set_figwidth(15)
Alternatively, when using .subplots() to create a new figure, specify figsize=:
fig, axs = plt.subplots(2, 2, figsize=(15, 15))
In addition to the previous answers, here is an option to set the size of the figure and the size of the subplots within the figure individually by means of gridspec_kw:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
#generate random data
x,y=range(100), range(10)
z=np.random.random((len(x),len(y)))
Y=[z[i].sum() for i in range(len(x))]
z=pd.DataFrame(z).unstack().reset_index()
#Plot data
fig, axs = plt.subplots(2,1,figsize=(16,9), gridspec_kw={'height_ratios': [1, 2]})
axs[0].plot(Y)
axs[1].scatter(z['level_1'], z['level_0'],c=z[0])
with this figure as result:
Alternatively, create a figure() object using the figsize argument and then use add_subplot to add your subplots. E.g.
import matplotlib.pyplot as plt
import numpy as np
f = plt.figure(figsize=(10,3))
ax = f.add_subplot(121)
ax2 = f.add_subplot(122)
x = np.linspace(0,4,1000)
ax.plot(x, np.sin(x))
ax2.plot(x, np.cos(x), 'r:')
Benefits of this method are that the syntax is closer to calls of subplot() instead of subplots(). E.g. subplots doesn't seem to support using a GridSpec for controlling the spacing of the subplots, but both subplot() and add_subplot() do.
You can use plt.figure(figsize = (16,8)) to change figure size of a single plot and with up to two subplots. (arguments inside figsize lets to modify the figure size)
To change figure size of more subplots you can use plt.subplots(2,2,figsize=(10,10)) when creating subplots.
from matplotlib import pyplot as plt
lis=[img,gaussian_img,gaussian_img_8bit]
f,axs=plt.subplots(3,1,figsize=(25,25)) #ROW,COLUMN
axs[0].imshow(lis[0])
axs[1].imshow(lis[1])
axs[2].imshow(lis[2])
For plotting subplots in a for loop which is useful sometimes:
Sample code to for a matplotlib plot of multiple subplots of histograms from a multivariate numpy array (2 dimensional).
plt.figure(figsize=(16, 8))
for i in range(1, 7):
plt.subplot(2, 3, i)
plt.title('Histogram of {}'.format(str(i)))
plt.hist(x[:,i-1], bins=60)
I am trying to plot a figure containing two subplots, a seaborn heatmap and simple matplotlib lines. However, when sharing the x-axis for both plots, they do not align as can be seen in this figure:
It would seem that the problem is similar to this post, but when displaying ax[0].get_xticks() and ax[1].get_xticks() I get the same positions, so I don't know what to change. And in my picture the the deviation seems to be more than a 0.5 shift.
What am I doing wrong?
The code I used to plot the figure is the following:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
M_1=np.random.random((15,15))
M_2=np.random.random((15,15))
L_1=np.random.random(15)
L_2=np.random.random(15)
x=range(15)
cmap = sns.color_palette("hot", 100)
sns.set(style="white")
fig, ax = plt.subplots(2, 1, sharex='col', figsize=(10, 12))
ax[0].plot(x,L_1,'-', marker='o',color='tab:orange')
sns.heatmap(M_1, cmap=cmap, vmax=np.max(M_1), center=np.max(M_1)/2., square=False, ax=ax[1])
#Mr-T 's comment is spot on. The easiest would be to create the axes beforehand instead of letting heatmap() shrink your axes in order to make room for the colorbar.
There is the added complication that the labels for the heatmap are not actually placed at [0,1,...] but are in the middle of each cell at [0.5, 1.5, ...]. So if you want your upper plot to align with the labels at the bottom (and with the center of each cell), you may have to shift your plot by 0.5 units to the right:
M_1=np.random.random((15,15))
M_2=np.random.random((15,15))
L_1=np.random.random(15)
L_2=np.random.random(15)
x=np.arange(15)
cmap = sns.color_palette("hot", 100)
sns.set(style="white")
fig, ax = plt.subplots(2, 2, sharex='col', gridspec_kw={'width_ratios':[100,5]})
ax[0,1].remove() # remove unused upper right axes
ax[0,0].plot(x+0.5,L_1,'-', marker='o',color='tab:orange')
sns.heatmap(M_1, cmap=cmap, vmax=np.max(M_1), center=np.max(M_1)/2., square=False, ax=ax[1,0], cbar_ax=ax[1,1])
I have created the following plot using seaborn kdeplot and customizing the gridlines.
sns.set_style('whitegrid')
cdf_accuracy = sns.kdeplot(eval_df['accuracy'], cumulative=True)
cdf_accuracy.yaxis.set_major_locator(ticker.MultipleLocator(0.25))
cdf_accuracy.xaxis.set_major_locator(ticker.MultipleLocator(10))
However, I would like to show the gridlines on the x-axis just on the points were the y-axis gridlines intersect the plot. There is a way to do this?
Thanks for your answers
As long as your characteristic is monotonic, which should be given with a cumulative dataset, you could simply use interpolation on the y-values:
import numpy as np
y_intrsct = [.25, .5, .75]
x_intrsct = np.interp(y_intrsct, y_data, x_data)
which results in
array([67.69792378, 83.24194722, 92.24041857])
plotted with the following code:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(x_data, y_data)
ax.set_yticks(np.linspace(0, 1, 5))
ax.grid(axis='y')
ax.vlines(x_intrsct, *ax.get_ylim())
How do I increase the figure size for this figure?
This does nothing:
fig.figsize(15, 15)
Use this on a figure object:
fig.set_figheight(15)
fig.set_figwidth(15)
Alternatively, when using .subplots() to create a new figure, specify figsize=:
fig, axs = plt.subplots(2, 2, figsize=(15, 15))
In addition to the previous answers, here is an option to set the size of the figure and the size of the subplots within the figure individually by means of gridspec_kw:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
#generate random data
x,y=range(100), range(10)
z=np.random.random((len(x),len(y)))
Y=[z[i].sum() for i in range(len(x))]
z=pd.DataFrame(z).unstack().reset_index()
#Plot data
fig, axs = plt.subplots(2,1,figsize=(16,9), gridspec_kw={'height_ratios': [1, 2]})
axs[0].plot(Y)
axs[1].scatter(z['level_1'], z['level_0'],c=z[0])
with this figure as result:
Alternatively, create a figure() object using the figsize argument and then use add_subplot to add your subplots. E.g.
import matplotlib.pyplot as plt
import numpy as np
f = plt.figure(figsize=(10,3))
ax = f.add_subplot(121)
ax2 = f.add_subplot(122)
x = np.linspace(0,4,1000)
ax.plot(x, np.sin(x))
ax2.plot(x, np.cos(x), 'r:')
Benefits of this method are that the syntax is closer to calls of subplot() instead of subplots(). E.g. subplots doesn't seem to support using a GridSpec for controlling the spacing of the subplots, but both subplot() and add_subplot() do.
You can use plt.figure(figsize = (16,8)) to change figure size of a single plot and with up to two subplots. (arguments inside figsize lets to modify the figure size)
To change figure size of more subplots you can use plt.subplots(2,2,figsize=(10,10)) when creating subplots.
from matplotlib import pyplot as plt
lis=[img,gaussian_img,gaussian_img_8bit]
f,axs=plt.subplots(3,1,figsize=(25,25)) #ROW,COLUMN
axs[0].imshow(lis[0])
axs[1].imshow(lis[1])
axs[2].imshow(lis[2])
For plotting subplots in a for loop which is useful sometimes:
Sample code to for a matplotlib plot of multiple subplots of histograms from a multivariate numpy array (2 dimensional).
plt.figure(figsize=(16, 8))
for i in range(1, 7):
plt.subplot(2, 3, i)
plt.title('Histogram of {}'.format(str(i)))
plt.hist(x[:,i-1], bins=60)