I work with magnetic materials and I haven't found a way to automatically plot structures with their corresponding magnetic moments. For this specific project I am using VASP. I am trying to plot something like this:

Magnetic moment plot example.

My POSCAR and OUTCAR files are uploaded to the Matter Modeling SE github page under the folder for question #1780: Here.

  • 2
    $\begingroup$ Maybe this will help however I don't know what you mean automatically $\endgroup$
    – Cody Aldaz
    Commented Jul 28, 2020 at 0:15
  • $\begingroup$ Thanks! I'll download VMD and see if it's easier to visualize magnetic moments using that. With the software that I am currently using, you have to define a point and a vector (it's basically a graphing calculator). It looks nice, but it's a lot of work and there's probably a much easier way. $\endgroup$
    – Daniel M.
    Commented Jul 28, 2020 at 0:27
  • $\begingroup$ @DanielMaldonado If you look at Cody's input file for VMD (he gave the input file at the bottom of that answer), then you can see that he gave the coordinates and the modes. Is that something you want to avoid? $\endgroup$ Commented Jul 28, 2020 at 20:39
  • 1
    $\begingroup$ @NikeDattani This type of format would be ideal, it should be simple to arrange the magnetic moments in this order and paste into the input file. However, I think Tristan's answer might be more thorough because it has the ability to be visualized by multiple different programs, including VMD. $\endgroup$
    – Daniel M.
    Commented Jul 29, 2020 at 20:43

1 Answer 1


This took some digging, but I can supply at least a partial answer.

Assuming you can copy the list of magnetic moments to a list, you could trick whatever software you are using for visualization into outputting it indirectly. A typical POSCAR can look like the following.

    25 0 0
    0 25 0
    0 0 25
Selective dynamics
  10 10 10   F   F   F
  10 10 11   F   F   F

  0  0  0
  0  0  0

The last 2 sets of vectors represent a velocity and some? softwares will automatically view and let you plot velocities in the way that you want. Simply copying your magnetic moments to these will allow you to visualize it in anything you want (VMD, ASE, etc).

Added on 7/31/2020 Here is an implementation using ASE to visualize vectors.

from sys import argv
from ase.io import read
from ase.visualize import view
import numpy as np

file_in = argv[1]
text = open(file_in).readlines()

orbital = 4
if len(argv) > 3:
    orbital = argv[2]

x, y, z = None, None, None

def process_mag(index):
    return_array = []
    while "--------" not in text[index]:
        index += 1
    return return_array

for index in range(len(text)):
    if " magnetization (x)" in text[index]:
        index += 4
        x = process_mag(index)
    if " magnetization (y)" in text[index]:
        index += 4
        y = process_mag(index)
    if " magnetization (z)" in text[index]:
        index += 4
        z = process_mag(index)

directions = zip(x, y, z)

for X, Y, Z in directions:
    print("{} {} {}". format(X, Y, Z))

atoms = read(file_in)

This can be run with the OUTCAR as the first argument and optionally the magnetization orbital as the second (although it will default to total). Then by clicking "show velocities" under "view" menu, the vectors will be shown. The script will also print the X Y Z data for you to plot in another software.

It seems that most software packages ignore the POSCAR velocities, this is a somewhat annoying consequence of the file format being badly documented. Maybe there is another approach that someone can give since this is a bad visualization for publications.

enter image description here

  • 1
    $\begingroup$ The reality is, this isn't a common use case so most people that need to do this probably have to homebrew up something. It would be good to document and provide something for people searching in the future. $\endgroup$ Commented Jul 29, 2020 at 18:23
  • 2
    $\begingroup$ One of the nice things about being in the Stack Exchange network (rather than just making our own Q/A site) is that SEO (search engine optimization) is done very well here by the SE developers, so when people search on Google for something, our site will often come up at the top of the list of search results. So your python code could be helpful to many users in the future. If it's a few hundreds lines it can easily fit in a code block here; otherwise I made a Git repository for the input & output files I made when solving the user's problem for question #1715. $\endgroup$ Commented Jul 29, 2020 at 18:35
  • 1
    $\begingroup$ I will try to do this in the next 24 hours. $\endgroup$ Commented Jul 29, 2020 at 21:52
  • 1
    $\begingroup$ This must be a trick with POSCAR. For example, a similar trick can be done with PDBs. The "Occupancy" column of a pdb can be filled with numerical values such as partial charges to plot different atoms with different colors $\endgroup$
    – Cody Aldaz
    Commented Jul 30, 2020 at 3:26
  • 1
    $\begingroup$ This was a slight nightmare, I couldnt actually find any software that correctly supported reading velocities and plotting vectors for a POSCAR. I have updated my answer though with what I was able to do. $\endgroup$ Commented Jul 31, 2020 at 15:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .