官术网_书友最值得收藏!

Different Matplotlib styles

In this section, we will be learning about various styles provided by Matplotlib such as temporary styles or creating your own custom styles. The following are a few examples of different styles:

# Temporary styles
plt.style.use('classic')
from scipy.ndimage.filters import gaussian_filter
plt.subplot(221)
plt.plot(np.arange(0,1,0.01), np.power(np.arange(0,1,0.01), 3))
plt.ylabel('Axis Label')
plt.subplot(222)
with plt.style.context('ggplot'):
plt.scatter(np.random.normal(size=10), np.random.normal(size=10), c=np.random.normal(size=10))
plt.subplot(223)
plt.hist(np.random.normal(size=1000));
plt.hist(np.random.normal(1, size=1000));
plt.hist(np.random.normal(2, size=500));
plt.ylabel('Axis Label')
plt.xlabel('Axis Label')
plt.subplot(224)
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))
plt.xlabel('Axis Label')

Using the dark background will give you an image that shows up nicely on a dark background, hence if you're building slides, you might want to use the dark background style sheet:

# Custom styles
plt.style.use('bigpoints')
from scipy.ndimage.filters import gaussian_filter
plt.subplot(221)
plt.plot(np.arange(0,1,0.01), np.power(np.arange(0,1,0.01), 3), 'ko')
plt.ylabel('Axis Label')
plt.subplot(222)
plt.scatter(np.random.normal(size=10), np.random.normal(size=10), c=np.random.normal(size=10))
plt.subplot(223)
plt.hist(np.random.normal(size=1000));
plt.hist(np.random.normal(1, size=1000));
plt.hist(np.random.normal(2, size=500));
plt.ylabel('Axis Label')
plt.xlabel('Axis Label')
plt.subplot(224)
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))
plt.xlabel('Axis Label')

You will see the output, which shows the black background:

You can also choose style sheets temporarily. So, previously, we have chose a style sheet that affects all four plots. If, for example, I take my scatter plot and put it in a with plt.style.context block and choose a style sheet, let's say ggplot, you can see that we have actually overridden it, as shown here:

# Temporary styles
plt.style.use('classic')
from scipy.ndimage.filters import gaussian_filter
plt.subplot(221)
plt.plot(np.arange(0,1,0.01), np.power(np.arange(0,1,0.01), 3))
plt.ylabel('Axis Label')
plt.subplot(222)
with plt.style.context('ggplot'):
plt.scatter(np.random.normal(size=10), np.random.normal(size=10), c=np.random.normal(size=10))
plt.subplot(223)
plt.hist(np.random.normal(size=1000));
plt.hist(np.random.normal(1, size=1000));
plt.hist(np.random.normal(2, size=500));
plt.ylabel('Axis Label')
plt.xlabel('Axis Label')
plt.subplot(224)
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))
plt.xlabel('Axis Label')

From the preceding code, we can see there's a small difference, but the color map has changed:

So, this one scatter plot has temporarily used a different set of choices for the appearance compared to the previous ones, so the other panels here are using the classic style sheet, whereas this is using ggplot, which changes the attributes of these points.

主站蜘蛛池模板: 百色市| 鲜城| 任丘市| 夹江县| 邵阳市| 荔波县| 乌拉特中旗| 南江县| 达孜县| 义乌市| 白玉县| 华宁县| 兴宁市| 区。| 青川县| 霍邱县| 衡阳市| 黑龙江省| 柳林县| 且末县| 宁武县| 赤水市| 栾川县| 彩票| 武陟县| 左贡县| 阿荣旗| 田林县| 楚雄市| 包头市| 安龙县| 枣强县| 新乡市| 天等县| 丽江市| 元阳县| 昌都县| 同德县| 新绛县| 新建县| 东平县|