简体   繁体   English

如何在python程序中使用包含特定字符的列表作为绘图标记?

[英]How to use a list containing specific characters as markers for plot in python program?

I have 4 lists referring to the x, y, z axes and a list containing the characters to be used as markers.我有 4 个引用 x、y、z 轴的列表和一个包含要用作标记的字符的列表。 When printing the lists, they all have a column format.打印列表时,它们都有列格式。 The 3D plot is working perfectly, without the addition of custom markers. 3D 绘图完美运行,无需添加自定义标记。 The list of bookmarks is as follows (in file <file_icons.txt>):书签列表如下(在文件 <file_icons.txt> 中):

$O$
$H$
$H$
$O$
$H$
$H$
$O$
$H$
$H$
$O$
$H$
...

    data2=[]
    markers=[]
    with open('file_icons.txt') as file_icons:
            for line in file_icons:
                row = line.split()
                data2.append(row[:-1])
                markers.append(row[-1])
        markersS = np.asarray(markers, dtype=np.str, order='C')

And to plot I used the following:为了绘制我使用了以下内容:


    text_style = dict(horizontalalignment='right', verticalalignment='center',
                      fontsize=12, fontdict={'family': 'monospace'})
    marker_style = dict(linestyle=':', color='0.8', markersize=10,
                        mfc="C0", mec="C0")
    fig, ax = plt.subplots()
    fig.subplots_adjust(left=0.4)
    marker_style.update(mec="None", markersize=5)
    fig = plt.figure(1)
    ax = fig.add_subplot(111, projection='3d')
    ax = Axes3D(fig)
    
    ax.plot(x,y,z,marker=markersS,**marker_style)
    
    fig.savefig('water_confined_3d.png',dpi=100)
    plt.show()

When trying to run, I get the following error:尝试运行时,出现以下错误:

ValueError: Unrecognized marker style array(['$O$', '$H$', '$H$', ..., '$Mo$', '$Mo$', '$Mo$'], dtype='<U4') ValueError: 无法识别的标记样式数组(['$O$', '$H$', '$H$', ..., '$Mo$', '$Mo$', '$Mo$'], dtype ='<U4')

Trying a loop one by one (as follows) the program reads the list but writes all the markers at each of the plotted points.一个接一个地尝试循环(如下所示),程序读取列表,但在每个绘制点处写入所有标记。

for n in markersS:
    ax.plot(x,y,z,marker=n,**marker_style)

How could I get each marker to be written in its specific position?我怎样才能让每个标记都写在它的特定位置? For example: At x1, y1, z1, marker = marker1 ... xn, yn, zn, marker = markern例如:在 x1, y1, z1, 标记 = 标记 1 ... xn, yn, zn, 标记 = 标记 n

You have to call ax.plot for every point separately if you want your points to be plotted using different markers:如果要使用不同的标记绘制点,则必须为每个点分别调用ax.plot

for xi, yi, zi, mi in zip(x, y, z, markersS):
    ax.plot([xi], [yi], [zi], marker=mi, **marker_style)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM