编程语言
首页 > 编程语言> > Python for Data Science - Creating basic charts

Python for Data Science - Creating basic charts

作者:互联网

Chapter 7 - Collaborative Analytics with Plotly

Segment 1 - Creating basic charts

Setting up to use Plotly within Jupyter

pip install --default-timeout=1000 Plotly
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: Plotly in /home/ericwei/.local/lib/python3.7/site-packages (4.14.3)
Requirement already satisfied: retrying>=1.3.3 in /home/ericwei/.local/lib/python3.7/site-packages (from Plotly) (1.3.3)
Requirement already satisfied: six in /home/ericwei/.local/lib/python3.7/site-packages (from Plotly) (1.15.0)
Note: you may need to restart the kernel to use updated packages.
pip install --default-timeout=1000 cufflinks
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: cufflinks in /home/ericwei/.local/lib/python3.7/site-packages (0.17.3)
Requirement already satisfied: ipython>=5.3.0 in /home/ericwei/.local/lib/python3.7/site-packages (from cufflinks) (7.8.0)
Requirement already satisfied: setuptools>=34.4.1 in /home/ericwei/.local/lib/python3.7/site-packages (from cufflinks) (51.1.0.post20201221)
Requirement already satisfied: ipywidgets>=7.0.0 in /home/ericwei/.local/lib/python3.7/site-packages (from cufflinks) (7.5.1)
Requirement already satisfied: pandas>=0.19.2 in /home/ericwei/.local/lib/python3.7/site-packages (from cufflinks) (1.1.5)
Requirement already satisfied: six>=1.9.0 in /home/ericwei/.local/lib/python3.7/site-packages (from cufflinks) (1.15.0)
Requirement already satisfied: colorlover>=0.2.1 in /home/ericwei/.local/lib/python3.7/site-packages (from cufflinks) (0.3.0)
Requirement already satisfied: numpy>=1.9.2 in /home/ericwei/.local/lib/python3.7/site-packages (from cufflinks) (1.19.4)
Requirement already satisfied: plotly>=4.1.1 in /home/ericwei/.local/lib/python3.7/site-packages (from cufflinks) (4.14.3)
Requirement already satisfied: traitlets>=4.2 in /home/ericwei/.local/lib/python3.7/site-packages (from ipython>=5.3.0->cufflinks) (4.3.3)
Requirement already satisfied: pickleshare in /home/ericwei/.local/lib/python3.7/site-packages (from ipython>=5.3.0->cufflinks) (0.7.5)
Requirement already satisfied: backcall in /home/ericwei/.local/lib/python3.7/site-packages (from ipython>=5.3.0->cufflinks) (0.1.0)
Requirement already satisfied: decorator in /home/ericwei/.local/lib/python3.7/site-packages (from ipython>=5.3.0->cufflinks) (4.4.0)
Requirement already satisfied: jedi>=0.10 in /home/ericwei/.local/lib/python3.7/site-packages (from ipython>=5.3.0->cufflinks) (0.15.1)
Requirement already satisfied: prompt-toolkit<2.1.0,>=2.0.0 in /home/ericwei/.local/lib/python3.7/site-packages (from ipython>=5.3.0->cufflinks) (2.0.10)
Requirement already satisfied: pexpect in /usr/lib/python3/dist-packages (from ipython>=5.3.0->cufflinks) (4.6.0)
Requirement already satisfied: pygments in /home/ericwei/.local/lib/python3.7/site-packages (from ipython>=5.3.0->cufflinks) (2.4.2)
Requirement already satisfied: ipykernel>=4.5.1 in /home/ericwei/.local/lib/python3.7/site-packages (from ipywidgets>=7.0.0->cufflinks) (5.1.2)
Requirement already satisfied: nbformat>=4.2.0 in /home/ericwei/.local/lib/python3.7/site-packages (from ipywidgets>=7.0.0->cufflinks) (4.4.0)
Requirement already satisfied: widgetsnbextension~=3.5.0 in /home/ericwei/.local/lib/python3.7/site-packages (from ipywidgets>=7.0.0->cufflinks) (3.5.1)
Requirement already satisfied: jupyter-client in /home/ericwei/.local/lib/python3.7/site-packages (from ipykernel>=4.5.1->ipywidgets>=7.0.0->cufflinks) (5.3.4)
Requirement already satisfied: tornado>=4.2 in /home/ericwei/.local/lib/python3.7/site-packages (from ipykernel>=4.5.1->ipywidgets>=7.0.0->cufflinks) (6.0.3)
Requirement already satisfied: parso>=0.5.0 in /home/ericwei/.local/lib/python3.7/site-packages (from jedi>=0.10->ipython>=5.3.0->cufflinks) (0.5.1)
Requirement already satisfied: ipython-genutils in /home/ericwei/.local/lib/python3.7/site-packages (from nbformat>=4.2.0->ipywidgets>=7.0.0->cufflinks) (0.2.0)
Requirement already satisfied: jupyter-core in /home/ericwei/.local/lib/python3.7/site-packages (from nbformat>=4.2.0->ipywidgets>=7.0.0->cufflinks) (4.6.0)
Requirement already satisfied: jsonschema!=2.5.0,>=2.4 in /home/ericwei/.local/lib/python3.7/site-packages (from nbformat>=4.2.0->ipywidgets>=7.0.0->cufflinks) (3.1.1)
Requirement already satisfied: attrs>=17.4.0 in /home/ericwei/.local/lib/python3.7/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets>=7.0.0->cufflinks) (19.2.0)
Requirement already satisfied: importlib-metadata in /home/ericwei/.local/lib/python3.7/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets>=7.0.0->cufflinks) (0.23)
Requirement already satisfied: pyrsistent>=0.14.0 in /home/ericwei/.local/lib/python3.7/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets>=7.0.0->cufflinks) (0.15.4)
Requirement already satisfied: pytz>=2017.2 in /home/ericwei/.local/lib/python3.7/site-packages (from pandas>=0.19.2->cufflinks) (2020.4)
Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.19.2->cufflinks) (2.8.0)
Requirement already satisfied: retrying>=1.3.3 in /home/ericwei/.local/lib/python3.7/site-packages (from plotly>=4.1.1->cufflinks) (1.3.3)
Requirement already satisfied: wcwidth in /home/ericwei/.local/lib/python3.7/site-packages (from prompt-toolkit<2.1.0,>=2.0.0->ipython>=5.3.0->cufflinks) (0.1.7)
Requirement already satisfied: notebook>=4.4.1 in /home/ericwei/.local/lib/python3.7/site-packages (from widgetsnbextension~=3.5.0->ipywidgets>=7.0.0->cufflinks) (6.0.1)
Requirement already satisfied: prometheus-client in /home/ericwei/.local/lib/python3.7/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.0.0->cufflinks) (0.7.1)
Requirement already satisfied: Send2Trash in /home/ericwei/.local/lib/python3.7/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.0.0->cufflinks) (1.5.0)
Requirement already satisfied: pyzmq>=17 in /home/ericwei/.local/lib/python3.7/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.0.0->cufflinks) (18.1.0)
Requirement already satisfied: nbconvert in /home/ericwei/.local/lib/python3.7/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.0.0->cufflinks) (5.6.0)
Requirement already satisfied: terminado>=0.8.1 in /home/ericwei/.local/lib/python3.7/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.0.0->cufflinks) (0.8.2)
Requirement already satisfied: jinja2 in /home/ericwei/.local/lib/python3.7/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.0.0->cufflinks) (2.10.3)
Requirement already satisfied: ptyprocess in /home/ericwei/.local/lib/python3.7/site-packages (from terminado>=0.8.1->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.0.0->cufflinks) (0.6.0)
Requirement already satisfied: zipp>=0.5 in /home/ericwei/.local/lib/python3.7/site-packages (from importlib-metadata->jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets>=7.0.0->cufflinks) (0.6.0)
Requirement already satisfied: more-itertools in /home/ericwei/.local/lib/python3.7/site-packages (from zipp>=0.5->importlib-metadata->jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets>=7.0.0->cufflinks) (7.2.0)
Requirement already satisfied: MarkupSafe>=0.23 in /usr/lib/python3/dist-packages (from jinja2->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.0.0->cufflinks) (1.1.0)
Requirement already satisfied: mistune<2,>=0.8.1 in /home/ericwei/.local/lib/python3.7/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.0.0->cufflinks) (0.8.4)
Requirement already satisfied: pandocfilters>=1.4.1 in /home/ericwei/.local/lib/python3.7/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.0.0->cufflinks) (1.4.2)
Requirement already satisfied: testpath in /home/ericwei/.local/lib/python3.7/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.0.0->cufflinks) (0.4.2)
Requirement already satisfied: bleach in /home/ericwei/.local/lib/python3.7/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.0.0->cufflinks) (3.1.0)
Requirement already satisfied: entrypoints>=0.2.2 in /usr/lib/python3/dist-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.0.0->cufflinks) (0.3)
Requirement already satisfied: defusedxml in /home/ericwei/.local/lib/python3.7/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.0.0->cufflinks) (0.6.0)
Requirement already satisfied: webencodings in /home/ericwei/.local/lib/python3.7/site-packages (from bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets>=7.0.0->cufflinks) (0.5.1)
Note: you may need to restart the kernel to use updated packages.
pip install --default-timeout=1000 chart_studio
Defaulting to user installation because normal site-packages is not writeable
Collecting chart_studio
  Downloading chart_studio-1.1.0-py3-none-any.whl (64 kB)
     |████████████████████████████████| 64 kB 9.2 kB/s ta 0:00:01
