Python 如何为绘图仪图表制作多图表?
我按照这个方向(教程)制作绘图仪图表 () 但它似乎只会在屏幕上创建一个图表 您知道如何在一个屏幕(在一个html文件中)中制作包含这些仪表图表的多个图表吗 所以我想在一个屏幕上创建多个仪表 非常感谢 这就是代码Python 如何为绘图仪图表制作多图表?,python,python-2.7,plotly,Python,Python 2.7,Plotly,我按照这个方向(教程)制作绘图仪图表 () 但它似乎只会在屏幕上创建一个图表 您知道如何在一个屏幕(在一个html文件中)中制作包含这些仪表图表的多个图表吗 所以我想在一个屏幕上创建多个仪表 非常感谢 这就是代码 import plotly import plotly.plotly as py import plotly.graph_objs as go from plotly.offline import plot import plotly.plotly as py import pl
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import plot
import plotly.plotly as py
import plotly.graph_objs as go
base_chart = {
"values": [40, 10, 10, 10, 10, 10, 10],
"labels": ["-", "0", "20", "40", "60", "80", "100"],
"domain": {"x": [0, .48]},
"marker": {
"colors": [
'rgb(255, 255, 255)',
'rgb(255, 255, 255)',
'rgb(255, 255, 255)',
'rgb(255, 255, 255)',
'rgb(255, 255, 255)',
'rgb(255, 255, 255)',
'rgb(255, 255, 255)'
],
"line": {
"width": 1
}
},
"name": "Gauge",
"hole": .4,
"type": "pie",
"direction": "clockwise",
"rotation": 108,
"showlegend": False,
"hoverinfo": "none",
"textinfo": "label",
"textposition": "outside"
}
meter_chart = {
"values": [50, 10, 10, 10, 10, 10],
"labels": ["Days" + "<br>" + "haaga", "How", "Many", "Days", "Are", "Left"],
"marker": {
'colors': [
'rgb(255, 255, 255)',
'rgb(226,126,64)',
'rgb(223,162,103)',
'rgb(223,189,139)',
'rgb(226,210,172)',
'rgb(232,226,202)'
]
},
"domain": {"x": [0, 0.48]},
"name": "Days Guage",
"hole": .3,
"type": "pie",
"direction": "clockwise",
"rotation": 90,
"showlegend": False,
"textinfo": "label",
"textposition": "inside",
"hoverinfo": "none",
}
layout = {
'xaxis': {
'showticklabels': False,
'showgrid': False,
'zeroline': False,
},
'yaxis': {
'showticklabels': False,
'showgrid': False,
'zeroline': False,
},
'shapes': [
{
'type': 'path',
'path': 'M 0.235 0.5 L 0.24 0.65 L 0.245 0.5 Z',
'fillcolor': 'rgba(44, 160, 101, 0.5)',
'line': {
'width': 0.5
},
'xref': 'paper',
'yref': 'paper'
}
],
'annotations': [
{
'xref': 'paper',
'yref': 'paper',
'x': 0.23,
'y': 0.45,
'text': '50',
'showarrow': False
}
]
}
# we don't want the boundary now
base_chart['marker']['line']['width'] = 0
fig = {"data": [base_chart, meter_chart],
"layout": layout}
plot(fig, filename='gauge-meter-chart')
绘图导入
导入plotly.plotly作为py
导入plotly.graph_objs作为go
从plotly.offline导入打印
导入plotly.plotly作为py
导入plotly.graph_objs作为go
基本图表={
“值”:[40,10,10,10,10,10,10],
“标签”:[“-”、“0”、“20”、“40”、“60”、“80”、“100”],
“域”:{“x”:[0.48]},
“标记”:{
“颜色”:[
'rgb(255,255,255)',
'rgb(255,255,255)',
'rgb(255,255,255)',
'rgb(255,255,255)',
'rgb(255,255,255)',
'rgb(255,255,255)',
'rgb(255,255,255)'
],
“行”:{
“宽度”:1
}
},
“名称”:“仪表”,
“洞”:,
“类型”:“馅饼”,
“方向”:“顺时针”,
“轮换”:108,
“showlegend”:假,
“hoverinfo”:“无”,
“文本信息”:“标签”,
“textposition”:“外部”
}
仪表图={
“值”:[50,10,10,10,10,10],
“标签”:[“天数”+“
”+“哈加”、“多少”、“天数”、“是”、“剩下的”],
“标记”:{
“颜色”:[
'rgb(255,255,255)',
"rgb(226126,64)",,
“rgb(223162103)”,
“rgb(223189139)”,
“rgb(226210172)”,
‘rgb(232226202)’
]
},
“域”:{“x”:[0,0.48]},
“名称”:“天计”,
“洞”:,
“类型”:“馅饼”,
“方向”:“顺时针”,
“轮换”:90,
“showlegend”:假,
“文本信息”:“标签”,
“textposition”:“内部”,
“hoverinfo”:“无”,
}
布局={
“xaxis”:{
“显示标签”:False,
“showgrid”:False,
“零线”:错误,
},
“yaxis”:{
“显示标签”:False,
“showgrid”:False,
“零线”:错误,
},
“形状”:[
{
“类型”:“路径”,
‘路径’:‘M0.2350.5L0.240.65L0.2450.5Z’,
“fillcolor”:“rgba(441601010.5)”,
“行”:{
“宽度”:0.5
},
“外部参照”:“纸张”,
“yref”:“纸”
}
],
“注释”:[
{
“外部参照”:“纸张”,
“yref”:“paper”,
“x”:0.23,
“y”:0.45,
“文本”:“50”,
“showarrow”:错误
}
]
}
#我们现在不想要边界
基础图表['marker']['line']['width']=0
图={“数据”:[基础图表、仪表图表],
“布局”:布局}
绘图(图,filename='gauge-meter-chart')
问题在于,勾号是作为布局下的形状
对象生成的,这可以通过对x
和y
位置进行一些小计算轻松实现。请参考下面的示例,其中我使用循环的来定义列的数量,并将形状和注释放置在正确的位置
注意:我只对列进行了此操作,请应用此逻辑并使其也适用于行,如果您这样做,也请发布您的解决方案,以便人们将来可以参考强>
请尝试下面的代码,让我知道你的问题是否得到解决
import plotly
import plotly.plotly as py
import numpy as np
import pandas as pd
import plotly.offline as py_offline
import plotly.graph_objs as go
py_offline.init_notebook_mode()
base_chart = {
"values": [40, 10, 10, 10, 10, 10, 10],
"labels": ["-", "0", "20", "40", "60", "80", "100"],
"domain": {"x": [0, .48]},
"marker": {
"colors": [
'rgb(255, 255, 255)',
'rgb(255, 255, 255)',
'rgb(255, 255, 255)',
'rgb(255, 255, 255)',
'rgb(255, 255, 255)',
'rgb(255, 255, 255)',
'rgb(255, 255, 255)'
],
"line": {
"width": 1
}
},
"name": "Gauge",
"hole": .4,
"type": "pie",
"direction": "clockwise",
"rotation": 108,
"showlegend": False,
"hoverinfo": "none",
"textinfo": "label",
"textposition": "outside"
}
meter_chart = {
"values": [50, 10, 10, 10, 10, 10],
"labels": ["Days" + "<br>" + "haaga", "How", "Many", "Days", "Are", "Left"],
"marker": {
'colors': [
'rgb(255, 255, 255)',
'rgb(226,126,64)',
'rgb(223,162,103)',
'rgb(223,189,139)',
'rgb(226,210,172)',
'rgb(232,226,202)'
]
},
"domain": {"x": [0, 0.48]},
"name": "Days Guage",
"hole": .3,
"type": "pie",
"direction": "clockwise",
"rotation": 90,
"showlegend": False,
"textinfo": "label",
"textposition": "inside",
"hoverinfo": "none",
}
layout = {
'xaxis': {
'showticklabels': False,
'showgrid': False,
'zeroline': False
},
'yaxis': {
'showticklabels': False,
'showgrid': False,
'zeroline': False
},
'shapes': [],
'annotations': []
}
base_shape = {
'type': 'path',
'path': 'M 0.49 0.5 L 0.5 0.65 L 0.51 0.5 Z',
'fillcolor': 'rgba(44, 160, 101, 0.5)',
'line': {
'width': 0.5
},
'xref': 'paper',
'yref': 'paper'
}
base_annotation = {
'xref': 'paper',
'yref': 'paper',
'x': 0,
'y': 0.45,
'text': '50',
'showarrow': False
}
arr = []
columns = 4
tick_width = 0.009
increment = 1/columns
initial = 0
for i in range(columns):
basechart = copy.deepcopy(base_chart)
meterchart = copy.deepcopy(meter_chart)
start = initial
end = initial + increment
basechart['domain']['x'] = [start, end]
meterchart['domain']['x'] = [start, end]
# code for placing the tick on each of the dials
shape = copy.deepcopy(base_shape)
temp = (start + (increment/2))
shape['path'] = 'M '+str(temp-tick_width/2)+' 0.5 L '+str(temp)+' 0.59 L '+str(temp+tick_width/2)+' 0.5 Z'
layout['shapes'].append(shape)
# code for placing the value annotation on each of the dials
annotation = copy.deepcopy(base_annotation)
annotation['x'] = temp
layout['annotations'].append(annotation)
arr.append(basechart)
arr.append(meterchart)
initial = end
# we don't want the boundary now
base_chart['marker']['line']['width'] = 0
fig = {"data": arr,
"layout": layout}
py_offline.iplot(fig, filename='gauge-meter-chart')
绘图导入
导入plotly.plotly作为py
将numpy作为np导入
作为pd进口熊猫
将plotly.offline作为py\u offline导入
导入plotly.graph_objs作为go
py_offline.init_notebook_mode()
基本图表={
“值”:[40,10,10,10,10,10,10],
“标签”:[“-”、“0”、“20”、“40”、“60”、“80”、“100”],
“域”:{“x”:[0.48]},
“标记”:{
“颜色”:[
'rgb(255,255,255)',
'rgb(255,255,255)',
'rgb(255,255,255)',
'rgb(255,255,255)',
'rgb(255,255,255)',
'rgb(255,255,255)',
'rgb(255,255,255)'
],
“行”:{
“宽度”:1
}
},
“名称”:“仪表”,
“洞”:,
“类型”:“馅饼”,
“方向”:“顺时针”,
“轮换”:108,
“showlegend”:假,
“hoverinfo”:“无”,
“文本信息”:“标签”,
“textposition”:“外部”
}
仪表图={
“值”:[50,10,10,10,10,10],
“标签”:[“天数”+“
”+“哈加”、“多少”、“天数”、“是”、“剩下的”],
“标记”:{
“颜色”:[
'rgb(255,255,255)',
"rgb(226126,64)",,
“rgb(223162103)”,
“rgb(223189139)”,
“rgb(226210172)”,
‘rgb(232226202)’
]
},
“域”:{“x”:[0,0.48]},
“名称”:“天计”,
“洞”:,
“类型”:“馅饼”,
“方向”:“顺时针”,
“轮换”:90,
“showlegend”:假,
“文本信息”:“标签”,
“textposition”:“内部”,
“hoverinfo”:“无”,
}
布局={
“xaxis”:{
“显示标签”:False,
“showgrid”:False,
“零线”:False
},
“yaxis”:{
“显示标签”:False,
“showgrid”:False,
“零线”:False
},
“形状”:[],
‘注释’:[]
}
基础形状={
“类型”:“路径”,
“路径”:“M 0.49 0.5 L 0.5 0.65 L 0.51 0.5 Z”,
“fillcolor”:“rgba(441601010.5)”,
“行”:{
“宽度”:0.5
},
“外部参照”:“纸张”,
“yref”:“纸”
}
基本注释={
“外部参照”:“纸张”,
“yref”:“paper”,
“x”:0,
“y”:0.45,
“文本”:“50”,
“showarrow”:错误
}
arr=[]
列=4
勾选宽度=0.009
增量=1/列
初始值=0
对于范围内的i(列):
基线图=