在matplotlib中标记一系列数据

时间:2020-07-14 10:29:43

标签: python matplotlib

我有一个matplotlib图,它想用一个标签标记y轴上的数据范围,并用大括号来标注每个范围。这里有一个similar question,但是如果括号应该在图的外部,但是在轴标签所在的空间中,这种方法不起作用,在我的情况下这是必需的,因为我要注释热图,该图内的所有空间均已使用。

我所拥有的:

enter image description here

我想要什么:

enter image description here

示例图的代码:

    //function to check if the user is logged in
Future getCurrentUser() async {
        FirebaseUser _user = await FirebaseAuth.instance.currentUser();
        print("User: ${_user?.email ?? "None"}");
        print(_user);
        return _user;
      }
//using future builder to show whichever screen you want to show loading screen is 
//empty screen with circular progress indicator in center i hope this will help you
     home: FutureBuilder(
              future: getCurrentUser(),
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return LoadingScreen();
                } else if (snapshot.connectionState == ConnectionState.done) {
                  return snapshot.data == null ? LoginScreen() : MainScreen();
                }
                return LoginScreen();
              },
            ),

2 个答案:

答案 0 :(得分:1)

我无法处理您在注释中看到的高级内容,但是我尝试使用'Latex'做到了。这不是您的答案,但我会分享给您参考。

import numpy as np
import matplotlib.pyplot as plt

arr = np.array([[3,4],[2,3.5],[10,11],[9,10]])

fig = plt.figure(figsize=(4,4))
ax = fig.add_subplot(111)

ax.imshow(arr)

ax.set_title("example plot")
ax.text(-1.10, 0.25, r'$group 1$', fontsize=24, ha='left', va='center', rotation='horizontal', transform=ax.transAxes)
ax.text(-0.35, 0.25, '$\{$', fontsize=72, ha='left', va='center', rotation='horizontal', transform=ax.transAxes)
ax.text(-1.10, 0.75, r'$group 2$', fontsize=24, ha='left', va='center', rotation='horizontal', transform=ax.transAxes)
ax.text(-0.35, 0.75, '$\{$', fontsize=72, ha='left', va='center', rotation='horizontal', transform=ax.transAxes)
ax.set_yticklabels([])
ax.set_yticks([])

enter image description here

答案 1 :(得分:0)

由于j_4321建议和链接问题中响应的代码,我提出了以下解决方案。 这并不完美,因为我仍然需要手动调整不同大小图的值。它还需要自动显示,这会稍微改变热图:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

def draw_brace(ax, span, text, axis):
    """Draws an annotated brace on the axes."""
    # axis could be reversed
    xx = ax.get_xlim()
    xmin = np.min(xx)
    xmax = np.max(xx)
    yy = ax.get_ylim()
    ymin = np.min(yy)
    ymax = np.max(yy)
    xspan = xmax - xmin
    yspan = ymax - ymin
    
    if axis=="y":
        tspan = yspan
        ospan = xspan
        omin  = xmin
    else:
        ospan = yspan
        omin  = ymin
        tspan = xspan
    
    amin, amax = span
    span = amax - amin
    
    resolution = int(span/tspan*100)*2+1 # guaranteed uneven
    beta = 300./tspan # the higher this is, the smaller the radius
    
    x = np.linspace(amin, amax, resolution)
    x_half = x[:resolution//2+1]
    y_half_brace = (1/(1.+np.exp(-beta*(x_half-x_half[0])))
                    + 1/(1.+np.exp(-beta*(x_half-x_half[-1]))))
    y = np.concatenate((y_half_brace, y_half_brace[-2::-1]))
    y = omin + (.05*y - .01)*ospan # adjust vertical position

    #ax.autoscale(False)
    if axis == "y":
        ax.plot(-y +1 , x, color='black', lw=1)
        ax.text(0.8+ymin+.07*yspan, (amax+amin)/2., text, ha='center', va='center')
    else:
        ax.plot(x, y, color='black', lw=1)
        ax.text((amax+amin)/2.,ymin+.07*yspan, text, ha='center', va='center')
    

arr = np.array([[3,4],[2,3.5],[10,11],[9,10]])
fig = plt.figure()

gs = fig.add_gridspec(nrows=1, ncols=2, wspace=0,width_ratios=[1,4])

ax2 = fig.add_subplot(gs[:, 1])

ax2.imshow(arr)
ax2.set_title("example plot")

ax2.set_yticklabels([])
ax2.set_yticks([])
ax2.set_xticklabels([])
ax2.set_aspect('auto')

ax1 = fig.add_subplot(gs[:, 0], sharey=ax2)
ax1.set_xticks([])
ax1.set_xticklabels([])
ax1.set_aspect('auto')
ax1.set_xlim([0,1])
ax1.axis('off')
draw_brace(ax1, (0, 1), 'group1',"y")
draw_brace(ax1, (2, 3), 'group2',"y")

fig.subplots_adjust(wspace=0, hspace=0)

这将创建以下图: enter image description here

相关问题