Python Matplotlib - 如何在 y 轴上指定值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21393802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:44:26  来源:igfitidea点击:

Python Matplotlib - how to specify values on y axis?

pythonpython-2.7matplotlibplot

提问by Crista23

I am new to Python and I need to generate a graph using pyplot and matplotlib like the one in the attached picture. So far I tried it like this:

我是 Python 新手,我需要使用 pyplot 和 matplotlib 生成一个图表,就像附图中的那个一样。到目前为止,我是这样尝试的:

 import matplotlib.pyplot as plt
 import numpy as np

 x = np.array([0,1,2,3])
 y = np.array([20,21,22,23])
 my_xticks = ['John','Arnold','Mavis','Matt']
 plt.xticks(x, my_xticks)
 plt.plot(x, y)
 plt.show()

But my problem is how can I specify a different number of values on the y axis different from the number of values on the x axis? And maybe specify them as an interval with 0.005 difference instead of a list? Many thanks! enter image description here

但我的问题是如何在 y 轴上指定不同数量的值与 x 轴上的值数量不同?并且可能将它们指定为具有 0.005 差异的间隔而不是列表?非常感谢!在此处输入图片说明

采纳答案by M4rtini

import matplotlib.pyplot as plt
import numpy as np

x = np.array([0,1,2,3])
y = np.array([0.650, 0.660, 0.675, 0.685])
my_xticks = ['a', 'b', 'c', 'd']
plt.xticks(x, my_xticks)
plt.yticks(np.arange(y.min(), y.max(), 0.005))
plt.plot(x, y)
plt.grid(axis='y', linestyle='-')
plt.show()

Something like this should work.

像这样的事情应该有效。