Matplotlib Plot Lines Above Each Bar

2024/10/3 21:17:01

I would like to plot a horizontal line above each bar in this chart. The y-axis location of each bar depends on the variable 'target.' I want to use axhline, if possible, or Line2D because I need to be able to modify the line style, color, length, and width.

import matplotlib.pyplot as plt
plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')#Here are the targets that I want to use 
#to plot horizontal lines above each bar...
targets = (6,6,8,6,9)ind = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))plt.bar(ind, performance, align='center')
plt.xticks(ind, people)plt.show()

Thanks in advance!

Answer

You need to provide the x_start and x_end to .hlines(). They can be numpy.arrays, in which case each element determines the start/end point of each line segment:

import matplotlib.pyplot as plt
plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')#Here are the targets that I want to use 
#to plot horizontal lines above each bar...
targets = (6,6,8,6,9)ind = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))Bars = plt.bar(ind, performance, align='center')
_ = plt.xticks(ind, people)#if you want your hlines to align with the bars.
#i.e. start and end at the same x coordinates:x_start = np.array([plt.getp(item, 'x') for item in Bars])
x_end   = x_start+[plt.getp(item, 'width') for item in Bars]plt.hlines(targets, x_start, x_end)

enter image description here

https://en.xdnf.cn/q/70680.html

Related Q&A

Flask-SQLAlchemy Lower Case Index - skipping functional, not supported by SQLAlchemy reflection

First off. Apologies if this has been answered but I can not find the answer any where.I need to define a lowercase index on a Flask-SQLAlchemy object.The problem I have is I need a models username and…

pip listing global packages in active virtualenv

After upgrading pip from 1.4.x to 1.5 pip freeze outputs a list of my globally installed (system) packages instead of the ones installed inside of my virtualenv. Ive tried downgrading to 1.4 again but …

Scrapy Crawler in python cannot follow links?

I wrote a crawler in python using the scrapy tool of python. The following is the python code:from scrapy.contrib.spiders import CrawlSpider, Rule from scrapy.contrib.linkextractors.sgml import SgmlLin…

Remove commas in a string, surrounded by a comma and double quotes / Python

Ive found some similar themes on stackoverflow, but Im newbie to Python and Reg Exps.I have a string,"Completely renovated in 2009, the 2-star Superior Hotel Ibis BerlinMesse, with its 168 air-con…

I need help making a discord py temp mute command in discord py

I got my discord bot to have a mute command but you have to unmute the user yourself at a later time, I want to have another command called "tempmute" that mutes a member for a certain number…

How to clip polar plot in pylab/pyplot

I have a polar plot where theta varies from 0 to pi/2, so the whole plot lies in the first quater, like this:%pylab inline X=linspace(0,pi/2) polar(X,cos(6*X)**2)(source: schurov.com) Is it possible b…

Cython and c++ class constructors

Can someone suggest a way to manipulate c++ objects with Cython, when the c++ instance of one class is expected to feed the constructor of another wrapped class as described below? Please look at th…

How to share state when using concurrent futures

I am aware using the traditional multiprocessing library I can declare a value and share the state between processes. https://docs.python.org/3/library/multiprocessing.html?highlight=multiprocessing#s…

Does IronPython implement python standard library?

I tried IronPython some time ago and it seemed that it implements only python language, and uses .NET for libraries. Is this still the case? Can one use python modules from IronPython?

finding the last occurrence of an item in a list python

I wish to find the last occurrence of an item x in sequence s, or to return None if there is none and the position of the first item is equal to 0This is what I currently have:def PositionLast (x,s):co…