Atair Histogram Scale Chart

How to master the visualization library proficiently and deal with difficult problems in the use process?

Basic usage may wish to visit GeeksforGeeks

Difficult problems first search https://stackoverflow.com

Try to use the official documentation: numpy learning access

https://numpy.org/doc/stable/user/index.html

For example:

A question about the use of altair

https://stackoverflow.com/questions/64112919/altair-how-to-change-color-of-line-in-line-chart?rq=3

For the example below, I want to use a green scheme for exports and a red scheme for imports. When I create the charts individually, everything works fine, they get the color scheme I assign to them.

alt

However, when I connect the graphs, both graphs get the red color scheme.

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 = pd.DataFrame(imports)

import altair as alt

cols_year_imp = df_imp.columns[1:]
cols_year_exp = df_exp.columns[1:]

imp = alt.Chart(df_imp).transform_fold(
    list(cols_year_imp)
).mark_bar(
    tooltip=True
).encode(
    x='value:Q',
    y='fruits:N',
    color=alt.Color('key:O', scale=alt.Scale(scheme='reds'))
)

exp = alt.Chart(df_exp).transform_fold(
    list(cols_year_exp)
).mark_bar(
    tooltip=True
).encode(
    x=alt.X('value:Q',title="Export"),
    y='fruits:N',
    color=alt.Color('key:O', scale=alt.Scale(scheme='greens', reverse=True)),
    order=alt.Order('key:O', sort='ascending')
)

# imp | exp
imp
exp
alt.hconcat(imp, exp)

line chart

Consider the following example:

alt

Please pay attention to the definition syntax of the code segment X, Y, add a legend to the layered chart in altair

In the resulting graph, I would like to add a legend showing that the blue line shows the highest temperature and the red line shows the lowest temperature. What's the easiest way to achieve this?

import altair as alt
from vega_datasets import data

df = data.seattle_weather()

temp_max = alt.Chart(df).mark_line(color='blue').encode(
    x='yearmonth(date):T',
    y='max(temp_max)',
)

temp_min = alt.Chart(df).mark_line(color='red').encode(
    x='yearmonth(date):T',
    y='max(temp_min)',
)

temp_max + temp_min

I saw in the solution to the question: Marker layered charts in Altair only add legend if color or size or so is set in code.

Normally categorical columns are used, but this is not possible here since the entire column is being plotted, the labels should be the column names, which are now shown in the y-axis labels:

alt

Do a folding transformation so that variables can be encoded correctly.

import altair as alt
from vega_datasets import data

df = data.seattle_weather()

alt.Chart(df).mark_line().transform_fold(
    fold=['temp_max''temp_min'], 
    as_=['variable''value']
).encode(
    x='yearmonth(date):T',
    y='max(value):Q',
    color='variable:N'
)

Case 2: Line chart of stock

Line chart with stroked point markers

alt

What is confusing is how the "mark_line" points are handled.

In the Vega example, I need to use "point" and then set "filled" to False.

import altair as alt
from vega_datasets import data

source = data.stocks()

alt.Chart(source).mark_line(
    point={
      "filled": False,
      "fill""white"
    }
).encode(
    x='date',
    y='price',
    color='symbol'
)

How would I apply it in Altair? I've found that setting "dot" to true or "{}" adds a dot marker, but am confused on how to get padding to work.

polyline + point

This article is published by mdnice multi-platform

Guess you like

Origin blog.csdn.net/qq_40523298/article/details/131185893