matplotlib中圆的标签

2024-10-01 22:25:47 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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


Tags: infromimportmatplotlibasshnpplt
2条回答

如果使用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()

哦!莫尔圆!在

对于较低级别的界面,它不会自动处理(即手动创建艺术家并添加它)。因此,您需要将艺术家和标签传递到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()

enter image description here

相关问题 更多 >

    热门问题