我试图在同一子图中绘制多个直方图,并向其添加图例。图例要求每个标签都有一个字符串。对于每个字符串,我使用的是数学表达式,但还需要在其中包含一个变量。
更具体地说,对于每个图例标签,我希望它看起来像r“ $ \ mathcal {M} _ {j} $”,其中j是我使用for循环进行的变量。 我检查了Matplotlib official documentation,但没有提及这种用法。我也做了很多Google搜索,没有结果。
我还在此处包括了一个简化代码,以更清楚地说明问题:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
n = 3 # number of subplots
m = 4 # number of histograms in each subplot
fig, axs = plt.subplots(nrows=n, ncols=1)
for ax in axs.reshape(-1):
# put data to be plotted in this subplot in a list
data_plot = []
# list for legend label
model_label_array = []
for j in range(0, m):
# generate random numbers to be plotted
data_plot.append(np.randn(100))
# generate label string for this group of data
model_label_array.append("$\mathcal{M}_{str(j)}$")
# plot
ax.hist(data_plot,
label=model_label_array)
以下图表是我现在得到的: please click here for image
我希望图例看起来像$ \ mathcal {M} _ {j} $,其中j是模型的索引。
答案 0 :(得分:0)
@ImportanceOfBeingErnest指出,
Dim dt As New DataTable
Try
*
open connection *
oraComm.CommandText = "SELECT Panel_ID, CELL_TYPE, IV_RESULT, MODEL_CD,IV_MODE,modelname,stringlength, quantitycell, " & _ " A_CL_LT_A1, A_CL_LT_A2, A_CL_LT_B1, A_CL_LT_B2, " & _ " A_WAF_ID_1, A_WAF_ID_2, A_WAF_ID_3, A_WAF_ID_4, " & _ " B_CL_LT_A1, B_CL_LT_A2, B_CL_LT_B1, B_CL_LT_B2, " & _ " B_WAF_ID_1, B_WAF_ID_2, B_WAF_ID_3, B_WAF_ID_4, " & _ " C_CL_LT_A1, C_CL_LT_A2, C_CL_LT_B1, C_CL_LT_B2, " & _ " C_WAF_ID_1, C_WAF_ID_2, C_WAF_ID_3, C_WAF_ID_4 " & _
"FROM table1 s,table2 m " & _
"WHERE s.areacode ='hawaii' AND m.areacode ='hawaii' AND s.panel_id = '" & Me.TextBox1.Text.Trim & "' "
ada = New Oracle.DataAccess.Client.OracleDataAdapter(oraComm)
ada.Fill(ds.Tables("M_DATANEW"))
dt = ds.Tables("M_DATANEW")
Dim a, c, d, e, f As String
Dim dtnew = New DataTable()
dtnew.Columns.Add("PANEL_ID", GetType(String))
dtnew.Columns.Add("CELL_TYPE", GetType(String))
dtnew.Columns.Add("IV_RESULT", GetType(String))
dtnew.Columns.Add("IV_MODE", GetType(String))
dtnew.Columns.Add("MODEL_CD", GetType(String))
a = dt.Rows(0)("panel_id").ToString.Trim
c = dt.Rows(0)("cell_type").ToString.Trim
d = dt.Rows(0)("iv_result").ToString.Trim
e = dt.Rows(0)("iv_mode").ToString.Trim
f = dt.Rows(0)("model_cd").ToString.Trim
Dim waf_ini As String = ""
For w = 1 To CInt(dt.Rows(0)("stringlength")) + 1
For x = 1 To CInt(dt.Rows(0)("cellquantity")) + 1
If w = 1 Then
waf_ini = "A_WAF_ID"
ElseIf w = 2 Then
waf_ini = "B_WAF_ID"
ElseIf w = 3 Then
waf_ini = "C_WAF_ID"
End If
dtnew.NewRow()
dtnew(0)("PANEL_ID") = a.Trim
dtnew(0)("CELL_TYPE") = c.Trim
dtnew(0)("IV_RESULT") = d.Trim
dtnew(0)("IV_MODE") = e.Trim
dtnew(0)("MODEL_CD") = f.Trim
dtnew.Rows.Add()
Next
Next
If dt.Rows.Count < > 0 Then
GridView1.DataSource = dt
GridView1.DataBind()
Else
GridView1.DataSource = Nothing
GridView1.DataBind()
End If
Catch ex As Exception
Throw ex
Finally
*
close connection *
End Try
End Sub
是答案。好吧,我有点愚蠢。但在我的辩护中,我始终给人一种印象,即数学格式的字符串需要原始字符串。显然不是这种情况。
但是出现了更多的混乱:在documentation of matplotlib中,它确实表示数学表达式需要原始字符串。我们为什么现在不需要它?