import module from string variable

The __import__ function can be a bit hard to understand. If you change i = __import__(‘matplotlib.text’) to i = __import__(‘matplotlib.text’, fromlist=[”]) then i will refer to matplotlib.text. In Python 2.7 and Python 3.1 or later, you can use importlib: import importlib i = importlib.import_module(“matplotlib.text”) Some notes If you’re trying to import something from a sub-folder … Read more

Modify tick label text

Caveat: Unless the ticklabels are already set to a string (as is usually the case in e.g. a boxplot), this will not work with any version of matplotlib newer than 1.1.0. If you’re working from the current github master, this won’t work. I’m not sure what the problem is yet… It may be an unintended … Read more

How can I convert an RGB image into grayscale in Python?

How about doing it with Pillow: from PIL import Image img = Image.open(‘image.png’).convert(‘L’) img.save(‘greyscale.png’) If an alpha (transparency) channel is present in the input image and should be preserved, use mode LA: img = Image.open(‘image.png’).convert(‘LA’) Using matplotlib and the formula Y’ = 0.2989 R + 0.5870 G + 0.1140 B you could do: import numpy … Read more

Save plot to image file instead of displaying it using Matplotlib

While the question has been answered, I’d like to add some useful tips when using matplotlib.pyplot.savefig. The file format can be specified by the extension: from matplotlib import pyplot as plt plt.savefig(‘foo.png’) plt.savefig(‘foo.pdf’) Will give a rasterized or vectorized output respectively, both which could be useful. In addition, there’s often an undesirable, whitespace around the … Read more

How to update a plot in matplotlib

You essentially have two options: Do exactly what you’re currently doing, but call graph1.clear() and graph2.clear() before replotting the data. This is the slowest, but most simplest and most robust option. Instead of replotting, you can just update the data of the plot objects. You’ll need to make some changes in your code, but this … Read more

How to add hovering annotations in matplotlib

It seems none of the other answers here actually answer the question. So here is a code that uses a scatter and shows an annotation upon hovering over the scatter points. import matplotlib.pyplot as plt import numpy as np; np.random.seed(1) x = np.random.rand(15) y = np.random.rand(15) names = np.array(list(“ABCDEFGHIJKLMNO”)) c = np.random.randint(1,5,size=15) norm = plt.Normalize(1,4) … Read more

prevent scientific notation in matplotlib.pyplot [duplicate]

In your case, you’re actually wanting to disable the offset. Using scientific notation is a separate setting from showing things in terms of an offset value. However, ax.ticklabel_format(useOffset=False) should have worked (though you’ve listed it as one of the things that didn’t). For example: fig, ax = plt.subplots() ax.plot(range(2003,2012,1),range(200300,201200,100)) ax.ticklabel_format(useOffset=False) plt.show() If you want to … Read more