python - Matplotlib:如何提高 streamplot 中的颜色图/线宽质量?

标签 python numpy matplotlib

我有以下代码来生成基于 interp1d 的流图 - 离散数据的插值:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
from scipy.interpolate import interp1d

# CSV Import
a1array=pd.read_csv('a1.csv', sep=',',header=None).values
rv=a1array[:,0]
a1v=a1array[:,1]
da1vM=a1array[:,2]
a1 = interp1d(rv, a1v)
da1M = interp1d(rv, da1vM)

# Bx and By vector components
def bx(x ,y):
    rad = np.sqrt(x**2+y**2)
    if rad == 0:
        return 0
    else:
        return x*y/rad**4*(-2*a1(rad)+rad*da1M(rad))/2.87445E-19*1E-12

def by(x ,y):
    rad = np.sqrt(x**2+y**2)
    if rad == 0:
        return 4.02995937E-04/2.87445E-19*1E-12
    else:
        return -1/rad**4*(2*a1(rad)*y**2+rad*da1M(rad)*x**2)/2.87445E-19*1E-12

Bx = np.vectorize(bx, otypes=[np.float])
By = np.vectorize(by, otypes=[np.float])

# Grid
num_steps = 11
Y, X = np.mgrid[-25:25:(num_steps * 1j), 0:25:(num_steps * 1j)]
Vx = Bx(X, Y)
Vy = By(X, Y)
speed = np.sqrt(Bx(X, Y)**2+By(X, Y)**2)
lw = 2*speed / speed.max()+.5

# Star Radius
circle3 = plt.Circle((0, 0), 16.3473140, color='black', fill=False)

# Plot
fig0, ax0 = plt.subplots(num=None, figsize=(11,9), dpi=80, facecolor='w', edgecolor='k')
strm = ax0.streamplot(X, Y, Vx, Vy, color=speed, linewidth=lw,density=[1,2], cmap=plt.cm.jet)
ax0.streamplot(-X, Y, -Vx, Vy, color=speed, linewidth=lw,density=[1,2], cmap=plt.cm.jet)
ax0.add_artist(circle3)
cbar=fig0.colorbar(strm.lines,fraction=0.046, pad=0.04)
cbar.set_label('B[GT]', rotation=270, labelpad=8)
cbar.set_clim(0,1500)
cbar.draw_all()
ax0.set_ylim([-25,25])
ax0.set_xlim([-25,25])
ax0.set_xlabel('x [km]')
ax0.set_ylabel('z [km]')
ax0.set_aspect(1)
plt.title('polyEos(0.05,2), M/R=0.2, B_r(0,0)=1402GT', y=1.01)
plt.savefig('MR02Br1402.pdf',bbox_inches=0)
plt.show(fig0)

如果你想尝试一些东西,我在这里上传了 csv 文件 https://www.dropbox.com/s/4t7jixpglt0mkl5/a1.csv?dl=0 . 生成以下图: StreamPlot

除了一个我无法弄清楚的小细节外,我实际上对结果非常满意:如果仔细观察,线宽和颜色会以相当大的步幅变化,这在中心尤其明显:

Problem

有没有什么方法/选项可以减少这个步骤的大小,特别是让颜色图变得更暗淡?

最佳答案

我又看了一遍,并没有我想象的那么痛苦。

添加:

    subdiv = 15
    points = np.arange(len(t[0]))
    interp_points = np.linspace(0, len(t[0]), subdiv * len(t[0]))
    tgx = np.interp(interp_points, points, tgx)
    tgy = np.interp(interp_points, points, tgy)
    tx = np.interp(interp_points, points, tx)
    ty = np.interp(interp_points, points, ty)

ty 在轨迹循环中初始化之后(在我的版本中是 164 行)。只需将 subdiv = 15 替换为您想要的任何分割数即可。流图中的所有段都将根据您的选择分割为同样大小的段。每个颜色和线宽仍然可以通过插值数据正确获得。

它不像改变积分步长那么整洁,但它确实绘制了完全相同的轨迹。

subdiv = 15

关于python - Matplotlib:如何提高 streamplot 中的颜色图/线宽质量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39294987/

相关文章:

python - 如何使用matplotlib创建一个原点远离中心且半径大于0的时间螺旋图?

python - 分批训练会导致更多的过拟合

python - 矢量化 : Limit the increase in value between two subsequent numbers in Series (or list/array/whatever)

python - RuntimeWarning : numpy. dtype 大小已更改,可能表示二进制不兼容

python - 如何在 Flask 模板中制作重定向按钮

python - 序列化器 DRF 中数据的非模型字段

python - numpy.unique 给出了集合列表的错误输出

python - matplotlib动画可以保存成什么格式?

python - 如果 x 轴是 Pandas 的日期时间索引,如何绘制多色线

Matplotlib 多个动画多行