import pandas as pd
import matplotlib.pyplot as plt
def csv_til_liste(filnavn):
occuDF = pd.read_csv(filnavn)
occuList=occuDF.values.tolist()
return occuDF, occuList
occuDF, occuList = csv_til_liste("occupancy.csv")
plt.figure(1)
occuDF.boxplot(column = 'Temperature', by = 'Occupancy')
plt.suptitle('')
x=(1, 2, 3, 4, 5)
y=(1,2,3,4,5)
plt.figure(2)
plt.plot(x,y)
plt.show()
When I run the program, the two plots are plotted in one figure, but I want them in two separate figures.
The pandas.DataFrame.boxplot takes an ax parameter, as written in the docs.
So you can use:
fig1 = plt.figure()
ax1 = fig1.add_subplot(1, 1, 1)
occuDF.boxplot(column = 'Temperature', by = 'Occupancy', ax=ax1)
plt.suptitle('')
x=(1, 2, 3, 4, 5)
y=(1,2,3,4,5)
fig2 = plt.figure(2)
ax2 = fig2.add_subplot(1, 1, 1)
ax2.plot(x,y)
plt.show()
Otherwise, you can plot in different subplots of the same figure by applying minimal changes.
Related
I have 5 datasets that have thousands of x and y coordinates grouped by 'frame' that create 5 trajectory plots. I'd like to mark the first and last coordinates for each plot but having difficulty figuring it out. I am using Jupiter Notebook.
mean_pos1 = gr1.mean()
mean_pos2 = gr2.mean()
mean_pos3 = gr3.mean()
mean_pos4 = gr4.mean()
mean_pos5 = gr5.mean()
plt.figure()
xlim=(200, 1500)
ylim=(0, 1200)
ax1 = mean_pos1.plot(x='x', y='y',color='blue',label='Dolphin A'); ax1.set_title('mean trajectory');
ax2 = mean_pos2.plot(x='x', y='y',color='red',label='Dolphin B'); ax2.set_title('mean trajectory');
ax3 = mean_pos3.plot(x='x', y='y',color='green',label='Dolphin C'); ax3.set_title('mean trajectory');
ax4 = mean_pos4.plot(x='x', y='y',color='magenta',label='Dolphin D'); ax4.set_title('mean trajectory');
ax5 = mean_pos5.plot(x='x', y='y',color='cyan',label='Dolphin E'); ax5.set_title('mean trajectory');
ax1.set_xlim(xlim)
ax1.set_ylim(ylim)
ax2.set_xlim(xlim)
ax2.set_ylim(ylim)
ax3.set_xlim(xlim)
ax3.set_ylim(ylim)
ax4.set_xlim(xlim)
ax4.set_ylim(ylim)
ax5.set_xlim(xlim)
ax5.set_ylim(ylim)
plt.show()
the output of them looks like this:
Use the scatter method to plot the markers separately on the same axis by grabbing the first and last elements from your x and y series:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({'x': np.random.normal(3,0.2,10), 'y': np.random.normal(5,0.3,10)})
fig, ax = plt.subplots()
df.plot(x='x', y='y', ax=ax)
ax.scatter(df['x'].iloc[0], df['y'].iloc[0], marker='o', color='red')
ax.scatter(df['x'].iloc[-1], df['y'].iloc[-1], marker='o', color='red')
plt.show()
I have a series of pyplot subplots that I've created using a gridspec. They all have an hspace between them, which is fine, except that I would like to keep three of them without any space. Is there a way to do this? Currently, they look like this:
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure()
grid_spec = gridspec.GridSpec(nrows=10, ncols=10)
grid_spec.update(hspace=1.5)
ax1 = plt.subplot(grid_spec[0:4, :])
ax2 = plt.subplot(grid_spec[4:7, :], sharex=ax1)
# I would like to group the next 3 together
# so that they are stacked top to bottom and side by side
ax3 = plt.subplot(grid_spec[7:8, :5])
ax4 = plt.subplot(grid_spec[8:, :5], sharex=ax3)
ax5 = plt.subplot(grid_spec[8:, 5:6], sharey=ax4)
plt.show()
I would like them to be arranged like this so I can plot the following 2-D KDE diagram and have the relevant 1-D diagrams above and to the right (roughly displaying this sort of data crudely drawn in paint):
I appreciate any help with this one. Can't seem to find documentation on this sort of thing. Thanks!
You can use mpl_toolkits.axes_grid1.make_axes_locatable to subdivide the area of a subplot of a 3 x 2 grid.
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
fig = plt.figure()
gs = fig.add_gridspec(nrows=3, ncols=2, hspace=.5,
height_ratios=[4, 3, 3], width_ratios=[7, 4])
ax1 = fig.add_subplot(gs[0, :])
ax2 = fig.add_subplot(gs[1, :], sharex=ax1)
ax3 = fig.add_subplot(gs[2, 0])
div = make_axes_locatable(ax3)
ax4 = div.append_axes("top", "40%", pad=0.2, sharex=ax3)
ax5 = div.append_axes("right", "25%", pad=0.2, sharey=ax3)
ax4.tick_params(labelbottom=False)
ax5.tick_params(labelleft=False)
plt.show()
Also, you can create a subgridspec, like
import matplotlib.pyplot as plt
from matplotlib import gridspec
fig = plt.figure()
gs = gridspec.GridSpec(nrows=3, ncols=2, hspace=.5,
height_ratios=[4, 3, 3], width_ratios=[7, 4])
ax1 = fig.add_subplot(gs[0, :])
ax2 = fig.add_subplot(gs[1, :], sharex=ax1)
sub_gs = gridspec.GridSpecFromSubplotSpec(2,2, subplot_spec=gs[2,0], hspace=0.3, wspace=0.1,
height_ratios=[1,3], width_ratios=[3,1])
ax3 = fig.add_subplot(sub_gs[1,0])
ax4 = fig.add_subplot(sub_gs[0,0], sharex=ax3)
ax5 = fig.add_subplot(sub_gs[1,1], sharey=ax3)
ax4.tick_params(labelbottom=False)
ax5.tick_params(labelleft=False)
plt.show()
In both cases you will probably want to fine tune the parameters a bit. In general, the matplotlib gridspec tutorial gives a nice overview with many examples on this matter.
I am having a problem using subplots in matplotlib. I am getting empty plots when using subplots. What is wrong with my implementation?
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
fig = plt.figure(figsize=(15,20))
ax = 1
for column in passenger_train_df.columns.values.tolist():
if column != 'Survived':
fig.add_subplot(4, 3, ax)
if column == 'Age':
age_binned=pd.cut(passenger_train_df['Age'], 10)
age_binned.sort_values(inplace=True)
table= pd.crosstab(age_binned, passenger_train_df['Survived'])
elif column == 'Fare':
fare_binned=pd.cut(passenger_train_df['Fare'], 10)
fare_binned.sort_values(inplace=True)
table= pd.crosstab(fare_binned, passenger_train_df['Survived'])
else:
table= pd.crosstab(passenger_train_df[column], passenger_train_df['Survived'])
table.div(table.sum(1).astype(float), axis=0).plot(kind='bar', stacked=True)
ax+=1
You need to return the axes that is created when you use fig.add_subplot(4, 3, ax). You then need to pass this as an argument into the plot function:
So change fig.add_subplot(4, 3, ax) to
axes = fig.add_subplot(4, 3, ax)
And then when you plot, pass in axes as an argument using ax=:
table.div(table.sum(1).astype(float), axis=0).plot(kind='bar', stacked=True, ax=axes)
I'm somewhat new to matplotlib. What I'm trying to do is write code that saves several figures to eps files, and then generates a composite figure. Basically what I'd like to do is have something like
import matplotlib.pyplot as plt
from matplotlib import gridspec
def my_plot_1():
fig = plt.figure()
...
return fig
def my_plot_2():
fig = plt.figure()
...
return fig
def my_combo_plot(fig1,fig2):
fig = plt.figure()
gs = gridspec.GridSpec(2,2)
ax1 = plt.subplot(gs[0,0])
ax2 = plt.subplot(gs[0,1])
ax1 COPY fig1
ax2 COPY fig2
...
where then later I could do something like
my_combo_plot( my_plot_1() , my_plot_2() )
and have all the data and settings get copied from the plots returned by the first two functions, but I can't figure out how this would be done with matplotlib.
Since pyplot kind of works like a state machine, I'm not sure if what you are asking for is possible. I would instead factor out the drawing code, something like this:
import matplotlib.pyplot as plt
def my_plot_1(ax=None):
if ax is None:
ax = plt.gca()
ax.plot([1, 2, 3], 'b-')
def my_plot_2(ax=None):
if ax is None:
ax = plt.gca()
ax.plot([3, 2, 1], 'ro')
def my_combo_plot():
ax1 = plt.subplot(1,2,1)
ax2 = plt.subplot(1,2,2)
my_plot_1(ax1)
my_plot_2(ax2)
Using the answer https://stackoverflow.com/a/46906599/5267751 it's possible to move the axes from one figure to other (using pickle it's also possible to keep the old figure).
Add set_subplotspec to position the resulting axes:
import matplotlib.pyplot as plt
from matplotlib import gridspec
def my_plot_1():
fig = plt.figure()
plt.plot([1, 2, 3], 'b-')
return fig
def my_plot_2():
fig = plt.figure()
plt.plot([3, 2, 1], 'ro')
return fig
fig1 = my_plot_1()
fig2 = my_plot_2()
def my_combo_plot(fig1,fig2):
fig = plt.figure()
gs = gridspec.GridSpec(2,2)
ax1 = fig1.axes[0]
ax1.remove()
ax1.figure = fig
fig.add_axes(ax1)
ax1.set_subplotspec(gs[0, 0])
ax2 = fig2.axes[0]
ax2.remove()
ax2.figure = fig
fig.add_axes(ax2)
ax2.set_subplotspec(gs[0, 1])
plt.close(fig1)
plt.close(fig2)
my_combo_plot( my_plot_1() , my_plot_2() )
plt.show()
The code assumes that each figure contains exactly one axes, however.
I need to add two subplots to a figure. One subplot needs to be about three times as wide as the second (same height). I accomplished this using GridSpec and the colspan argument but I would like to do this using figure so I can save to PDF. I can adjust the first figure using the figsize argument in the constructor, but how do I change the size of the second plot?
As of matplotlib 3.6.0, width_ratios and height_ratios can now be passed directly as keyword arguments to plt.subplots and subplot_mosaic, as per What's new in Matplotlib 3.6.0 (Sep 15, 2022).
f, (a0, a1) = plt.subplots(1, 2, width_ratios=[3, 1])
f, (a0, a1, a2) = plt.subplots(3, 1, height_ratios=[1, 1, 3])
Another way is to use the subplots function and pass the width ratio with gridspec_kw
matplotlib Tutorial: Customizing Figure Layouts Using GridSpec and Other Functions
matplotlib.gridspec.GridSpec has available gridspect_kw options
import numpy as np
import matplotlib.pyplot as plt
# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)
# plot it
f, (a0, a1) = plt.subplots(1, 2, gridspec_kw={'width_ratios': [3, 1]})
a0.plot(x, y)
a1.plot(y, x)
f.tight_layout()
f.savefig('grid_figure.pdf')
Because the question is canonical, here is an example with vertical subplots.
# plot it
f, (a0, a1, a2) = plt.subplots(3, 1, gridspec_kw={'height_ratios': [1, 1, 3]})
a0.plot(x, y)
a1.plot(x, y)
a2.plot(x, y)
f.tight_layout()
You can use gridspec and figure:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)
# plot it
fig = plt.figure(figsize=(8, 6))
gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1])
ax0 = plt.subplot(gs[0])
ax0.plot(x, y)
ax1 = plt.subplot(gs[1])
ax1.plot(y, x)
plt.tight_layout()
plt.savefig('grid_figure.pdf')
I used pyplot's axes object to manually adjust the sizes without using GridSpec:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.2)
y = np.sin(x)
# definitions for the axes
left, width = 0.07, 0.65
bottom, height = 0.1, .8
bottom_h = left_h = left+width+0.02
rect_cones = [left, bottom, width, height]
rect_box = [left_h, bottom, 0.17, height]
fig = plt.figure()
cones = plt.axes(rect_cones)
box = plt.axes(rect_box)
cones.plot(x, y)
box.plot(y, x)
plt.show()
Probably the simplest way is using subplot2grid, described in Customizing Location of Subplot Using GridSpec.
ax = plt.subplot2grid((2, 2), (0, 0))
is equal to
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 2)
ax = plt.subplot(gs[0, 0])
so bmu's example becomes:
import numpy as np
import matplotlib.pyplot as plt
# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)
# plot it
fig = plt.figure(figsize=(8, 6))
ax0 = plt.subplot2grid((1, 3), (0, 0), colspan=2)
ax0.plot(x, y)
ax1 = plt.subplot2grid((1, 3), (0, 2))
ax1.plot(y, x)
plt.tight_layout()
plt.savefig('grid_figure.pdf')
In a simple way, different size sub plotting can also be done without gridspec:
plt.figure(figsize=(12, 6))
ax1 = plt.subplot(2,3,1)
ax2 = plt.subplot(2,3,2)
ax3 = plt.subplot(2,3,3)
ax4 = plt.subplot(2,1,2)
axes = [ax1, ax2, ax3, ax4]
A nice way of doing this was added in matplotlib 3.3.0, subplot_mosaic.
You can make a nice layout using an "ASCII art" style.
For example
fig, axes = plt.subplot_mosaic("ABC;DDD")
will give you three axes on the top row and one spanning the full width on the bottom row like below
A nice thing about this method is that the axes returned from the function is a dictionary with the names you define, making it easier to keep track of what is what e.g.
axes["A"].plot([1, 2, 3], [1, 2, 3])
You can also pass a list of lists to subplot_mosaic if you want to use longer names
fig, axes = plt.subplot_mosaic(
[["top left", "top centre", "top right"],
["bottom row", "bottom row", "bottom row"]]
)
axes["top left"].plot([1, 2, 3], [1, 2, 3])
will produce the same figure