<jupyter_start><jupyter_text>Watch Me Code 2: Plotly and Cufflinks- Plotly is a cloud based plotting service. It uses the popular JavaScript library D3.js.- Plotly is simple to use<jupyter_code>!pip install chart-studio
import chart_studio as plotly
import chart_studio.plotly as py
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd<jupyter_output><empty_output><jupyter_text>To use plot.ly you need to sign up for a free account then get API credentials. https://chart-studio.plot.ly/settings/api<jupyter_code># setup the credentials
plotly.tools.set_credentials_file(username='mafudge73fc', api_key='xq1nCPgiHZNrU9je0VaW')
# Start with a Simple Pandas DataFrame
grades = { 'subjects' : ['Mathematics', 'English', 'History', 'Science', 'Arts'],
           'grades' : [67, 60, 36, 61, 58]
         }
grades_df = pd.DataFrame(grades)
grades_df<jupyter_output><empty_output><jupyter_text>To plot with plotly, we need:- Data: this is a list of subplots, - A Python Dictionary of information to put on the plot<jupyter_code>grade_data = [go.Bar(x=grades_df['subjects'], y=grades_df['grades'])] # Bar Plot, note this is a list

py.iplot({ 'data': grade_data,
            'layout': {
               'title': 'My Grades R Asum',
               'xaxis': {
                 'title': 'Subjects Are Bad'},
               'yaxis': {
                'title': 'Grades'}
        }})<jupyter_output><empty_output><jupyter_text>Plotly express makes it easy to plot a dataframe<jupyter_code># Same plot as a one-liner using plotly.express
px.bar(grades_df, x='subjects', y='grades', title='My Grades R Awesum')<jupyter_output><empty_output><jupyter_text>How about an example with multiple series? For that we need to pull in another dataset<jupyter_code>cuse_weather_df = pd.read_csv('https://raw.githubusercontent.com/mafudge/datasets/master/weather/syracuse-ny.csv')
cuse_weather_df = cuse_weather_df[ cuse_weather_df['EST'].str.startswith('2015-')] 
cuse_weather_df.head(5)
r = dict(color='red')
g = dict(color='green')
b = dict(color='blue')
grade_data = [
    go.Scatter(x=cuse_weather_df['EST'], y=cuse_weather_df['Max TemperatureF'], mode="lines", name="Max Temp", marker=r),
    go.Scatter(x=cuse_weather_df['EST'], y=cuse_weather_df['Mean TemperatureF'], mode="lines+markers", name="Mean Temp", marker=g),
    go.Scatter(x=cuse_weather_df['EST'], y=cuse_weather_df['Min TemperatureF'], mode="lines", name="Min Temp", marker=b)
]

py.iplot({ 'data': grade_data,
            'layout': {
               'title': 'Syracuse Weather 2015',
               'xaxis': {
                 'title': 'Day of the Year'},
               'yaxis': {
                'title': 'Temperature Deg F'}
        }})<jupyter_output><empty_output><jupyter_text>Here's another example with the Exam Scores Dataset. Shows you how much more expressive plot.ly can be.<jupyter_code>scores_df = pd.read_csv('https://raw.githubusercontent.com/mafudge/datasets/master/exam-scores/exam-scores.csv')
scores_df = scores_df.sort_values(by='Student_Score')
scores_df[0:6]
grade_data = [
    go.Scatter(x=scores_df['Letter_Grade'], y=scores_df['Completion_Time'], mode="markers", 
               marker= { 'size': scores_df['Student_Score'], 'sizemode' : 'diameter', 'sizeref' : 1.0})
]

py.iplot({ 'data': grade_data,
            'layout': {
               'title': 'Exam Grades',
               'xaxis': {
                 'title': 'Letter Grade'},
               'yaxis': {
                'title': 'Time To Complete Exam'}
        }})
grade_data = [
    go.Heatmap(x=scores_df['Exam_Version'], y=scores_df['Completion_Time'], z=scores_df['Student_Score'])
]

py.iplot({ 'data': grade_data,
            'layout': {
               'title': 'Exam Grades Heat Map',
               'xaxis': {
                 'title': 'Exam Version'},
               'yaxis': {
                 'title': 'Time To Complete Exam'}           
        }})
# A manual sample, showing you don't need to use Pandas at all.

trace0 = go.Scatter(
    x=[1,2,3,4,5,6,7,8],
    y=[10, 15, 13, 17, 15, 12, 10, 18],
    mode = "markers",
    name = "series 2"
)
trace1 = go.Scatter(
    x=[1,2,3,4,5,6,7,8],
    y=[16, 5, 11, 9, 16, 10, 14, 12],
    mode="lines",
    name = "series 1"
)
data = [trace0, trace1]

py.iplot({ 'data': data,
            'layout': {
               'title': 'Sample Chart',
               'xaxis': {
                 'title': 'X Axis'},
               'yaxis': {
                 'title': 'Y Axis'}            
        }})
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv')

# make the drawing figure
fig = go.Figure(go.Scatter(x=df.gdpPercap, y=df.lifeExp, text=df.country, mode='markers', name='2007'))
fig.update_xaxes(title_text='GDP per Capita', type='log')
fig.update_yaxes(title_text='Life Expectancy')

#save it under a specific name
py.plot(fig, filename='pandas-multiple-scatter')
#show it
fig.show()
# example using px also using ipython display
from IPython.display import display

display(px.scatter(df, x='gdpPercap', y='lifeExp', text='country'))
display(px.scatter(df, x='gdpPercap', y='lifeExp', text=None))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15005793742757823, 'fraction_numerical': 0.01738122827346466, 'mean_word_length': 3.385266723116003, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '404836', 'license': 'MIT', 'n_tokens_mistral': 1883, 'n_tokens_neox': 1758, 'n_words': 410, 'path': 'content/lessons/13/Watch-Me-Code/WMC2-Plotly.ipynb', 'repo_name': 'MahopacHS/spring-2020-oubinam0717', 'size': '5178'}