English 中文(简体)
现有试射轴心
原标题:Give existing matplotlib axes object kwargs
  • 时间:2020-04-23 01:49:46
  •  标签:
  • matplotlib

我愿向一位现成的轴心提出关键词词。 这是可能的吗?

ie something like

import matplotlib.pyplot as plt

kwargs = { xlim :(0, 2),  ylabel : y }

fig, ax = plt.subplots()
ax.give_kwargs(**kwargs)

页: 1 Axes 类别有**kwargs论据,但(一) 只有在物体初始化期间,而不是在物体已经存在之后,方可提供。

Edit:

避免X Y problem。 这里是想做类似事情的背景。

What I find myself doing a lot is creating functions that plot some generalized data and I have to add 5+ extra arguments to handle all of the "set" methods for the axes:

def fancy_plot(data_arg1, dataarg2, xlim=None, ylabel=None):

    fig, ax = plt.subplots()
    ax.plot(data[data_arg1], data[data_arg2])

    if xlim: ax.set_xlim(xlim)
    if ylabel: ax.set_ylabel(ylabel)

最佳回答

是的,我们可以将<条码>kwargs dictionnary 和一套方法和论点。

import matplotlib.pyplot as plt

kwargs = { set_xlim : (0, 2),  set_ylabel :  y }

fig, ax = plt.subplots()
for (method, arguments) in kwargs.items():
    getattr(ax, method)(*arguments)

或者,如果所有方法都沿用<代码>_~thing。 命名公约:

import matplotlib.pyplot as plt

kwargs = { xlim : (0, 2),  ylabel :  y }

fig, ax = plt.subplots()
for (method, arguments) in kwargs.items():
    getattr(ax, f"set_{method}")(*arguments)

You can then wrap the getattr part in a try-except in case your dictionnary contains names that are not existing ax methods.

for (method, arguments) in kwargs.items():
    try:
        getattr(ax, f"set_{method}")(*arguments)
    except AttributeError:
        print("Please everyone panic.")
问题回答

暂无回答




相关问题
Interactive mode in matplotlib

I want to dynamically update the scatter plot based on the y-axis data received from a socket connection. I used python matplot lib in interactive mode to do this, but during dynamic updation if i ...

Couple of matplotlib newbie doubts

I am just starting to use matplotlib and I have hit upon 2 major roadblocks, which I can t seem to work around from the docs/examples,etc: Here is Python source: #!/usr/bin/python import matplotlib ...

How to disable screen update in matplotlib

I have a loop that is adding a line to a plot on each iteration. Right now this is horribly slow as it seems to redraw the the whole graph each time. Is it possible to disable screen updates for a ...

How to use Matplotlib in Django?

From some examples from the Internet I made the test code below. It works! ... BUT if I reload the page, the pie will draw itself with the same image. Some parts get darker every time I reload the ...

Plotting vector fields in python (matplotlib)

I found this code on http://matplotlib.sourceforge.net/examples/pylab_examples/quiver_demo.html from pylab import * from numpy import ma X,Y = meshgrid( arange(0,2*pi,.2),arange(0,2*pi,.2) ) U = cos(...

Multiple grids on matplotlib

I m making plots in Python and matplotlib, which I found huge and flexible, till now. The only thing I couldn t find how to do, is to make my plot have multiple grids. I ve looked into the ...

热门标签