码迷,mamicode.com
首页 > 编程语言 > 详细

Python for Data Science - Creating standard data graphics

时间:2021-01-06 12:27:05      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:exe   eric   enc   rand   data-   pyplot   res   obj   lib   

Chapter 4 - Practical Data Visualization

Segment 1 - Creating standard data graphics

import numpy as np
from numpy.random import randn
import pandas as pd
from pandas import Series, DataFrame

import matplotlib.pyplot as plt
from matplotlib import rcParams

Creating a line chart from a list object

Plotting a line chart in matplotlib

x = range(1,10)
y = [1,2,3,4,0,4,3,2,1]

plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x7f361b7a7780>]

技术图片

Plotting a line chart from a Pandas object

address = ‘~/Data/mtcars.csv‘

cars = pd.read_csv(address)
cars.columns = [‘car_names‘,‘mpg‘,‘cyl‘,‘disp‘, ‘hp‘, ‘drat‘, ‘wt‘, ‘qsec‘, ‘vs‘, ‘am‘, ‘gear‘, ‘carb‘]

mpg = cars[‘mpg‘]
mpg.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7f361afafd30>

技术图片

df = cars[[‘cyl‘,‘wt‘,‘mpg‘]]
df.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7f361af44e80>

技术图片

Creating bar charts

Creating a bar chart from a list

plt.bar(x,y)
<BarContainer object of 9 artists>

技术图片

Creating bar charts from Pandas objects

mpg.plot(kind="bar")
<matplotlib.axes._subplots.AxesSubplot at 0x7f361ae4a710>

技术图片

mpg.plot(kind="barh")
<matplotlib.axes._subplots.AxesSubplot at 0x7f361af44400>

技术图片

Creating a pie chart

x = [1,2,3,4,0.5]
plt.pie(x)
plt.show()

技术图片

Saving a plot

plt.pie(x)
plt.savefig(‘plt_chart.png‘)
plt.show()

技术图片

%pwd
‘/home/ericwei/Ex_Files_Python_Data_Science_EssT_Pt_1/Exercise Files/04_01_begin‘

Python for Data Science - Creating standard data graphics

标签:exe   eric   enc   rand   data-   pyplot   res   obj   lib   

原文地址:https://www.cnblogs.com/keepmoving1113/p/14226855.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!