Chart barh matplotlib - overlap bars

2024/11/20 11:32:09

I'm new user of matplotlib and I have a problem with chart barh: overlap bars. When plot the graph, the bars draws overlapped and I haven't found the reason. In my opinion the problem is on re-size the graph. I re-size it, because in the future I will insert title, legend and x,y description. I try some solution, but I have one solution!! This is my code:

    import matplotlib.pyplot as pltimport matplotlib.font_manager as fmfrom matplotlib.colors import LightSourcefrom matplotlib.artist import Artistimport numpy as npfrom decimal import *import datetime#Sizewidth = 360height = 240dpi = 80.0colors = ['#be1e2d','#666699','#92d5ea','#ee8310','#8d10ee','#5a3b16','#26a4ed','#f45a90','#e9e744']#Datacolumns = ['2005','2006']data = [[2.6,3.5],[2, 1.5]]linewidth = 1N = len(columns)ind = np.arange(N)  #Re-Sizerdata = len(data) if columns is None else len(columns)heightColumn = height*1.0 / (rdata) / (len(columns))heightColumn = heightColumn/dpifig = plt.figure(1, figsize=(width/dpi,height/dpi),facecolor='w')ax = fig.add_axes([0.2, 0.3, 0.6, 0.5])#DrawtupleRects = ()idxColor = 0 valPositionCol = indfor dat in data:rects = plt.barh(valPositionCol, dat, heightColumn, color=colors[idxColor], alpha=0.8,  linewidth=linewidth)valPositionCol=valPositionCol+heightColumnidxColor += 1if idxColor==9:idxColor = 0tupleRects += (rects,)plt.show()

THANKS


The code is the same, but I change the data (columns[] e data[]):

import matplotlib.pyplot as plt
import matplotlib.font_manager as fmfrom matplotlib.colors import LightSource
import numpy as np
from decimal import *
import datetime
#Size
width = 360
height = 240
dpi = 80.0
colors = ['#be1e2d','#666699','#92d5ea','#ee8310','#8d10ee','#5a3b16','#26a4ed','#f45a90','#e9e744']
#Data
columns = ['2005','2006']
data = [[1.5, 1.5], [1.5,1.5], [1.5,1.5]]
linewidth = 1
N = len(columns)ind = np.arange(N)  
#Re-Size
height_of_group = .9heightColumn = height_of_group / (len(columns))
fig = plt.figure(1, figsize=(width/dpi,height/dpi),facecolor='w')
ax = fig.add_axes([0.2, 0.3, 0.6, 0.5])
#DrawtupleRects = ()
idxColor = 0 
valPositionCol = ind
for dat in data:rects = plt.barh(valPositionCol, dat, heightColumn, color=colors[idxColor], alpha=0.8,  linewidth=linewidth)valPositionCol=valPositionCol+heightColumnidxColor += 1if idxColor==9:idxColor = 0tupleRects += (rects,)
plt.show()

The problem is that I have variable data and I have to find a stable algorithm.

Answer

I think you are mis-understanding the meaning of height (doc). The units are in axis units, not pixels.

heightColumn = height*1.0 / (rdata) / (len(columns))
heightColumn = heightColumn/dpi

to

height_of_group = .9
heightColumn = height_of_group / (len(data))
#heightColumn = heightColumn/dpi

will get non-overlapping bars with just a bit of extra space between the groups. You can adjust the space between the groups by making height_of_group larger or smaller so long as it is less than 1.

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

Related Q&A

Python django image upload is not working

Python Django image upload is not working when I am adding data and click on submit button not give a any response and also not upload any data in the database.models.py fisrst of all i add model file.…

Android bluetooth send message working first time only

I need to send string message from Raspberry PI to Android device. I am getting message first time only. After that it does not work at all. I am using PYTHON code in Raspberry PI. After first time, it…

Scraping data from href

I was trying to get the postcodes for DFS, for that i tried getting the href for each shop and then click on it, the next page has shop location from which i can get the postal code, but i am able to g…

Numpy - how to sort an array of value/key pairs in descending order

I was looking at the problem Fastest way to rank items with multiple values and weightings and came up with the following solution, but with two remaining issues:import numpy as np# set up values keys …

How to extract certain under specific condition in pandas? (Sentimental analysis)

The picture is what my dataframe looks like. I have user_name, movie_name and time column. I want to extract only rows that are first day of certain movie. For example, if movie as first date in the ti…

Flask app.run method does not work with WinPython 3.11.1 and next.js application: fetch failed

When using WinPython 3.10.5 I am able to debug my flask & next.js application using the flask debug mode (to enable hot reloads): app.run(debug=True, host=host, port=port)However, when using WinPyt…

Pythonic way to assign global administrator roles for Azure Active Directory

What specifically needs to be changed in the Python 3 code below in order to successfully assign the Global Administrator role for an Azure Active Directory Tenant to a given service principal? We tri…

Pandas calculating age from a date

I really need help with this one. My previous post was very bad and unclear - Im sorry - I wish I could delete but hopefully this one will be better.I need to calculate the age based off of a date (se…

Create new folders within multiple existing folders with python

I am looking for a way to create new folders within multiple existing folders. For example I have folders a,b,c.. etc and I want to create a new folder inside each of these existing folders and name th…

extract a column from text file

I have a a text file (huge amount of float numbers) with 25 columns. I want to extract column 14 and divide it by column 15. I could not extract this two columns. Codes:with open(sample for north.txt) …