def line_x_y(x, y, x_label="Time", y_label="Value"): data = np.concatenate((x, y)).reshape(2, -1).T df = pd.DataFrame(data, columns=[x_label, y_label]) return (Chart(df).encode(x=x_label, y=(y_label)).mark_line())
""" Multi Series Line Chart ----------------------- This example shows how to make a multi series line chart of the daily closing stock prices for AAPL, AMZN, GOOG, IBM, and MSFT between 2000 and 2010. """ from altair import Chart from vega_datasets import data stocks = data.stocks() chart = Chart(stocks).mark_line().encode(x='date', y='price', color='symbol')
prices_predict = compute_all_y(array, coefficients) MSE2 = compute_mse(prices_actual, prices_predict) if MSE2 > MSE1: coefficients[1] = coefficients[1] - 0.02 prices_predict = compute_all_y(array, coefficients) MSE3 = compute_mse(prices_actual, prices_predict) if MSE3 < MSE1: MSE1 = MSE3 else: MSE1 = MSE2 coefficients[2] = coefficients[2] + 0.01 prices_predict = compute_all_y(array, coefficients) MSE2 = compute_mse(prices_actual, prices_predict) if MSE2 > MSE1: coefficients[2] = coefficients[2] - 0.02 prices_predict = compute_all_y(array, coefficients) MSE3 = compute_mse(prices_actual, prices_predict) if MSE3 < MSE1: MSE1 = MSE3 else: MSE1 = MSE2 MSE_list.append(MSE1) count += 1 attempt.append(count) data = Data(attempt=attempt, MSE_list=MSE_list) chart = Chart(data) mark = chart.mark_point() enc = mark.encode(x='attempt:Q', y='MSE_list:Q', ) enc.display()
def line_xy(data, x_label="Time", y_label="Value"): df = pd.DataFrame(data, columns=[x_label, y_label]) return (Chart(df).encode(x=x_label, y=(y_label)).mark_line())
alt.renderers.enable('default') from altair import Chart, X, Y, Axis, SortField alt.__version__ import altair as alt print(alt.renderers.active) import altair as alt alt.renderers.enable('default') budget = pd.read_csv( "https://github.com/chris1610/pbpython/raw/master/data/mn-budget-detail-2014.csv" ) budget.head() # %matplotlib inline budget_top_10 = budget.sort_values(by='amount', ascending=False)[:10] # alt.renderers.enable('notebook') Chart(budget_top_10).mark_bar().encode(x='detail', y='amount') plt.show() import altair as alt from vega_datasets import data iris = data.iris() alt.Chart(iris).mark_point().encode(x='petalLength', y='petalWidth', color='species') # scipy.optimize #방정식이 의미하는 것 ? x 와 y의 함수적 관계 import numpy as np import matplotlib.pyplot as plt
data.to_json(path_or_buf=json_filename, orient='records', date_format='iso') #data.to_csv(path_or_buf=csv_filename) colors = [ "#67001f", "#b2182b", "#d6604d", "#f4a582", "#fddbc7", "#d1e5f0", "#92c5de", "#4393c3", "#2166ac", "#053061" ] colors = colors[::-1] #d = [0, 12, 24, 36, 48, 60, 72, 84, 96, 108] d = [0, 120] r = Row('dt:T', timeUnit='hours', axis=Axis(title='Hour of day')) c = Column('dt:T', timeUnit='monthdate', axis=Axis(format=u'%b', labels=False, title='Month')) col = Color( 'temp:N', bin=Bin(step=12), scale=Scale(domain=[0, 120], range=colors, clamp=True, zero=True), #scale=Scale(range=colors, domain=[0, 120], zero=True), legend=Legend(title="Temperature", format=u'.0f')) chart = Chart(data).mark_text(applyColorToBackground=True).encode( row=r, column=c, text=Text('blanks'), color=col).configure_scale(textBandWidth=3, bandSize=25) chart.max_rows = 8761 filename = sys.argv[2] + ".html" chart.savechart(filename)
def scatter(): chart = Chart(data.df_0, height=HEIGHT, width=WIDTH).mark_circle().encode(x='name:N', y='y2:Q') return chart.to_json()
""" Simple Scatter Plot ------------------- A simple example of an interactive scatter plot using the well-known iris dataset. """ from altair import Chart from vega_datasets import data iris = data.iris() chart = Chart(iris).mark_point().encode(x='petalWidth', y='petalLength', color='species').interactive()
def data_waterfall(): chart = Chart(data.df_water).mark_bar(color='lightgreen').encode( X('Name', axis=Axis(title='Sample')), Y('Value', axis=Axis(title='Value'))) return chart.to_json()
def data_line(): chart = Chart(data=data.df_list, height=HEIGHT, width=WIDTH).mark_line().encode( X('name', axis=Axis(title='Sample')), Y('data', axis=Axis(title='Value'))) return chart.to_json()
import pandas as pd import numpy as np from altair import Chart, X, Y, SortField, Detail, Axis csv_path = "../data/dropped-frames.csv" df = pd.read_csv(csv_path, parse_dates=["Dropped Frame Start", "Dropped Frame End"], low_memory=False) data = df[['Officer ID', 'Dropped Frame Start', 'Duration', 'FPS', 'Dropped Frames', 'Resolution', 'File Size', 'File Name', 'Frame Range', 'Player Time Range']] data = data.rename(columns={'Dropped Frame Start': 'Timestamp'}) ## Overview Chart(data.sample(100)).configure_axis(gridColor='#ccc').mark_line(interpolate='linear').encode( X(field='Timestamp', type='temporal', timeUnit='yearmonth', axis=Axis(title=' ', ticks=6, labelAngle=0, tickSizeEnd=0, tickSize=0, tickPadding=10)), Y('sum(Duration)', axis=Axis(title='Seconds lost')) ).savechart('test.svg')
def timed(text): ts = time.time() parser.parse(text) te = time.time() result = te - ts return result if __name__ == '__main__': affiliations = pd.read_csv('affiliation.csv') # CSV of affiliations in affiliations = list(affiliations.affiliation) affiliations_len = [(affiliation, len([t for t in nlp(affiliation)])) for affiliation in affiliations] affiliations_len_df = pd.DataFrame(affiliations_len, columns=['affiliation', 'n_token']) affiliations_len_filter_df = affiliations_len_df.sort_values( 'n_token', ascending=True).query("n_token > 10").query("n_token < 50") affiliations_len_filter_df[ 'time'] = affiliations_len_filter_df.affiliation.map( lambda x: timed(x)) runtime_df = affiliations_len_filter_df[[ 'n_token', 'time' ]].groupby('n_token').mean().reset_index() chart = Chart(runtime_df).mark_circle().encode( x=X('n_token', scale=Scale(domain=(10, 50)), title='Number of Tokens'), y=Y('time', scale=Scale(domain=(0.0, 5.0)), title='Time (ms)'), ).configure_facet_cell(strokeWidth=0.0, )
import os.path import datetime import re from altair_saver import save eng = create_engine(os.environ["DBURL"]) last_day = pd.read_sql_query( """ select date_trunc('hour', timestamp) date_bucket, count(distinct sid) from useinfo where timestamp > now() - interval '49 hours' group by date_bucket """, eng, parse_dates=["date_bucket"], ) last_five = pd.read_sql_query( """ select count(distinct sid) from useinfo where timestamp > now() - interval '5 minutes' """, eng, ) last_day["date_bucket"] = last_day.date_bucket - datetime.timedelta(hours=8) hc = (Chart( last_day, title=f"Unique Students Per Hour - Current: {last_five.iloc[0]['count']}", ).mark_area().encode(x="date_bucket", y="count")) hc.save("chart.png")
# on Windows: open the shell as admin then: `pip install vega_datasets altair` # on Unix: `sudo pip install vega_datasets altair` # You might need to reload Atom after installation of dependencies if they are not found import altair as alt from vega_datasets import data iris = data.iris() alt.Chart(iris).mark_point().encode( x='petalLength', y='petalWidth', color='species' ) from altair import Chart cars = data.cars() spec = Chart(cars).mark_point().encode( x='Horsepower', y='Miles_per_Gallon', color='Origin', )
def make_chart(): data = pd.DataFrame({'x': range(10), 'y': range(10)}) return Chart(data).mark_point().encode(x='x', y='y')
def hc_grade(): session_id = os.environ.get("SESSION_ID") selected_course = session.get('selected_course', None) # show all course grades if haven't selected course from dropdown if selected_course == None: HcData = pd.read_sql(db.session.query(Hc).filter_by(user_id=session_id).statement, db.session.bind) final = Chart( data=HcData, height=1000, width=380).mark_bar().encode( X('mean:Q', axis=alt.Axis(title='HC Forum Score'), scale=Scale(domain=(0, 5)) ), alt.Y('name:N', sort=alt.EncodingSortField(field= "mean", op="sum", order = "descending") ,axis=alt.Axis(title=None) ), color='course:N')#.interactive() else: # query data df = grade_calculations.hc_grade_over_time(session_id, selected_course) longdata = df.melt('Date', var_name='course', value_name='grade') data = longdata[longdata['grade'].notnull()] def getBaseChart(): """ Creates a chart by encoding the Data along the X positional axis and rolling mean along the Y positional axis """ base = ( alt.Chart(data) .encode( x=alt.X( "Date:T", axis=alt.Axis(title=None, format=("%b %Y"), labelAngle=0), ), y=alt.Y( "grade:Q", axis=alt.Axis(title=None), scale=Scale(domain=(0, 5)) ), color=alt.Color('course:N', legend=None) ).properties(width=400, height=336) ) return base def getSelection(): """ This function creates a selection element and uses it to conditionally set a color for a categorical variable (course). It return both the single selection as well as the Category for Color choice set based on selection. """ radio_select = alt.selection_multi( fields=["course"], name="Course", ) course_color_condition = alt.condition( radio_select, alt.Color("course:N", legend=None), alt.value("lightgrey") ) return radio_select, course_color_condition def createChart(): """ This function uses the "base" encoding chart to create a line chart. The highlight_course variable uses the mark_line function to create a line chart out of the encoding. The color of the line is set using the conditional color set for the categorical variable using the selection. The chart is bound to the selection using add_selection. It also creates a selector element of a vertical array of circles so that the user can select between courses. """ radio_select, course_color_condition = getSelection() make_selector = ( alt.Chart(data) .mark_circle(size=220) .encode( y=alt.Y("course:N", title="Click on circle"), color=course_color_condition ).add_selection(radio_select) ) base = getBaseChart() highlight_course = ( base.mark_line(strokeWidth=2) .add_selection(radio_select) .encode(color=course_color_condition, opacity=alt.condition(radio_select, alt.value(1.0), alt.value(0.2))) ).properties(title="Rolling Weighted Average of Cornerstone Courses") return base, make_selector, highlight_course, radio_select def createTooltip(base, radio_select): """ This function uses the "base" encoding chart and the selection captured. Four elements related to selection are created here """ # Create a selection that chooses the nearest point & selects based on x-value nearest = alt.selection( type="single", nearest=True, on="mouseover", fields=["Date"], empty="none" ) # Transparent selectors across the chart. This is what tells us # the x-value of the cursor selectors = ( alt.Chart(data) .mark_point() .encode( x="Date:T", opacity=alt.value(0), ).add_selection(nearest) ) # Draw points on the line, and highlight based on selection points = base.mark_point().encode( color=alt.Color("course:N", legend=None), opacity=alt.condition(nearest, alt.value(1), alt.value(0)) ).transform_filter(radio_select) # Draw text labels near the points, and highlight based on selection tooltip_text = base.mark_text( align="left", dx=5, dy=-5, fontSize=12 # fontWeight="bold" ).encode( text=alt.condition( nearest, alt.Text("grade:Q", format=".2f"), alt.value(" "), ), ).transform_filter(radio_select) # Draw a rule at the location of the selection rules = ( alt.Chart(data) .mark_rule(color="black", strokeWidth=1) .encode( x="Date:T", ).transform_filter(nearest) ) return selectors, rules, points, tooltip_text base, make_selector, highlight_course, radio_select = createChart() selectors, rules, points, tooltip_text = createTooltip(base, radio_select) # Bring all the layers together with layering and concatenation final = (make_selector | alt.layer(highlight_course, selectors, points, rules, tooltip_text)) return final.to_json()
""" Simple Bar Chart ================ This example shows a basic bar chart created with Altair. """ # category: basic charts from altair import Chart import pandas as pd data = pd.DataFrame({ 'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'], 'b': [28, 55, 43, 91, 81, 53, 19, 87, 52] }) chart = Chart(data).mark_bar().encode(x='a', y='b')
def scatter(): chart = (Chart(sample_data.df_0, height=HEIGHT, width=WIDTH).mark_circle().encode( x="name:N", y="y2:Q").interactive()) return chart.to_json()
def chartLog(): "Display chart for selected log" db_folder = app.config['UPLOAD_FOLDER'] logFiles = glob.glob('%s/*.db' % db_folder) form = ChartLog() form.logFile.choices = [(f, f) for f in logFiles] form.chartId.choices = [(q['id'], q['id']) for q in queries.graphs] try: dbname = app.dbname if os.path.exists(dbname): form.logFile.data = dbname except: pass if not form.validate_on_submit(): return render_template('chartLog.html', chart={}, dbName=None, form=form) dbname = os.path.join(form.logFile.data) if not os.path.exists(dbname): flash('Database does not exist', 'error') return render_template('error.html', title='Database error') try: conn = sqlite3.connect(dbname) except Exception as e: app.logger.error(traceback.format_exc()) flash('Error: %s' % (str(e)), 'error') return render_template('error.html', title='Error in database reporting') chartId = form.chartId.data charts = [q for q in queries.graphs if q['id'] == chartId] if not charts: flash("Error: logic error couldn't find chartId", 'error') return render_template( 'error.html', title='Error in in configuration of chart reports') q = charts[0] app.logger.debug("running chart query: %s - %s" % (q['title'], q['sql'])) start = datetime.now() try: df = pd.read_sql_query(q['sql'], conn) except Exception as e: flash('Error: %s' % (str(e)), 'error') return render_template('error.html', title='Error in database reporting') end = datetime.now() delta = end - start if q['graph_type'] == 'line': chart = Chart(data=df, height=HEIGHT, width=WIDTH).mark_line().encode( X(q['x']['field'], axis=Axis(title=q['x']['title'], labelOverlap='greedy')), Y(q['y']['field'], axis=Axis(title=q['y']['title']))) else: chart = Chart(data=df, height=HEIGHT, width=WIDTH).mark_bar().encode( X(q['x']['field'], axis=Axis(title=q['x']['title'], labelOverlap='greedy')), Y(q['y']['field'], axis=Axis(title=q['y']['title']))) data = { 'id': "chart", 'data': chart.to_json(), 'title': q['title'], 'explanation': q['explanation'], 'sql': q['sql'], 'time_taken': str(delta) } return render_template('chartLog.html', chart=data, dbName=dbname, form=form)