Last modified: 18 December 2023

Adding a line to a plotĀ [New]


Matplotlib has a number of ways to add lines toa plot. For this example we shall use a simple histogrammed dataset

sherpa> load_arrays(1, [1, 5, 8], [5, 7, 10], [20, 30, 25])
sherpa> prefs = get_plot_prefs("data")
sherpa> prefs["linestyle"] = "solid"
sherpa> prefs["yerrorbars"] = False
sherpa> plot_data()  

Figure 1: the original plot

[The plot contains three bins, where the first two are connected but the third is separate.]
[Print media version: The plot contains three bins, where the first two are connected but the third is separate.]

Figure 1: the original plot

A number of Matlpotlib commands can be used to add lines to a plot.

sherpa> plot_data()

For example, horizontal and vertical lines can be added that cover the whole plot:

sherpa> plt.axhline(22, c="g", alpha=0.5)
sherpa> plt.axvline(7.5, c="k")

A line that crosses two points can also be added:

sherpa> plt.axline((2, 21), (10, 29), ls="dotted", lw=2)

Figure 2: adding lines

[The plot now has three extra lines: a horizontal opaque green line at y=22 covering the whole X axis; a vertical black line at x=7.5 covering the whole Y axis; and a dotted blue line that passes through (2,21) and (10,29) but extends to the edges of the plot.]
[Print media version: The plot now has three extra lines: a horizontal opaque green line at y=22 covering the whole X axis; a vertical black line at x=7.5 covering the whole Y axis; and a dotted blue line that passes through (2,21) and (10,29) but extends to the edges of the plot.]

Figure 2: adding lines