在同一组轴上学习python多重绘图

2024-04-26 13:35:54 发布

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

如何在Python中设置此绘图?你知道吗

以下是我在Mathematica的工作:

mykvalue = Table[k, {k, 0, 10, 1}];

u[r_, k_] = 1/(2*r^2) + k/(2*r^2);

Plot[u[r, mykvalue], {r, 0, 5}, PlotStyle -> {Red}, 
 PlotRange -> {{0, 5}, {0, 2}}]

enter image description here


Tags: 绘图plottableredmathematicaplotrangemykvalueplotstyle
2条回答

在我看来,最简单的方法之一是使用多维列表存储值,而不是使用matplotlib绘图。这就是我的意思

from math import *
import numpy as np
import matplotlib.pyplot as plt

k = range(11)

yvals = [[] for i in range(len(k))] #there should be a more pythonic way to
x = np.arange(0.4,5,0.1)               #create nested lists i think, ALM I added np.


for i in k:
    for j in x:
        i = float(i) #this conversion from int to float was to debug,       
        j = float(j) #it should be made better
        y=1/(2*j**2) + i/(2*j**2)
        i = int(i)
        yvals[i].append(y)


for i in k:
    plt.plot(x,yvals[i])

plt.show()

我想这应该管用。你可以摆弄matplotlib来固定轴,得到标签之类的东西。你知道吗

相关问题 更多 >