简体   繁体   中英

Place two inset graphs within a matplotlib plot

I have a csv file that looks like eg

SET ,A,B,C
1,1,1,1
2,2,2,2
3,3,3,3
4,4,4,4
5,5,5,5
6,6,6,6
7,7,7,7
8,8,8,8
9,9,9,9
10,10,10,7
15,20,23,17
20,30,33,27
25,40,43,37
30,50,53,47
35,60,63,57
40,70,73,67

I am using the following to graph it:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

data = pd.read_csv('test.csv', sep=',')

ax = data.plot(x="SET ", y=["A"],marker='^', linestyle='--', color='#CC2936')
data.plot(x="SET ", y=["B"],ax=ax,marker='o',linestyle='-',color='#CC2936')
data.plot(x="SET ", y=["C"], ax=ax,marker='^', linestyle='--', color='#08415C')

plt.show()

The above produces a plot that looks like so: 在此处输入图片说明

I would like to add two small insets to this graph: one in the top left(where the legend currently is) showing the data from x=0 to x=10 zoomed in, and then another in the bottom right showing the data from x=30 to x=40 zoomed in. I am pretty new to matplotlib so any help is appreciated. Thank you!

Thanks to the documentation posted by @DavidG, I think I've got it figured out!

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

data = pd.read_csv('test.csv', sep=',')
set_p = data["SET "].tolist()
A = data["A"].tolist()
B = data["B"].tolist()
C = data["C"].tolist()

ax = data.plot(x="SET ", y=["A"],marker='^', linestyle='--', color='#CC2936')
data.plot(x="SET ", y=["B"],ax=ax,marker='o',linestyle='-',color='#CC2936')
data.plot(x="SET ", y=["C"], ax=ax,marker='^', linestyle='--', color='#08415C')

axins0 = inset_axes(ax, width="30%", height="30%", loc=2)
axins0.plot(set_p[:3], A[:3], marker='^', linestyle='--', color='#CC2936')
axins0.plot(set_p[:3], B[:3], marker='^', linestyle='--', color='#CC2936')
axins0.plot(set_p[:3], C[:3], marker='^', linestyle='--', color='#08415C')
axins0.tick_params(labelleft=False, labelbottom=False)

axins1 = inset_axes(ax, width="30%", height="30%", loc=4)
axins1.plot(set_p[6:], A[6:], marker='^', linestyle='--', color='#CC2936')
axins1.plot(set_p[6:], B[6:], marker='o', linestyle='-', color='#CC2936')
axins0.plot(set_p[6:], C[6:], marker='^', linestyle='--', color='#08415C')
axins1.tick_params(labelleft=False, labelbottom=False)


plt.show()

在此处输入图片说明

Although you answered your own question nicely, the only difference in my answer is in using conditions in the DataFrame while plotting rather than using slicing/indices 3: and 6: .

I also specify the coordinates, width and height of the insets manually instead of in-built locations (locs) which gives me more freedom on where to put the insets for better aesthetics.

fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111)
data = pd.read_csv('test.csv', sep=',')

data.plot(x="SET ", y=["A"], marker='^', linestyle='--', color='#CC2936', ax=ax)
data.plot(x="SET ", y=["B"], marker='o',linestyle='-',color='#CC2936', ax=ax)
data.plot(x="SET ", y=["C"], marker='^', linestyle='--', color='#08415C', ax=ax)
plt.legend(loc=(0.02, 0.3), fontsize = 16)

ax2 = fig.add_axes([0.2, 0.6, 0.3, 0.25])
data[data["SET "]<=10].plot(x="SET ", y=["A"], marker='^', linestyle='--', color='#CC2936', ax=ax2)
data[data["SET "]<=10].plot(x="SET ", y=["B"], marker='o',linestyle='-',color='#CC2936', ax=ax2)
data[data["SET "]<=10].plot(x="SET ", y=["C"], marker='^', linestyle='--', color='#08415C', ax=ax2)
ax2.legend_.remove()
ax2.set_xlabel("")

ax3 = fig.add_axes([0.6, 0.2, 0.27, 0.25])
data[(data["SET "]>30) & (data["SET "]<=40)].plot(x="SET ", y=["A"], marker='^', linestyle='--', color='#CC2936', ax=ax3)
data[(data["SET "]>30) & (data["SET "]<=40)].plot(x="SET ", y=["B"], marker='o',linestyle='-',color='#CC2936', ax=ax3)
data[(data["SET "]>30) & (data["SET "]<=40)].plot(x="SET ", y=["C"], marker='^', linestyle='--', color='#08415C', ax=ax3)
ax3.legend_.remove()
ax3.set_xlabel("")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM