0

I want to do this Graph (Graph with Vertical Gradient and Ticker Axis values with Background Color):

Graph with Vertical Gradient and Ticker Axis values with Background Color enter image description here

At this moment I can do this:

enter image description here

My problems now are:

  • I have a Gradient but I don't know how to change to orange color, and I don't know how to change gradient values.

My Code is:

import matplotlib.pyplot as plt
from matplotlib.offsetbox import (OffsetImage,AnnotationBbox)
from matplotlib.cbook import get_sample_data
import matplotlib.transforms as transforms
from matplotlib.ticker import (MultipleLocator)
import numpy as np
import matplotlib as mpl

# Control of lines appears in graphic

mpl.rcParams['axes.spines.left'] = True
mpl.rcParams['axes.spines.right'] = True
mpl.rcParams['axes.spines.top'] = False
mpl.rcParams['axes.spines.bottom'] = False


# Sample Data to Show
# create some sample data
x=[0,0,1, 2, 3, 4, 5, 6,7]
y=[0,4,1, 8, 20, 6, 7, 4,0]

fig, ax = plt.subplots()

# Set values of axis
ax.set_xlim(0,max(x))
ax.set_ylim(0,max(y))

# Set color and font size of X Axis
ax.tick_params(axis='x', colors='white')

# Create a transform in Axis VAlues
trans = transforms.blended_transform_factory(ax.transAxes, ax.transAxes)

# Create Rectanagles in percentage of Axis VAlues, and set order
ax.add_patch(plt.Rectangle((-0.04,-0.1),1.08, 0.12,facecolor='black',clip_on=False,linewidth = 0,zorder=1,transform=trans))
ax.add_patch(plt.Rectangle((1.05,-0.1),0.1, 0.12,facecolor='silver',clip_on=False,linewidth = 0,zorder=5,transform=trans))
ax.text(1.06, -0.07, max(x), fontsize = 12,zorder = 6, color = 'k',transform=trans)

# Put pictos in axis
xy = (1, 1)
with get_sample_data(r"C:\Roberto\Visual_Studio_Code\GisBike\pruebas\Grafica\meta.png") as file:
    arr_img = plt.imread(file, format='png')

imagebox = OffsetImage(arr_img, zoom=0.2)
imagebox.image.axes = ax
ab = AnnotationBbox(imagebox, xy, xybox=(1, 1.05), xycoords='data', boxcoords=("axes fraction", "axes fraction"), pad=0.5,frameon=False)
ax.add_artist(ab)

with get_sample_data(r"C:\Roberto\Visual_Studio_Code\GisBike\pruebas\Grafica\sortida.png") as file:
    arr_img = plt.imread(file, format='png')

imagebox = OffsetImage(arr_img, zoom=0.2)
imagebox.image.axes = ax
ab = AnnotationBbox(imagebox, xy, xybox=(0, 1.05), xycoords='data', boxcoords=("axes fraction", "axes fraction"), pad=0,frameon=False)
ax.add_artist(ab)


with get_sample_data(r"C:\Roberto\Visual_Studio_Code\GisBike\pruebas\Grafica\sortida.png") as file:
    arr_img = plt.imread(file, format='png')

imagebox = OffsetImage(arr_img, zoom=0.2)
imagebox.image.axes = ax
ab = AnnotationBbox(imagebox, xy, xybox=(0.5, 1.05), xycoords='data', boxcoords=("axes fraction", "axes fraction"), pad=0,frameon=False,
                    arrowprops=dict(arrowstyle="-",connectionstyle="angle")) # ,angleA=90
ax.add_artist(ab)


# plot only the outline of the polygon, and capture the result
poly, = ax.fill(x, y, facecolor='none')

# get the extent of the axes
xmin, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()

# create a dummy image
img_data = np.arange(xmin,xmax,(xmax-xmin)/100.)
img_data = np.arange(ymin,ymax,(ymax-ymin)/100.)
img_data = img_data.reshape(img_data.size,1)

# plot and clip the image
im = ax.imshow(img_data, aspect='auto', origin='upper', cmap=plt.cm.Reds_r, extent=[xmin,xmax,ymin,ymax],  zorder=0)

im.set_clip_path(poly)

plt.show()
RBenet
  • 171
  • 1
  • 10

1 Answers1

0

Finally I have a solution for my question. Surelly is not the best, but it works. If someone can improve it I will be very gratefully.

The final result is:

