赞
踩
文章最前: 我是Octopus,这个名字来源于我的中文名--章鱼;我热爱编程、热爱算法、热爱开源。所有源码在我的个人github ;这博客是记录我学习的点点滴滴,如果您对 Python、Java、AI、算法有兴趣,可以关注我的动态,一起学习,共同进步。
最近由于工作需要,需要对数据进行一些可视化操作,找到了Plotly库,现在就学习一下plotly进行绘图
- import numpy as np
- import pandas as pd
- import plotly.graph_objects as go
- import plotly.offline as po
- from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
- import matplotlib.pyplot as plt
- import plotly.express as px
- import random
- import plotly.figure_factory as ff
- random_x = np.random.randint(1, 100, 50)
- random_y = np.random.randint(1, 100, 50)
- data = [
- go.Scatter(x = random_x, y = random_y, mode = 'markers')
- ]
-
- layout = go.Layout(
- xaxis = dict(
- showgrid=True, # Hide Gridlines
- showline=False, # Hide X-Axis
- ),
- yaxis = dict(
- showgrid=True, # Hide Gridlines
- showline=False, # Hide X-Axis
- ),
- )
- fig = go.Figure(data = data, layout = layout)
- iplot(fig)
- # Changing Marker size , shape & color using Marker parameter
- x_val = np.random.randint(1,100,50)
- y_val = np.random.randint(1,100,50)
- data = [go.Scatter(
- x = x_val,
- y = y_val,
- mode = 'markers',
- marker = dict(
- size = 10,
- color = '#91bd3a', #color of marker
- symbol = 'circle', # Shape of scatter plot
- line = dict(width = 1) #width of boundary
- )
- )
- ]
- layout = go.Layout(
- xaxis=dict(
- showgrid=False, # Hide Gridlines
- showline=False, # Hide X-Axis
- ),
- yaxis=dict(
- showgrid=False, # Hide Gridlines
- showline=False, # Hide X-Axis
- ),
- )
- fig = go.Figure(data=data,layout=layout)
- iplot(fig)
- # Defining Labels (X-Axis & Y-Axis label , Graph tile)
- x_val = np.random.randint(1,100,50)
- y_val = np.random.randint(1,100,50)
- data = [go.Scatter(
- x = x_val,
- y = y_val,
- mode = 'markers',
- marker = dict(
- size = 10,
- color = '#4ED700',
- symbol = 'circle',
- line = dict(width = 1,color = '#0E8700')
- )
- )
- ]
- layout = go.Layout(
- title = '$Scatter Plot$', # Title
- xaxis = dict(title = '$X-Axis$',showgrid=False,showline=False), # x-axis label
- yaxis = dict(title = '$Y-Axis$',showgrid=False,showline=False), # y-axis label
- )
- fig = go.Figure(data=data, layout=layout)
- iplot(fig)
- x_values = np.linspace(0, 100, 100) # 100 evenly spaced values
- y_values = np.random.randn(100) # 100 random values
- trace0 = go.Scatter(
- x = x_values,
- y = y_values,
- mode = 'markers',
- marker = dict(
- size = 7,
- color = '#F4D03F',
- symbol = 'circle',
- line = dict(width = 1,color = '#0E8700')
- )
- )
- trace1 = go.Scatter(
- x = x_values,
- y = y_values-5,
- mode = 'markers',
- marker = dict(size = 7,
- color = '#A9DFBF',
- symbol = 'circle',
- line = dict(width = 1,color = '#0E8700')
- )
- )
- data = [trace0, trace1]
- layout = go.Layout(
- xaxis=dict(
- showgrid=False, # Hide Gridlines
- showline=False, # Hide X-Axis
- ),
- yaxis=dict(
- showgrid=False, # Hide Gridlines
- showline=False, # Hide X-Axis
- ),
- )
- fig = go.Figure(data=data,layout=layout)
- iplot(fig)
- data = [go.Scatter(
- x = auto_df["engine-size"],
- y = auto_df["wheel-base"],
- mode = 'markers',
- marker = dict(size = 7,
- color = '#4ED700',
- symbol = 'circle',
- line = dict(width = 1,color = '#0E8700')
- )
- )
- ]
- layout = go.Layout(
- title = 'EngineSize-WheelBase', # Chart Title
- xaxis = dict(title = 'Engine Size$',showgrid=False,showline=False), # x-axis label
- yaxis = dict(title = '$Wheel Base$',showgrid=False,showline=False), # y-axis label
- )
- fig = go.Figure(data=data, layout=layout)
- iplot(fig)
- # trace0 will capture all smokers
- trace0 = go.Scatter(
- x = auto_df[auto_df["fuel-type"]=='gas']["engine-size"],
- y = auto_df[auto_df["fuel-type"]=='gas']["wheel-base"],
- mode = 'markers',
- name = 'Gas',
- marker = dict(size = 7, color = '#F39C12',symbol = 'circle',line = dict(width = 1))
- )
- # trace1 will capture all non-smokers
- trace1 = go.Scatter(
- x = auto_df[auto_df["fuel-type"]=='diesel']["engine-size"],
- y = auto_df[auto_df["fuel-type"]=='diesel']["wheel-base"],
- mode = 'markers',
- name = 'Diesel',
- marker = dict(size = 7, color = '#8BC34A',symbol = 'circle',line = dict(width = 1))
- )
- layout = go.Layout(
- title = '$Scatter Plot$', # Title
- xaxis = dict(title = '$Engine Size$',showgrid=False,showline=False), # x-axis label
- yaxis = dict(title = '$Wheel Base$',showgrid=False,showline=False), # y-axis label
- )
- data = [trace0, trace1]
- fig = go.Figure(data=data,layout=layout)
- iplot(fig)
- # Display multiple Scatter plots in one figure using Subplots
- from plotly.subplots import make_subplots
- #Subplot initialization
- fig = make_subplots(
- rows=1,
- cols=2,
- subplot_titles=("Subplot-1", "Subplot-2")
- )
- # Subplot - 1 (Add graph object trace to a figure)
- fig.add_trace(go.Scatter
- (
- x = x_val,
- y = y_val,
- mode = 'markers',
- marker = dict(size = 10, color = 'crimson',symbol = 'circle',line = dict(width = 1,color = '#0E8700'))
- ),
- row=1, col=1
- )
-
- # Add graph object trace to a figure (Subplot-2)
- fig.add_trace(go.Scatter
- (
- x = x_val,
- y = y_val,
- mode = 'markers',
- marker = dict(size = 10, color = 'gold',symbol = 'circle',line = dict(width = 1))
- ),
- row=1, col=2
- )
- # Hide grid lines
- fig.update_xaxes(showgrid=False)
- fig.update_yaxes(showgrid=False)
-
- fig.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。