簡體   English   中英

在matplotlib圖中放置兩個插圖

[英]Place two inset graphs within a matplotlib plot

我有一個csv文件,例如

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

我正在使用以下圖形進行繪制:

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()

上面產生的圖看起來像這樣: 在此處輸入圖片說明

我想在此圖中添加兩個小插圖:一個在左上角(當前圖例所在的位置)放大了從x = 0到x = 10的數據,然后在另一個右下角顯示了x的數據= 30到x = 40放大了。對於matplotlib來說我是一個新手,所以可以提供任何幫助。 謝謝!

感謝@DavidG發布的文檔,我想我已經知道了!

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()

在此處輸入圖片說明

盡管您很好地回答了自己的問題,但我回答的唯一區別是在繪制數據時使用了DataFrame中的條件,而不是使用切片/索引3:6: :。

我還手動指定插圖的坐標,寬度和高度,而不是內置位置(位置),這使我在放置插圖的位置上擁有更大的自由度,以獲得更好的美觀性。

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("")

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM