我不知道如何在 Altair 中增加图表标题的字体大小。我的代码只会更改轴标题的字体大小,而不会更改图表标题的字体大小。
这是我测试过的:
data = df
bars = alt.Chart(data, title="This is the Chart Title").mark_bar().encode(
x = 'Feature1',
y = 'Feature2',
color = alt.Color('Feature1', legend=None)).configure_axis(
labelFontSize=16,
titleFontSize=16
)
bars
Run Code Online (Sandbox Code Playgroud)
在这种情况下,titleFontSize
参数改变用于轴标题(字体'Feature1'
和'Feature2'
在这个例子中)。有没有什么简单的方法可以增加图表标题的字体?
我正在尝试使用一个带有坐标的熊猫数据框添加一个用作罢工区的框,并将其传递给altair。
box = pd.DataFrame()
box.loc[:,"x"] = [-0.5, 0.5, 0.5, -0.5]
box.loc[:,'y'] = [1.25, 1.25, 0.5, 0.5]
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
g = alt.Chart(box.loc[0:1,:]).mark_line().encode(
x = 'x',
y = 'y')
d = alt.Chart(box.loc[1:2,:]).mark_line().encode(
x = 'x',
y = 'y')
e = alt.Chart(box.loc[2:3,:]).mark_line().encode(
x = 'x',
y = 'y')
f = alt.Chart(box.loc[3:4,:]).mark_line().encode(
x = 'x',
y = 'y')
g + d + e + f
Run Code Online (Sandbox Code Playgroud)
我也想知道如何调整x和y轴,以便在盒子周围留一点余量?
我想突出显示Altair中2条线之间的区域。
我试图查看解决方案是否正在使用,mark_area()
但找不到如何指定数据下限。
我的数据(几行):
index created pct_pl_transit pct_pl_transit_max
0 1970-01-01 00:00:00 49.627697 60.056873
2 1970-01-01 01:00:00 43.800967 55.301460
4 1970-01-01 02:00:00 41.440480 49.757740
6 1970-01-01 03:00:00 37.879753 40.352226
8 1970-01-01 04:00:00 36.691287 19.429075
Run Code Online (Sandbox Code Playgroud)
我的图表:
base=alt.Chart(lien_traf_gest_traf_lapi).encode(x=alt.X('created', axis=alt.Axis(title='Heure', format='%Hh%M')))
line_pct_pl_lapi=base.mark_line(color='blue').encode(y=alt.Y('pct_pl_transit:Q', axis=alt.Axis(title='Nombre de PL SIREDO')))
line_pct_max=base.mark_line().encode(y='pct_pl_transit_max')
line_pct_max+line_pct_pl_lapi
Run Code Online (Sandbox Code Playgroud) 我已成功在 Altair 中创建并渲染带有货币前缀 ($) 的图表,但我需要将其设置为 GBP (\xc2\xa3)。我知道有一个formatLocale
可以设置的 Vega-lite,但我不知道如何将我需要的值传递给 Vega-lite。我在 Altair 文档中找不到有关区域设置的任何内容。
def chart_tenders_monthly_value(dataframe=None):\n chart = (\n alt.Chart(dataframe, title="Tender value")\n .mark_bar()\n .encode(\n alt.X(\n "yearmonth(date):O",\n axis=alt.Axis(title="Month") \n ),\n alt.Y("total_monthly_value:Q",\n axis=alt.Axis(title="Monthly cumulative tender value (\xc2\xa3)") \n ),\n tooltip=[\n alt.Tooltip(\'total_monthly_value:Q\', title="Total value", format="$,.4r"), \n alt.Tooltip(\'median_monthly_value:Q\', title="Median value", format="$,.4r"),\n alt.Tooltip(\'no_of_tenders:Q\', title="Total tenders", format=",.2r")\n ],\n color = \'variable:N\'\n )\n )\n\n text = (\n chart.mark_text(align="center", baseline="bottom")\n .encode(text=\'label:N\')\n .transform_calculate(label=f\'format(datum.total_monthly_value,"$,.3s")\')\n )\n return chart+text\n
Run Code Online (Sandbox Code Playgroud)\n\n\n 我可以通过执行以下操作来创建堆积条形图:
import pandas as pd
import numpy as np
import altair as alt
data = {'First': {('Header-1', 'H1-A'): 'Red',
('Header-1', 'H1-B'): 'Red',
('Header-1', 'H1-C'): 'Red',
('Header-2', 'H2-A'): 'White',
('Header-2', 'H2-B'): 'White',
('Header-2', 'H2-C'): 'Yellow',
('Header-3', 'H3-A'): 'Red',
('Header-3', 'H3-B'): 'White',
('Header-3', 'H3-C'): 'White',
('Header-3', 'H3-D'): 'Yellow'},
'Second': {('Header-1', 'H1-A'): 'White',
('Header-1', 'H1-B'): 'Yellow',
('Header-1', 'H1-C'): 'Yellow',
('Header-2', 'H2-A'): 'Yellow',
('Header-2', 'H2-B'): 'Green',
('Header-2', 'H2-C'): 'Green',
('Header-3', 'H3-A'): 'Green',
('Header-3', 'H3-B'): 'Red',
('Header-3', 'H3-C'): 'Red',
('Header-3', 'H3-D'): 'White'},
'Third': {('Header-1', 'H1-A'): …
Run Code Online (Sandbox Code Playgroud) 我能够在鼠标悬停时生成带有工具提示的世界等值线地图,然后单击选择以将未选择的国家/地区变灰。我希望能够点击一个国家并让地图平移和放大。我已经弄清楚如何为每个国家使用经度/纬度质心并手动更改比例/经度/纬度以进行平移和缩放,但我我正在努力如何通过选择方法更改 lon/lat,以便用户可以单击国家/地区进行放大(此外,我需要能够以某种方式放大回默认的完整视图)。是否有可能以某种方式使用transform_calculate
或某些东西为 lon/lat 赋值?谢谢。(地图数据来自naturalearthdata)
import altair as alt
import geopandas as gpd
import json
import numpy as np
world_shp = gpd.read_file('data_world/ne_110m_admin_0_countries.shp')[['ADMIN', 'geometry']]
world_shp.rename(columns={'ADMIN': 'Country'}, inplace=True)
world_shp = world_shp.drop(159) # remove antarctica
world_shp.sort_values(by='Country')
# Add centroids
world_shp['centroid_lon'] = world_shp['geometry'].centroid.x
world_shp['centroid_lat'] = world_shp['geometry'].centroid.y
# Add column to map_df of some dummy data to plot
dummy = np.random.randint(10, 1000, len(world_shp))
world_shp['Cases'] = dummy
# Setup data
world_json = json.loads(world_shp.to_json())
world_data = alt.Data(values=world_json['features'])
# Plotting:
selection = alt.selection_single(fields=['properties.Cases'])
color = …
Run Code Online (Sandbox Code Playgroud) 假设我有以下数据框:
我检查了文档,但它仅基于单列。
可重现的代码:
x = np.random.normal(100,5,100)
data = pd.DataFrame(x)
epsilon = 10
data.columns = ['x']
data['lower'] = x - epsilon
data['upper'] = x + epsilon
data
Run Code Online (Sandbox Code Playgroud)
我实际上喜欢使用 altair,因为我喜欢它的交互性。
我正在尝试使用以下数据df_roc
来使用 Altair 绘制 ROC 曲线:
Threshold TPR FPR
0 0.1 1.000000 0.941176
1 0.2 1.000000 0.705882
2 0.3 0.923077 0.588235
3 0.4 0.846154 0.470588
4 0.5 0.692308 0.352941
5 0.6 0.615385 0.235294
6 0.7 0.461538 0.117647
7 0.8 0.307692 0.058824
8 0.9 0.076923 0.000000
Run Code Online (Sandbox Code Playgroud)
这是我尝试使用的代码来制作交互式绘图:
base = alt.Chart(df_roc,
title='ROC Curve of KNN'
).properties(width=300)
roc_curve = base.mark_line(point=True).encode(
alt.X('fpr', title='False Positive Rate (FPR)', sort=None),
alt.Y('tpr', title='True Positive Rate (TPR) (a.k.a Recall)'),
)
roc_rule = base.mark_line(color='green').encode(
x='fpr',
y='fpr',
size=alt.value(2)
)
(roc_curve …
Run Code Online (Sandbox Code Playgroud) 我有一个 Streamlit 仪表板,可让我使用 Altair 图以交互方式探索 t-SNE 嵌入。我试图弄清楚如何访问所选数据的元数据,以便我可以可视化相应的图像。换句话说,给定:
selector = alt.selection_single()
chart = (
alt.Chart(df)
.mark_circle()
.encode(x="tSNE_dim1", y="tSNE_dim2", color="predicted class", tooltip=["image url", "predicted class"])
.add_selection(selector)
)
Run Code Online (Sandbox Code Playgroud)
...有类似的东西吗
selected_metadata = selector.tooltip
update_dashboard_img(img=selected_metadata["image url"], caption=selected_metadata["predicted class"])
Run Code Online (Sandbox Code Playgroud)
我知道图像标记,但图像位于 S3 上,并且图像太多,无法融入情节中。
对于下面的示例,我想使用绿色配色方案进行导出,使用红色方案进行导入。当我单独创建图表时,一切都很好,他们得到了我分配给他们的配色方案。然而,当我连接图表时,它们都得到了红色方案。
import pandas as pd
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ["2015", "2016", "2017"]
exports = {'fruits' : fruits,
'2015' : [2, 1, 4, 3, 2, 4],
'2016' : [5, 3, 4, 2, 4, 6],
'2017' : [3, 2, 4, 4, 5, 3]}
imports = {'fruits' : fruits,
'2015' : [-1, 0, -1, -3, -2, -1],
'2016' : [-2, -1, -3, -1, -2, -2],
'2017' : [-1, -2, -1, 0, -2, -2]}
df_exp = pd.DataFrame(exports)
df_imp …
Run Code Online (Sandbox Code Playgroud) altair ×10
python ×7
python-3.x ×2
data-science ×1
dataframe ×1
encoding ×1
geopandas ×1
maps ×1
pandas ×1
vega-lite ×1