Fortunato
Fortunato

Reputation: 567

Matplotlib: how to change a line's width without changing its length

I am trying to display a series of events that take place sequentially. The default linewidth makes the plots difficult to see. However, if I increase the linewidth, the length of the lines also changes. Is there anyway to increase the width of the lines only?

e.g.

import matplotlib.pyplot as plt

fig, ax = plt.subplots(1,2, figsize=(15,6))
ax[0].set_title('linewidth = 1.0', fontsize=12)
ax[0].plot([3.0, 3.0], [55924, 56926], linewidth=1.)
ax[0].plot([3.0, 3.0], [57167, 58225], linewidth=1.)
ax[0].plot([2.5, 3.5], [56926,56926], linewidth=1., color='black')
ax[0].plot([2.5, 3.5], [57167,57167], linewidth=1., color='black')

ax[1].set_title('linewidth = 30.0', fontsize=12)
ax[1].plot([3.0, 3.0], [55924, 56926], linewidth=30.)
ax[1].plot([3.0, 3.0], [57167, 58225], linewidth=30.)
ax[1].plot([2.5, 3.5], [56926,56926], linewidth=1., color='black')
ax[1].plot([2.5, 3.5], [57167,57167], linewidth=1., color='black')

enter image description here

Upvotes: 1

Views: 5008

Answers (2)

swatchai
swatchai

Reputation: 18812

You need to specify solid_capstyle='butt' in the plot() function.

ax[1].plot([3.0, 3.0], [55924, 56926], linewidth=30., solid_capstyle='butt')
ax[1].plot([3.0, 3.0], [57167, 58225], linewidth=30., solid_capstyle='butt')

For line's join style and cap style, consult this document.

Upvotes: 3

steven
steven

Reputation: 2519

Try Axes.vlines instead

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(5, 5))
ax.set_title('linewidth = 30.0', fontsize=12)
# vlines
ax.vlines(x=3, ymin=55924, ymax=56926, linewidth=30)
ax.vlines(x=3, ymin=57167, ymax=58225, linewidth=30)
# hlines
ax.hlines(y=56926, xmin=2.5, xmax=3.5, linewidth=1., color='black')
ax.hlines(y=57167, xmin=2.5, xmax=3.5, linewidth=1., color='black')

enter image description here

Upvotes: 2

Related Questions