Final Result

What I'm doing is take a picture in PNG format with my gradient color background and a texture. I cut this picture with the area of my graph.

For the axes I put a rectangle under the tickers, put in white and I delete 0 value from Y Axis.

Here is my code:

import matplotlib.pyplot as plt
from matplotlib.offsetbox import (OffsetImage,AnnotationBbox)
from matplotlib.cbook import get_sample_data
import matplotlib.transforms as transforms
from matplotlib.ticker import (MultipleLocator)
import numpy as np
import matplotlib as mpl

# Control of lines appears in graphic

mpl.rcParams['axes.spines.left'] = True
mpl.rcParams['axes.spines.right'] = True
mpl.rcParams['axes.spines.top'] = False
mpl.rcParams['axes.spines.bottom'] = False


# Sample Data to Show
# create some sample data
x=[0,0,1, 2,  3, 4, 5, 6, 7, 8, 9, 10,11,12,13,13]
y=[0,4,1, 8, 12, 6, 7, 4, 4, 1, 8, 12, 6, 7, 4, 0] 
# Important Start at Point Y=0 and Finish in Y=o to close the Area. Repeat X in this points

fig, ax = plt.subplots()

# Set values of axis
ax.set_xlim(0,max(x))
ax.set_ylim(0,max(y))

# Set color and font size of X Axis
ax.tick_params(axis='x', colors='white')

# Delete First Y Value
my_yticks = ax.get_yticks()
my_yticks=np.delete(my_yticks,0)
ax.set_yticks(my_yticks)

# Create a transform in Axis VAlues
trans = transforms.blended_transform_factory(ax.transAxes, ax.transAxes)

# Create Rectanagles in percentage of Axis VAlues, and set order
ax.add_patch(plt.Rectangle((-0.04,-0.10),1.08, 0.10,facecolor='black',clip_on=False,linewidth = 0,zorder=1,transform=trans))
ax.add_patch(plt.Rectangle((1.05,-0.10),0.1, 0.10,facecolor='silver',clip_on=False,linewidth = 0,zorder=5,transform=trans))
ax.text(1.06, -0.07, max(x), fontsize = 12,zorder = 6, color = 'k',transform=trans)

# Put pictos in axis
xy = (1, 0)
with get_sample_data(r"C:\Roberto\Visual_Studio_Code\GisBike\pruebas\Grafica\meta.png") as file:
    arr_img = plt.imread(file, format='png')

imagebox = OffsetImage(arr_img, zoom=0.2)
imagebox.image.axes = ax
ab = AnnotationBbox(imagebox, xy, xybox=(1, 1.05), xycoords='data', boxcoords=("axes fraction", "axes fraction"), pad=0.5,frameon=False)
ax.add_artist(ab)

with get_sample_data(r"C:\Roberto\Visual_Studio_Code\GisBike\pruebas\Grafica\sortida.png") as file:
    arr_img = plt.imread(file, format='png')

imagebox = OffsetImage(arr_img, zoom=0.2)
imagebox.image.axes = ax
ab = AnnotationBbox(imagebox, xy, xybox=(0, 1.05), xycoords='data', boxcoords=("axes fraction", "axes fraction"), pad=0,frameon=False)
ax.add_artist(ab)


with get_sample_data(r"C:\Roberto\Visual_Studio_Code\GisBike\pruebas\Grafica\sortida.png") as file:
    arr_img = plt.imread(file, format='png')

imagebox = OffsetImage(arr_img, zoom=0.2)
imagebox.image.axes = ax
ab = AnnotationBbox(imagebox, xy, xybox=(0.5, 1.05), xycoords='data', boxcoords=("axes fraction", "axes fraction"), pad=0,frameon=False,
                    arrowprops=dict(arrowstyle="-",connectionstyle="angle")) # ,angleA=90
ax.add_artist(ab)

# plot only the outline of the polygon, and capture the result
poly, = ax.fill(x, y, facecolor='none')

# get the extent of the axes
xmin, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()

# plot and clip the image
with get_sample_data(r'C:\Roberto\Visual_Studio_Code\GisBike\programa\IMG\Textura.png') as image_file:
    image = plt.imread(image_file)
im = ax.imshow(image, aspect='auto',  extent=[xmin,xmax,ymin,ymax],zorder=0)
im.set_clip_path(poly)

plt.show()

Some ideas I get from this links:

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
RBenet
  • 171
  • 1
  • 10