[Python Tutorial] create a beautiful bar chart

Source: Internet
Author: User
Matplotlib is an open-source project based on the Python language. it aims to provide a data drawing package for Python. This article briefly introduces how to use this package to draw beautiful bar charts. Matplotlib is an open-source project based on the Python language. it aims to provide a data drawing package for Python. This article briefly introduces how to use this package to draw beautiful bar charts.

Import command

1) set the working environment % cd "F: \ Dropbox \ python" 2) import the package import matplotlib. pyplot as pltimport numpy as npfrom matplotlib. image import BboxImagefrom matplotlib. _ png import read_pngimport matplotlib. colorsfrom matplotlib. cbook import get_sample_dataimport pandas pd3) read data = pd. read_csv ("CAR.csv") 4) define and draw the image class RibbonBox (object): original_image = read_png (get_sample_data ("Minduka_Present_Blue_Pack.png", asfileobj = F Alse) cut_location = 70b_and_h = original_image [:,:, 2] color = original_image [:,:, 2]-original_image [:,:, 0] alpha = original_image [:, :, 3] nx = original_image.shape [1] def _ init _ (self, color): rgb = matplotlib. colors. colorConverter. to_rgb (color) im = np. empty (self. original_image.shape, self. original_image.dtype) im [:,:,: 3] = self. B _and_h [:,:, np. newaxis] im [:,:,: 3]-= self. color [:,:, np. newaxis] * (1. -np. arr Ay (rgb) im [:,:, 3] = self. alphaself. im = imdef get_stretched_image (self, stretch_factor): stretch_factor = max (stretch_factor, 1) ny, nx, nch = self. im. shapeny2 = int (ny * stretch_factor) stretched_image = np. empty (ny2, nx, nch), self. im. dtype) cut = self. im [self. cut_location,:,:] stretched_image [:,:,:] = cutstretched_image [: self. cut_location,:,:] = \ self. im [: self. cut_location,:,:] stretched_image [-(ny-sel F. cut_location):,:,:] = \ self. im [-(ny-self.cut_location):,:,:] self. _ cached_im = stretched_imagereturn stretched_imageclass RibbonBoxImage (BboxImage): zorder = 1def _ init _ (self, bbox, color, cmap = None, norm = None, interpolation = None, origin = None, filternorm = 1, filterrad = 4.0, resample = False, ** kwargs): BboxImage. _ init _ (self, bbox, cmap = cmap, norm = norm, interpolation = interpolation, origin = origin, filte Rnorm = filternorm, filterrad = filterrad, resample = resample, ** kwargs) self. _ ribbonbox = RibbonBox (color) self. _ cached_ny = Nonedef draw (self, renderer, * args, ** kwargs): bbox = self. get_window_extent (renderer) stretch_factor = bbox. height/bbox. widthny = int (stretch_factor * self. _ ribbonbox. nx) if self. _ cached_ny! = Ny: arr = self. _ ribbonbox. get_stretched_image (stretch_factor) self. set_array (arr) self. _ cached_ny = nyBboxImage. draw (self, renderer, * args, ** kwargs) if 1: from matplotlib. transforms import Bbox, TransformedBboxfrom matplotlib. ticker import ScalarFormatterfig, ax = plt. subplots () years = np. arange (0.8) box_colors = [(0.2, 0.2, 0.2), (0.8, 0.2, 0.2, 0.2), (0.8, 0.7, 0.5 ), (0.3, 0.8, 0.7), (0.4, 0.6, 0.3), (0.5, 0.5, 0.1),] heights = data ['price'] fmt = ScalarFormatter (useOffset = False) ax. xaxis. set_major_formatter (fmt) for year, h, bc in zip (years, heights, box_colors): bbox0 = Bbox. from_extents (year-0.4, 0 ., year + 0.4, h) bbox = TransformedBbox (bbox0, ax. transData) rb_patch = RibbonBoxImage (bbox, bc, interpolation = "bicubic") ax. add_artist (rb_patch) ax. annotate (h, (year, h), va = "bottom", ha = "center") ax. set_title ('The Price of cart') patch_gradient = BboxImage (ax. bbox, interpolation = "bicubic", zorder = 0.1,) gradient = np. zeros (2, 2, 4), dtype = np. float) gradient [:,:,: 3] = [1, 1, 0.] gradient [:,:, 3] = [[0.1, 0.3], [0.3, 0.5] patch_gradient.set_array (gradient) ax. add_artist (patch_gradient) ax. set_xlim (years [0]-0.5, years [-1] + 0.5) ax. set_ylim (0, 15000) 5) save the image fig. savefig ('The Price of Car.png ') plt. show ()

The output image is as follows:

The above is the content of creating a beautiful bar chart in the [Python Tutorial]. For more information, see The PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.