0

I'm just started learning and I got a little problem with my code I need yMin start from 60 and yMax=100, but why my results like this?

enter image description here

Can someone please help me?

this my code

import matplotlib.pyplot as plt
import numpy as np


data = [
["{:.2}".format(0.80443), "{:.2}".format(0.84176), "{:.2}".format(0.84278), "{:.2}".format(0.82316),"{:.2}".format(0.82260)],
["{:.2}".format(0.71956), "{:.2}".format(0.77691), "{:.2}".format(0.77279), "{:.2}".format(0.74522),"{:.2}".format(0.74747)],
["{:.2}".format(0.84256), "{:.2}".format(0.83268), "{:.2}".format(0.84152), "{:.2}".format(0.84204),"{:.2}".format(0.83775)],
["{:.2}".format(0.71956), "{:.2}".format(0.77691), "{:.2}".format(0.77279), "{:.2}".format(0.74522),"{:.2}".format(0.74747)],
["{:.2}".format(0.80320), "{:.2}".format(0.83787), "{:.2}".format(0.83933), "{:.2}".format(0.82087),"{:.2}".format(0.82008)],
["{:.2}".format(0.71956), "{:.2}".format(0.77043), "{:.2}".format(0.76772), "{:.2}".format(0.74286),"{:.2}".format(0.74432)],
["{:.2}".format(0.83641), "{:.2}".format(0.83009), "{:.2}".format(0.83847), "{:.2}".format(0.83743),"{:.2}".format(0.83333)],
["{:.2}".format(0.71956), "{:.2}".format(0.77043), "{:.2}".format(0.76772), "{:.2}".format(0.74286),"{:.2}".format(0.74432)],
]


year = ['set1', 'set2', 'set3', 'set4', 'set5']
plt.plot(year, data[0])
plt.plot(year, data[1])
plt.plot(year, data[2])
plt.plot(year, data[3])
plt.plot(year, data[4])
plt.plot(year, data[5])
plt.plot(year, data[6])
plt.plot(year, data[7])
plt.xlabel('xx')
plt.ylabel('aa')
plt.title('bb')
plt.show()

If i change my format ex : "{:.2%}".format(0.80443)

so this my results

enter image description here

I want to change the decimal number to percent and round it up to two decimal places. that's why I use format - @bigben

4
  • 2
    Why are you .formatting your data? Commented Mar 19, 2020 at 19:30
  • I want to change the decimal number to percent and round it up to two decimal places.@BigBen Commented Mar 19, 2020 at 19:33
  • But .format() returns a formatted string, which is not what you want. Commented Mar 19, 2020 at 19:35
  • @BigBen i give more pic for my problems Commented Mar 19, 2020 at 19:39

1 Answer 1

1

First, you're going about getting the percentages formatted incorrectly. Those values are strings after you format them, not numbers!

If you have a list like this, just multiply the float values by 100:

a = [0.80443, 0.84176, 0.84278, 0.82316, 0.82260]
data[0] = [ x * 100 for x in a ]

Then when you plot each list, you'll have values of 80.443, 84.176..., etc.

If you want to control the range on the y-axis after multiplying and plotting, set:

plt.ylim([60, 100])

Which limits the y-axis to values between 60 and 100.

EDIT:

To format the ticks on the y-axis, I recommend using the ticker methods in matplotlib:

from matplotlib import pyplot as plt
import matplotlib.ticker as ticker

fig, ax = plt.subplots(figsize=(15, 7))
plt.plot(year, data[0])
plt.ylim([60, 100])
ax.get_yaxis().set_major_formatter(
    ticker.FuncFormatter(lambda x, p: "{:.2%}".format(x))
)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.