简体   繁体   中英

How to fix y axis on matplotlib?

For the y-axis the numbers aren't ascending up like how I'd want them. Am I just using the plot function wrong?

x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
y = [120.6519, 121.1300, 120.8700, 120.8700, 120.7350, 120.7540, 121.0900, 121.0100, 121.0900, 121.4800, 121.5810, 121.8700,121.8200, 121.8899, 121.9000, 121.8800', 121.5301, 121.7500, 121.6600] 
plt.plot (x, y)
plt.show()

在此处输入图像描述

Your y values are strings, which will be interpreted very differently by matplotlib , even if they represent numbers. Convert them to floats before plotting:

import matplotlib.pyplot as plt

x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
y = ['120.6519', '121.1300', '120.8700', '120.8700', '120.7350', '120.7540', '121.0900', '121.0100', '121.0900', '121.4800', '121.5810', '121.8700', '121.8200', '121.8899', '121.9000', '121.8800', '121.5301', '121.7500', '121.6600'] 
y = [float(yy) for yy in y]
plt.plot (x, y)
plt.show()

Output:

在此处输入图像描述

You can also use map to convert your list of strings to floats

x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
y = ['120.6519', '121.1300', '120.8700', '120.8700', '120.7350', '120.7540', '121.0900', '121.0100', '121.0900', '121.4800', '121.5810', '121.8700', '121.8200', '121.8899', '121.9000', '121.8800', '121.5301', '121.7500', '121.6600'] 

y = list(map(float, y)) 
plt.plot(x, y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM