Kim*_*ine 7 python contour plotly-python
我画了两张等高线图,一张是用 matplotlib 画的,另一张是用plotly 画的。我想在图中添加更多等高线。对于 matplotlib,没有问题。但关于情节,我不知道如何改变轮廓线的数量。下面是我的代码。
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('seaborn')
import math
x = np.linspace(-np.pi, np.pi, num=50)
y = x
def pf(a, b):
return math.cos(b) / (1 + a**2)
f = np.empty((len(x), len(y)))
for i in range(len(x)):
for j in range(len(y)):
f[i,j] = pf(x[i], y[j])
# contour plot using matplotlib
cp = plt.contour(x, y, f, 45, cmap='viridis')
plt.clabel(cp, inline=1, fontsize=10);
Run Code Online (Sandbox Code Playgroud)
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Contour(z=f, x=x, y=y, contours_coloring='lines', line_width=1,
contours={"showlabels":True, "labelfont":{"size":12, "color":'green'}}))
fig.update_layout(
{"title": {"text": "<b>Contour Plot with Plotly</b>", "x": 0.5, "y": 0.85, "font": {"size": 15} },
"showlegend": True,
"xaxis": {"title": "x axis", "showticklabels":True, "dtick": 1},
"yaxis": {"title": "y axis", "showticklabels": True, "dtick": 1},
"autosize":False,
"width":600,
"height":450})
fig.show()
Run Code Online (Sandbox Code Playgroud)
您可以使用参数调整等值线图的详细程度ncontours。
import math
import numpy as np
import plotly.graph_objects as go
#Create data
x = np.linspace(-np.pi, np.pi, num=50)
y = x
def pf(a, b):
return math.cos(b) / (1 + a**2)
f = np.empty((len(x), len(y)))
for i in range(len(x)):
for j in range(len(y)):
f[i,j] = pf(x[i], y[j])
#Create plot
fig = go.Figure()
fig.add_trace(go.Contour(z=f, x=x, y=y, contours_coloring='lines',
line_width=1,contours={"showlabels":True,
"labelfont":{"size":12, "color":'green'}}))
#Adjust the number of coutour levels
fig.update_traces(ncontours=45, selector=dict(type='contour'))
fig.show()
Run Code Online (Sandbox Code Playgroud)
还可以修改coutour_size轨迹的参数来调整每个轮廓级别之间的步长。为此,您还需要指定绘图的contour_start和。contour_end
#Create plot
fig = go.Figure()
fig.add_trace(go.Contour(z=f, x=x, y=y, contours_coloring='lines',
line_width=1,contours={"showlabels":True,
"labelfont":{"size":12, "color":'green'}}))
#Adjust the number of coutour levels
fig.update_traces(contours_start=-1, contours_end=1, contours_size=0.1)
fig.show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1753 次 |
| 最近记录: |