0

I want to draw a line & bar chart by matplotlib.my code is like this, you can see data label's format is number in my chart:

import matplotlib.pyplot as plt
from matplotlib import ticker
import numpy as np
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
plt.figure(figsize=(9, 6), dpi=100)
x = ['>12 Months','6-12 Months','3-6 Months','1-3 Months','1-4 Weeks','>1 Times Per Week']
y = [0.0964,0.1607,0.4158,0.3054,0.0215,0.0001]
z = [11544,19247,49794,36572,2578,16]
plt.bar(x=x, height=z, label='Vehicles Num', color='Blue', alpha=0.7, width=0.5)
plt.legend(loc="upper left")
plt.title("Frequency Distribution of Hood Ajar/Closed Events",size=15)
plt.xlabel("Frequency",size=13)
plt.ylabel("Number of Vehicles",size=13)
ax2 = plt.twinx()
ax2.set_ylabel("Percentage",size=13)
ax2.plot(y)
ax2.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=1))
plt.plot(x, y, "r", marker='.', c='r', ms=8, linewidth='1', label="Percentage")
plt.legend(loc="upper right")
for a, b in zip(x, y):
    plt.text(a, b, b, ha='center',va='bottom',fontsize=13)
plt.show()

The result I want: the data label list "y", can show like:9.64%, 16.07%

Many Thanks~

Yount Shi
  • 13
  • 2
  • Try changing the line below `zip` (inside the for loop) to `plt.text(a, b, str(b*100)+'%', ha='center',va='bottom',fontsize=13)` – Redox Feb 06 '23 at 12:53
  • Does this answer your question? [pandas matplotlib labels bars as percentage](https://stackoverflow.com/questions/73674585/pandas-matplotlib-labels-bars-as-percentage) – Redox Feb 07 '23 at 00:52

1 Answers1

0

enter image description hereYou can use below code i have changes may help!!(Mark use full if helps!!!)

import matplotlib.pyplot as plt
from matplotlib import ticker
import numpy as np
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
plt.figure(figsize=(9, 6), dpi=100)
x = ['>12 Months','6-12 Months','3-6 Months','1-3 Months','1-4 Weeks','>1 Times 
Per Week']
y = [0.0964,0.1607,0.4158,0.3054,0.0215,0.0001]
y=np.multiply(y,100)

z = [11544,19247,49794,36572,2578,16]
plt.bar(x=x, height=z, label='Vehicles Num', color='Blue', alpha=0.7, width=0.5)
plt.legend(loc="upper left")
plt.title("Frequency Distribution of Hood Ajar/Closed Events",size=15)
plt.xlabel("Frequency",size=13)
plt.ylabel("Number of Vehicles",size=13)
ax2 = plt.twinx()
ax2.set_ylabel("Percentage",size=13)
ax2.plot(y)
ax2.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=1))
plt.plot(x,y, "r", marker='.', c='r', ms=8, linewidth='1', label="Percentage")
plt.legend(loc="upper right")
for a, b in zip(x, y):
    plt.text(a, b,str(b) +'%', ha='center',va='bottom',fontsize=13)

plt.show()  

Sagar Sah
  • 55
  • 1
  • 8