Plot Background Colour in Gradient

Gradient color background on matplotlib polar plot

You can use ax.pcolormesh to achieve that. Note that in the following example I have applied a cyclic color map and colors defines the the way color is going to be mapped to the colormap.

import numpy as np
import matplotlib.pyplot as ply
import matplotlib.cm as cm

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
r = [2, 1.5, 4, 3, 0.5, 2.5]
theta = [(2*np.pi)/6*i for i in range(6)]
ax.set_theta_zero_location("N") # Put 0 at the top
ax.set_rticks([])
ax.set_thetagrids([i*180/np.pi for i in theta[:-1]])
ax.set_theta_direction(-1) # Make angles go clockwise
# ax.set_xticklabels(['one', 'two', 'three', 'four', 'five', 'six'])
N = 500j
rr, tt = np.mgrid[0:4:N, 0:2*np.pi:N]
colors = rr / rr.max()
ax.pcolormesh(tt, rr, colors, cmap=cm.hsv, shading="nearest")
ax.plot(theta, r, 'k')

Sample Image

Subplot background gradient color

This doesn't use the axisbg parameter, but may do what you want.

There's a matplotlib example for gradients: http://matplotlib.sourceforge.net/examples/pylab_examples/gradient_bar.html.
I tried it myself, this simplified version gives me a green-white gradient background (for some reason when doing this in the interactive python shell I need to call draw() in order for the image to show up):

import matplotlib.pyplot as mplt  
fig = mplt.figure()
ax = fig.add_subplot(111)
mplt.plot([1,2,3],[1,2,1])
plotlim = mplt.xlim() + mplt.ylim()
ax.imshow([[0,0],[1,1]], cmap=mplt.cm.Greens, interpolation='bicubic', extent=plotlim)
mplt.draw()

Pick another colormap for different gradients.
Works without 'bicubic' interpolation too, but it's uglier then.

How to make the background color of the chart as gradient using Chart.js

You can use the canvas "createLinearGradient" method.

Docs:
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createLinearGradient,
https://developer.mozilla.org/en-US/docs/Web/API/CanvasGradient/addColorStop

Example: https://codepen.io/alexgill/pen/MWbjXOP

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var gradient = ctx.createLinearGradient(0, 0, 0, 400);
gradient.addColorStop(0, 'rgba(10,10,10,.2)');
gradient.addColorStop(1, 'rgba(255,255,255,1)');

const data = {
labels: ["day1", "day2", "day3", "day4", "day5", "day6"],
datasets: [
{
label: "Your BMI",
data: [28.3, 28, 27, 27.6, 25, 25.6],
backgroundColor : gradient,
borderColor: "rgba(152,222,217,0.2)"
}
]
};

DotPlot with Gradient Background in Python?

You can draw a simple background image.

To get started:

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

names = ['A', 'B', 'C', 'D', 'E', 'F']
values = [0, 1, 2, 3, 1, 2]
n = len(names)
gradient = np.linspace(0, 1, 100).reshape(1, -1)
plt.imshow(gradient , extent=[-0.25, 3.25, -1, n], aspect='auto', cmap='RdYlGn_r')
sns.stripplot(x=values, y=names, color='b', size=12, edgecolor='b')
plt.hlines(np.arange(0, n), -0.25, 3.25, linestyles='--', linewidth=1)
plt.grid(False)
plt.xlim(-0.25, 3.25)
plt.xticks([0, 1, 2, 3])

Sample Image

How to colour MatPlotLib graph with gradient colour

Take a look at the gradient_bar.py example from the matplotlib documentation.

The basic idea is that you don't use the hist() method from pyplot, but build the barchart yourself by using imshow() instead. The first argument to imshow() contains the color map which will be displayed inside of the box specified by the extent argmument.

Here's a simplified version of the example cited above that should get you on the track. It uses the values from your Excel example, and a color map that uses the CSS colors 'dodgerblue' and 'royalblue' for a linear gradient.

from matplotlib import pyplot as plt
from matplotlib import colors as mcolors

values = [22, 15, 14, 10, 7, 5, 4, 3, 3, 2, 2, 1, 1, 1, 1, 7]

# set up xlim and ylim for the plot axes:
ax = plt.gca()
ax.set_xlim(0, len(values))
ax.set_ylim(0, max(values))

# Define start and end color as RGB values. The names are standard CSS color
# codes.
start_color = mcolors.hex2color(mcolors.cnames["dodgerblue"])
end_color = mcolors.hex2color(mcolors.cnames["royalblue"])

# color map:
img = [[start_color], [end_color]]

for x, y in enumerate(values):
# draw an 'image' using the color map at the
# given coordinates
ax.imshow(img, extent=(x, x + 1, 0, y))

plt.show()


Related Topics



Leave a reply



Submit