Reputation: 45
I'll have only one record in my 1st List and count of that record in my 2nd List. I need to put them in a horizontal bar graph. Value of List 1 in Y axis and value of List 2 as a label of the bar. The below code is giving me the bar whose width I can't change. How I can change the width? It's looking awkward with this large width. If not in matplotlib, can i use some other python library to achieve this?
import os
import cx_Oracle
import matplotlib.pyplot as plt
r_c = [Sales_report_2020]
a_i = [1311]
# Figure Size
fig, ax = plt.subplots(figsize =(12, 8))
#replace the None with integer 0
b = [0 if x is None else x for x in a_i]
print(b)
# Horizontal Bar Plot
plt.barh(r_c,b,height=0.5)
plt.ylim(-0.5, len(b) - 0.5)
ax.tick_params(width=0.5,length=6)
# Add annotation to bars
for i in ax.patches:
plt.text(i.get_width()+0.2, i.get_y()+0.27,
"{:1.0f}".format(i.get_width()),
fontsize = 10, fontweight ='bold',
color ='grey') #str(round((i.get_width()), 2))
# Add Plot Title
ax.set_title('DTP!',
loc ='center', )
# Add Text watermark
fig.text(0.9, 0.15, 'STPREPORT', fontsize = 12,
color ='grey', ha ='right', va ='bottom',
alpha = 0.7)
path = r"\\ssd.COM\View\Folder_Redirect\id\Documents\Conda_Envs"
os.chdir(path)
plt.savefig(path + '\BAR.png')
# Show Plot
plt.show()
conn.close()
Upvotes: 2
Views: 15243
Reputation: 1595
The width
and height
parameters in barh
can help you a little bit, but since there is only one record, matplotlib
is probably adjusting the plot automatically to make it look bigger than you need. Adjusting your ylim
should help you. I can't execute your code because I don't have the data, but the following illustrates what I mean:
For example,
plt.barh(1311, width=0.01, height=0.01)
gives you
while
plt.barh(1311, width=20, height=20)
gives you
But, if you set a better ylim
, you can get a better-looking horizontal bar.
plt.barh(1311, width=0.5, height=20)
plt.ylim([1200, 1400])
Of course, you would edit these values to get your desired result.
Upvotes: 4