[?25hRequirement already satisfied: plotly in /home/ericwei/.local/lib/python3.7/site-packages (from chart_studio) (4.14.3)
Requirement already satisfied: six in /home/ericwei/.local/lib/python3.7/site-packages (from chart_studio) (1.15.0)
Requirement already satisfied: requests in /usr/lib/python3/dist-packages (from chart_studio) (2.21.0)
Requirement already satisfied: retrying>=1.3.3 in /home/ericwei/.local/lib/python3.7/site-packages (from chart_studio) (1.3.3)
Installing collected packages: chart-studio
Successfully installed chart-studio-1.1.0
Note: you may need to restart the kernel to use updated packages.
import numpy as np
import pandas as pd

import cufflinks as cf

import chart_studio.plotly as py
import chart_studio.tools as tls
import plotly.graph_objs as go
tls.set_credentials_file(username='xxxxx', api_key='xxxxxxxxxx')

Creating line charts

A very basic line chart

a = np.linspace(start=0, stop=36, num=36)

np.random.seed(25)

b = np.random.uniform(low=0.0, high=1.0, size=36)

trace = go.Scatter(x=a, y=b)

data = [trace]

py.iplot(data, filename="basic-line-chart")

image-20210123074538172

A line chart from a pandas dataframe

address = '~/Data/mtcars.csv'

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

df = cars[['cyl','wt','mpg']]

layout = dict(title='Chart from Pandas DataFrame', xaxis = dict(title='x-axis'), yaxis = dict(title='y-axis'))

df.iplot(filename='cf-simple-line-chart', layout=layout)

image-20210123074559854

Creating bar charts

data = [go.Bar(x=[1,2,3,4,5,6,7,8,9,10], y=[1,2,3,4,0.5,4,3,2,1])]
print(data)
[Bar({
    'x': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'y': [1, 2, 3, 4, 0.5, 4, 3, 2, 1]
})]
layoyt = dict(titile='Simple Bar Chart', 
              xaxis=dict(titile='x-axis'),yaxis=dict(title='y-axis'))
py.iplot(data, filename='basic-bar-chart', layout=layout)

image-20210123074609987

Creating pie charts

fig = {'data':[{'labels':['bicycle','motorcycle','car','van','stroller'],
               'values':[1,2,3,4,0.5],
               'type':'pie'}],
      'layout':{'title':'Simple Pie Chart'}}
py.iplot(fig)

image-20210123074620228

标签:Requirement,already,Creating,lib,Python,Science,satisfied,site,packages
来源: https://www.cnblogs.com/keepmoving1113/p/14316461.html