在 matplotlib 图中设置轴限制 [英] Set axis limits in matplotlib graph

查看:60
本文介绍了在 matplotlib 图中设置轴限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有五个周期的数据及其概率,我想用20000次模拟它使用泊松分布,我希望 x 轴从 0-1 开始,我尝试过,但我在这里保存了我的代码:还有什么方法比我做的容易吗?

I have a five period of data with their probabilities and I would like to simulate it with 20000 times with the Poisson distribution, I want the x-axis start from 0-1, I tried but I stock with that my code here: And is there any way to code it easy than what I did

import numpy
import matplotlib.pyplot
import pylab
import numpy as np
from pylab import *


data = []
inventory = 0

for j in range(4000):
    totalOrder= 0
    totalProduction = 0
    for i in range (5):

        # calculate order
        mean = 105
        order = np.random.gamma(mean, 20000)
        totalOrder = totalOrder+order


        # calculate production
        production = numpy.random.choice([80, 100, 130, 150], p = [0.2, 0.50 ,0.20, 0.1])

        totalProduction = totalProduction + production

        # calcculate new inventory
        inventory = inventory + production - order
        if inventory < 0:
            inventory = 0

    # calculate fill rate for last 5 orders
    fr = float(totalProduction) / float(totalOrder)
    if fr > 1:
        fr = 1

    # append FR to dataset
    data.append(fr)


grid(True)
xlabel('Fill Rate')
ylabel('Density')
title('Simulate the system for 20000 week')
matplotlib.pyplot.hist(data, normed = True)
pylab.show()

推荐答案

你会想要使用 matplotlib 的 轴的set_xlim 方法.看看他们的一些示例,例如这个,以更好地了解您可以使用该模块做什么.

You'll want to use matplotlib's set_xlim method for the axes. Have a look at some of their examples, this one for example, to understand better what you can do with the module.

对于您的代码,您需要添加:

For your code, you'll need to add:

ax = plt.gca()
ax.set_xlim(0,1)

对于一些优化,我看到您在不同的别名下添加了相同的模块(例如,您导入了numpy及其别名np,也由pylab导入了).它告诉我您还没有使用该语言的丰富经验.随着学习的不断深入,最终您将把所有这些进口减少到少量,例如

As for some optimizations, I see that you're adding the same module just under different aliases (e.g., you have numpy and its alias np imported, which are also imported by pylab). It tells me you don't have a lot of experience with the language yet. As you continue to learn, you'll eventually reduce all those imports to just a few, e.g.

import numpy as np
import matplotlib.pyplot as plt

,您将调用属于这些名称空间的正确函数(例如, plt.show(),而不是 pylab.show -pylab仅仅是一个瘦的覆盖 numpy 和 matplotlib 包.

and you'll call the correct functions belonging to these namespaces (e.g. plt.show(), rather than pylab.show - pylab is no more than a thin veil over a package of numpy and matplotlib.

您还可以对代码进行其他一些优化(例如,对循环进行矢量化处理),但是鉴于您当前的水平,我认为这会使它过于复杂.此外,循环确实使您在做什么更明确.

There are a few more optimizations you could make to your code (e.g. vectorizing the loop), but given your current level, I think this would make it too complex. Besides, the loop does make it more explicit what you're doing.

也许只是一个提示:在python中,当你想更新一个数字变量(int、float、...)时,你可以这样做:

Maybe just one tip: in python, when you want to update a variable that is numeric (int, float, ...), you could just do:

inventory += production - order

它可以让您免于再次输入 inventory,从而减少将来在您想要更改代码时出错的机会.

It saves you from typing inventory again and thus less chances of errors in the future if you want to make changes to your code.

这篇关于在 matplotlib 图中设置轴限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