【问题标题】:Label for circles in matplotlibmatplotlib 中的圆圈标签
【发布时间】:2014-12-23 23:46:04
【问题描述】:

我已经为莫尔圆中的有效应力构建了一个非常简化的代码。这是我的代码:

from mpl_toolkits.mplot3d import *     # für Druckplots benötigt
from matplotlib import cm
from math import *                     # für pi und exp benötigt
from scipy.special import *            # für expn benötigt
import matplotlib.pyplot as plt        # für Plotting in 2D/3D benötigt
import numpy as np                     # für für Arrays benötigt
import matplotlib.lines as mlines
import os
clear = lambda: os.system('cls')
clear()
#==============================================================================
#                               Mohr Cirlce
#==============================================================================

#depth = 3000.0                  # Reservoirtiefe
#density = 2700.0                # Dichte des überlagernden Gesteins
#G = 12700.0                     # shear modulus [MPa]
#E = 26000.0                     # Young´s modulus in [MPa]
#sv = 9.81*density*depth/1e6     # vertical stress
#sh = 0.5*sv                     # minimum horizontal stress
cf1 = 4.0                        # Cohesion C Fault1 [MPa] 
muef1 = 0.5                      # coefficient of friction  [MPa]
#f1dip = 45                      # Einfallen der Störung
p0 = 15.0                        # initial pore pressure [MPa]

# Mohr failure criterion ##
sigman = np.zeros((80))
tauf1 = np.zeros((80))
for i in range(0,80):
    sigman[i] = i
    tauf1[i] = muef1*sigman[i]+cf1 # Bruchgerade

## Stresses ##
sH = 60.0
sh = 30.0   
smean = float((sH+sh)/2)         # Kreismittelpunkt
shear = float((sH-sh)/2)         # Kreisradius

## Effective Stresses ##
sHeff = sH-p0                       # effektive Vertikalspannung
sheff = sh-p0                       # effektive Horizontalspannung
smeaneff = float((sHeff+sheff)/2)   # Kreismittelpunkt
sheareff = float((sHeff-sheff)/2)   # Kreisradius

## Plotting ## 
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
plt.plot(sigman, tauf1, "b", linewidth=1, linestyle="-", label="Bruchgerade")
ax.axis('scaled')
mohr=plt.Circle((smean,0), radius=shear, color='g', fill=False, label = "Mohr")
mohreff=plt.Circle((smeaneff,0), radius=sheareff, color='r', fill=False)
plt.title("Mohrkreise bei ...")
ax.set_xlabel('$\sigma$ [MPa]', fontsize=12)
ax.set_ylabel('$\tau$ [MPa]', fontsize=12)
plt.xticks(np.arange(0, 90, 10))
plt.yticks(np.arange(0, 45, 5))
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0., numpoints = 1)
ax.add_patch(mohr)
ax.add_patch(mohreff)
plt.show()

我的绿色圆圈代表初始压力,我的红色圆圈代表有效压力。我想在我的图例中为这两个圆圈添加标签,这可能吗?到目前为止,我没有找到任何解决方案。

干杯, A.

【问题讨论】:

    标签: python matplotlib geometry legend


    【解决方案1】:

    如果您使用mpatches,您可以在同一行设置图例标签,适用于有多个绘图时。说你想画numberOfCircles圈子

    import matplotlib.patches as mpatches
    colorList = ['r','b','g'] #list of colours for each circle
    fig, ax = plt.subplots(figsize=(15,10))
    for i in numberOfCircles:
        artist = mpatches.Circle((0.5,0.5),radius=0.5, edgecolor=colorList[i],fill=False,label = #label for the ith circle)
        ax.add_patch(artist)
    plt.legend()
    plot.show()
    

    【讨论】:

      【解决方案2】:

      哦!莫尔圈!

      对于较低级别的界面不会自动处理(即手动创建艺术家并添加它)。因此,您需要将艺术家和标签传递给legend

      举个简单的例子:

      import matplotlib.pyplot as plt
      
      fig, ax = plt.subplots()
      
      circ = plt.Circle((0.5, 0), 0.3, facecolor='none', edgecolor='red')
      ax.add_patch(circ)
      
      ax.legend([circ], ['Stress State'])
      
      # The rest is purely optional and just for appearance.
      ax.axhline(0, color='black')
      ax.axvline(0, color='black')
      ax.margins(0.05)
      ax.axis('scaled')
      ax.set(xlabel=r'Normal Stress', ylabel=r'Shear Stress')
      
      plt.show()
      

      【讨论】:

      • 谢谢乔,正是我想要的!我只需要剪切应力的正区域,因为我想将它用于一些储层应力计算。干杯!
      猜你喜欢
      • 1970-01-01
      • 2014-10-11
      • 2021-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 2016-07-23
      相关资源
      最近更新 更多