0

How can we make a figure like one attached in matplotlib? Thick horizontal lines as markers with their ends connected through dashed lines. I tried, but couldn't find this marker style.

Following is my attempt -

import numpy as np
import matplotlib.pyplot as plt

rc = [0,1,2,3,4,5]
nci = [0.0000,-0.0001,0.4325,1.2711,0.2162,0.0988]
plt.plot(rc,nci,'b--',marker="_",markersize='15')
plt.xlabel("Reaction Coordinate",fontsize=22)
plt.ylabel('Relative Energy (eV)',fontsize=22)
plt.xticks(fontsize=22)
plt.yticks(fontsize=22)
plt.show()

figure

JohanC
  • 71,591
  • 8
  • 33
  • 66
Krishh
  • 15
  • 4
  • 1
    Did you already try to add [arrows](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.arrow.html) and [texts](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.text.html)? Where did you get stuck? – JohanC Mar 15 '22 at 06:46
  • @JohanC I don't need arrows and text (that I will do with image manipulation software's later), just horizontal blue or red bars connected with dashed lines like the figure above. – Krishh Mar 15 '22 at 20:22

1 Answers1

0

There are several methods to draw what you want.
You can use axline, define marker size with the scale of x axis, and others.

Here I write a simple (but bad-looking) solution.

import numpy as np
import matplotlib.pyplot as plt

# Data
len_arr = 6
x = [0, 1, 2, 3, 4, 5]
y = [0.0000,-0.0001,0.4325,1.2711,0.2162,0.0988]
x_gap = 0.2

# Dashed line
x_dash = np.empty(len_arr * 2)
for idx in range(len_arr):
    x_dash[idx * 2 + 0] = x[idx] - x_gap
    x_dash[idx * 2 + 1] = x[idx] + x_gap
y_dash = np.repeat(y, 2)

# Streched marker
x_strech = np.empty(len_arr * 3)
for idx in range(len_arr):
    x_strech[idx * 3 + 0] = x[idx] - x_gap
    x_strech[idx * 3 + 1] = x[idx] + x_gap
    x_strech[idx * 3 + 2] = None
y_strech = np.repeat(y, 3)

# Plot
fig = plt.figure(figsize=(8, 6))
plt.plot(x_dash, y_dash, linestyle = '--', linewidth = 1, c = 'orange')
plt.plot(x_strech, y_strech, linestyle = '-', linewidth = 3, c = 'orange')

The result is as follows. enter image description here

J. Choi
  • 1,616
  • 12
  • 23