点击下方卡片,关注“小白玩转Python”公众号
import holoviews as hvfrom holoviews import optsimport pandas as pdimport numpy as nphv.extension('bokeh')# Sample matrix representing the export volumes between 5 countriesexport_data = np.array([[0, 50, 30, 20, 10],[10, 0, 40, 30, 20],[20, 10, 0, 35, 25],[30, 20, 10, 0, 40],[25, 15, 30, 20, 0]])labels = ['USA', 'China', 'Germany', 'Japan', 'India']# Creating a pandas DataFramedf = pd.DataFrame(export_data, index=labels, columns=labels)df = df.stack().reset_index()df.columns = ['source', 'target', 'value']# Creating a Chord objectchord = hv.Chord(df)# Styling the Chord diagramchord.opts(opts.Chord(cmap='Category20', edge_cmap='Category20',labels='source', label_text_font_size='10pt',edge_color='source', node_color='index',width=700, height=700)).select(value=(5, None))# Display the plotchord
import plotly.express as pximport numpy as npdf = px.data.gapminder().query("year == 2007")fig = px.sunburst(df, path=['continent', 'country'],values='pop',color='lifeExp',hover_data=['iso_alpha'],color_continuous_scale='RdBu',color_continuous_midpoint=np.average(df['lifeExp'], weights=df['pop']))fig.show()
import numpy as npimport matplotlib.pyplot as pltfrom mplhexbin import HexBin# Simulated datanp.random.seed(0) # Ensure reproducibilityn_points = 10000x = np.random.rand(n_points) * 100 # Air Quality Index (AQI) range from 0 to 100y = 5 * np.sin(x * np.pi / 50) + np.random.randn(n_points) * 15 # Simulated hospital visits, related to AQI but with noise# Create a new figurefig, ax = plt.subplots(figsize=(10, 8))# Use HexBin to create a hexagonal bin plothb = HexBin(ax, gridsize=20, cmap='viridis', extent=[0, 100, -30, 50]) # Set grid size, colormap, and rangehb.hexbin(x, y, mincnt=1) # Draw the hexagonal bin plot, mincnt sets the minimum count threshold# Add title and axis labelsax.set_title('Relationship between Air Quality Index (AQI) and Hospital Visits')ax.set_xlabel('Air Quality Index (AQI)')ax.set_ylabel('Hospital Visits')# Show the figureplt.colorbar(hb.cmap, ax=ax, label='Number of Data Points') # Add color bar and set labelplt.show()
import plotly.graph_objects as golabels = ["Coal", "Solar", "Wind", "Nuclear", "Residential", "Industrial", "Commercial"]source = [0, 1, 2, 3, 0, 1, 2, 3]target = [4, 4, 4, 4, 5, 5, 5, 5]value = [25, 10, 40, 20, 30, 15, 25, 35]# Create the Sankey diagram objectfig = go.Figure(data=[go.Sankey(node=dict(pad=15,thickness=20,line=dict(color="black", width=0.5),label=labels),link=dict(source=source,target=target,value=value))])fig.update_layout(title_text="Energy Flow in Model City", font_size=12)fig.show()
import altair as altfrom vega_datasets import datasource = data.unemployment_across_industries.urlalt.Chart(source).mark_area().encode(alt.X('yearmonth(date):T',axis=alt.Axis(format='%Y', domain=False, tickSize=0)),alt.Y('sum(count):Q', stack='center', axis=None),alt.Color('series:N',scale=alt.Scale(scheme='category20b'))).interactive()