python - Plotly:如何摆脱多余的 xaxis 刻度?
问题描述
这是一个从 .xlsx 文件中读取数据并利用 Plotly 库绘制气泡图的程序。这是 .xlsx 文件的原始数据:
1991 1992 1993 1994 1995 1996 1997 1998 1999
US 10 14 16 18 20 42 64 100 50
JAPAN 100 30 70 85 30 42 64 98 24
CN 50 22 30 65 70 66 60 45 45
INDIA 90 88 35 50 90 60 40 66 76
UK 40 50 70 50 25 30 22 40 60
这是代码:
# Version 2
import plotly as py
import plotly.graph_objs as go
import openpyxl
import pandas as pd
wb = openpyxl.load_workbook('grape output.xlsx')
sheet = wb['Sheet1']
row_max = sheet.max_row
col_max = sheet.max_column
first_row_list = []
first_col_list = []
for col_n in range(2, col_max+1):
first_row_list.append(sheet.cell(row=1, column=col_n).value)
for row_n in range(2,row_max+1):
first_col_list.append(sheet.cell(row=row_n, column=1).value)
data_all = pd.read_excel('grape output.xlsx')
data_selected = data_all.loc[:,first_row_list]
df = pd.DataFrame(data_selected)
df.index = first_col_list
colors = ['rgb(150,204,90)','rgb(255, 130, 71)','rgb(255, 193, 37)','rgb(180,240,190)','rgb(255, 10, 1)',
'rgb(25, 19, 3)','rgb(100, 100, 100)','rgb(45,24,200)','rgb(33, 58, 108)','rgb(35, 208, 232)']
data = [go.Scatter(
x=df.columns,
y=[country]*len(df.columns),
mode='markers+text',
marker=dict(
color=colors[num],
size= df.loc[country],
showscale = False,
),
text=list(map(str, df.loc[country])),
textposition='middle center',
)
for num, country in enumerate(reversed(df.index))
]
layout = go.Layout(plot_bgcolor='rgb(10, 10, 10)',
paper_bgcolor='rgb(20, 55, 100)',
font={
'size': 15,
'family': 'sans-serif',
'color': 'rgb(255, 255, 255)'
},
width=1000,
height=800,
xaxis=dict(
title='Output of grapes per year in different countries',
nticks = col_max+1,
),
showlegend=False,
margin=dict(l=100, r=100, t=100, b=100),
hovermode = False,
)
fig = go.Figure(data=data, layout=layout)
py.offline.plot(fig, filename='basic-scatter.html')
气泡图绘制如下: 除了 1990 年和 2000 年的两个冗余刻度以及两条白色垂直线外,一切正常。我怎样才能做出一些改变来摆脱它们?谢谢!
解决方案
如果您type='category'
在xaxis
布局中设置两个多余的刻度和相应的网格线将不再显示在图中:
import plotly as py
import plotly.graph_objs as go
import openpyxl
import pandas as pd
wb = openpyxl.load_workbook('grape output.xlsx')
sheet = wb['Sheet1']
row_max = sheet.max_row
col_max = sheet.max_column
first_row_list = []
first_col_list = []
for col_n in range(2, col_max+1):
first_row_list.append(sheet.cell(row=1, column=col_n).value)
for row_n in range(2,row_max+1):
first_col_list.append(sheet.cell(row=row_n, column=1).value)
data_all = pd.read_excel('grape output.xlsx')
data_selected = data_all.loc[:,first_row_list]
df = pd.DataFrame(data_selected)
df.index = first_col_list
colors = ['rgb(150,204,90)','rgb(255, 130, 71)','rgb(255, 193, 37)','rgb(180,240,190)','rgb(255, 10, 1)',
'rgb(25, 19, 3)','rgb(100, 100, 100)','rgb(45,24,200)','rgb(33, 58, 108)','rgb(35, 208, 232)']
data = [go.Scatter(
x=df.columns,
y=[country]*len(df.columns),
mode='markers+text',
marker=dict(
color=colors[num],
size= df.loc[country],
showscale = False,
),
text=list(map(str, df.loc[country])),
textposition='middle center',
)
for num, country in enumerate(reversed(df.index))
]
layout = go.Layout(plot_bgcolor='rgb(10, 10, 10)',
paper_bgcolor='rgb(20, 55, 100)',
font={
'size': 15,
'family': 'sans-serif',
'color': 'rgb(255, 255, 255)'
},
width=1000,
height=800,
xaxis=dict(
title='Output of grapes per year in different countries',
nticks = col_max+1,
type='category'
),
showlegend=False,
margin=dict(l=100, r=100, t=100, b=100),
hovermode = False,
)
fig = go.Figure(data=data, layout=layout)
py.offline.plot(fig, filename='basic-scatter.html')
推荐阅读
- sql-server - MS SQL Linux“资源池‘内部’中的系统内存不足,无法运行此查询。”
- vue.js - 如何从另一个组件导入一个组件作为道具?
- azure - 从分配的用户中删除 Azure AD 用户时不再停用
- c# - 如何在序列化过程中为我的 ExpandoObject 命名?
- javascript - 使用单个 JavaScript 文件更新 2 个 HTML 文件中的设计
- maven - spark3.0.0 中内置的 hivethriftserver。在 CLASSPATH 中找不到错误“org.postgresql.Driver”
- unity3d - 如何使用按钮从不同场景更改对象的材质?
- github - Codesandbox 和导入使用 Vue-CLI 构建的 Github 存储库:TypeError 无法读取未定义的属性“toString”
- java - Micronaut Http 基本身份验证提示
- java - 在重新连接到服务器时,管道中没有发生 SSL 握手