简体   繁体   English

Matplotlib-更改用作绘图背景的图像的不透明度

[英]Matplotlib - changing opacity of the image used as a background for a plot

I have used matplotlib to set an image background for a plot. 我已经使用matplotlib为绘图设置图像背景。 But the color of the curves gets mixed with the background at some places. 但是曲线的颜色在某些地方与背景混合在一起。 Can anyone help me reduce the opacity of the background, so that the actual plot is more visible. 任何人都可以帮助我减少背景的不透明度,从而使实际图更清晰可见。 my code till this point is -- 到目前为止,我的代码是-

import pandas as pd
import sys, os
import matplotlib.pyplot as plt
import numpy as np
import itertools
from scipy.misc import imread

def flip(items, ncol):
    return itertools.chain(*[items[i::ncol] for i in range(ncol)])

df = pd.read_pickle('neer.pickle')
rows = list(df.index)
countries = ['USA','CHN','JPN','DEU','GBR','FRA','IND','ITA','BRA','CAN','RUS']
x = range(len(rows))
df = df.pct_change()

fig, ax = plt.subplots(1)
for country in countries:
    ax.plot(x, df[country], label=country)

plt.xticks(x, rows, size='small', rotation=75)
#legend = ax.legend(loc='upper left', shadow=True)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.show(1)

df1 = df[countries]
plt.figure(2)
for country in countries:
    my_plot = plt.plot(x, 10*df[country], label=country)
img = imread("world.png")
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.imshow(img,zorder=0,  extent=[0.1, 30.0, -10.0, 10.0])
plt.set_alpha(0.5)
plt.title('Variation of NEER across the world')
plt.show(2)

在此处输入图片说明

您是否尝试过类似的方法:

plt.imshow(img, zorder=0,  extent=[0.1, 30.0, -10.0, 10.0], alpha = 0.6)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM