Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to draw dynamic Visual Chart

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

The knowledge of this article "how to use Python to draw dynamic visualization chart" is not understood by most people, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use Python to draw dynamic visualization chart" article.

First, install related modules

For the first step, we need to install the relevant modules, through the pip command.

Pip install gif

In addition, since the gif module will later be used as a decorator on the function of drawing visual charts, we mainly rely on the matplotlib, plotly, and altair modules of Python that draw visual charts, so we also need the following libraries

Pip install "gif [altair]" pip install "gif [matplotlib]" pip install "gif [plotly]" II. The combination of gif and matplotlib

Let's first look at the combination of gif and matplotlib modules. Let's first look at a simple example.

The code is as follows:

Import randomfrom matplotlib import pyplot as pltimport gifx = [random.randint (0100) for _ in range (100)] y = [random.randint (0100) for _ in range (100)] gif.options.matplotlib ["dpi"] = 300@gif.framedef plot (I): xi = x [iTunes 10: (item1) * 10] yi = y [iTunes 10: (iodide 1) * 10] plt.scatter (xi, yi) plt.xlim (0,100) plt.ylim ((0) Frames = [] for i in range (10): frame = plot (I) frames.append (frame) gif.save (frames, 'example.gif', duration=3.5, unit= "s", between= "startend")

Output:

The logic of the code is not difficult to understand, first we need to define a function to draw the chart and take the gif decorator, then we need an empty list, through the for loop to put the drawn objects into this empty list and save it into a file in gif format.

III. The combination of gif and plotly

In addition to being used in conjunction with matplotlib, gif and plotly can also be used together

The code is as follows:

Import randomimport plotly.graph_objects as goimport pandas as pdimport gifdf = pd.DataFrame ({'tress: list (range (10)) * 10, 'xcake: [random.randint (0,100) for _ in range (100)],' yearly: [random.randint (0)] For _ in range (100)]}) @ gif.framedef plot (I): d = df [df [t'] = = I] fig = go.Figure () fig.add_trace (go.Scatter (x / d ["x"], y / d ["y"], mode= "markers")) fig.update_layout (width=500 Height=300) return figframes = [] for i in range (10): frame = plot (I) frames.append (frame) gif.save (frames, 'example_plotly.gif', duration=100)

Output:

The overall code logic is similar to the above, so there is no specific explanation here.

4. Dynamic visualization of matplotlib multi-subgraphs.

The charts drawn above are all done in a single chart, and of course we can also show them dynamically and visually in multiple sub-images.

The code is as follows:

# read data df = pd.read_csv ('weather_hourly_darksky.csv') df = df.rename (columns= {"time": "date"}) @ gif.framedef plot (df, date): df = df.loc [df.index [0]: pd.Timestamp (date)] fig, (ax1, ax2, ax3) = plt.subplots (3, figsize= (10,6), dpi=100) ax1.plot (df.temperature, marker='o', linestyle='--', linewidth=1, markersize=3) Color='g') maxi = round (df.temperature.max () + 3) ax1.set_xlim ([START, END]) ax1.set_ylim ([0, maxi]) ax1.set_ylabel ('TEMPERATURE', color='green') ax2.plot (df.windSpeed, marker='o', linestyle='--', linewidth=1, markersize=3, color='b') maxi = round (df.windSpeed.max () + 3) ax2.set_xlim ([START) END]) ax2.set_ylim ([0, maxi]) ax2.set_ylabel ('WIND', color='blue') ax3.plot (df.visibility, marker='o', linestyle='--', linewidth=1, markersize=3, color='r') maxi = round (df.visibility.max () + 3) ax3.set_xlim ([START, END]) ax3.set_ylim ([0, maxi]) ax3.set_ylabel (' VISIBILITY' Color='red') frames = [] for date in pd.date_range (start=df.index [0], end=df.index [- 1], freq='1M'): frame = plot (df, date) frames.append (frame) gif.save (frames, "file name .gif", duration=0.5, unit='s')

Output:

5. Dynamic bubble diagram

Finally, we use the plotly module to draw a dynamic bubble diagram.

The code is as follows:

Import gifimport plotly.graph_objects as goimport numpy as npnp.random.seed (1) N = 100x = np.random.rand (N) y = np.random.rand (N) colors = np.random.rand (N) sz = np.random.rand (N) * 30layout = go.Layout (xaxis= {'range': [- 2,2]}, yaxis= {' range': [- 2,2]}, margin=dict ) @ gif.framedef plot (I): fig = go.Figure (layout=layout) fig.add_trace (go.Scatter (xonomx [: I], yellowy [: I], mode= "markers", marker=go.scatter.Marker (size=sz [: I], color=colors [: I], opacity=0.6) Colorscale= "Viridis")) fig.update_layout (width=500, height=300) return figframes = [] for i in range: frame = plot (I) frames.append (frame) gif.save (frames, "bubble.gif")

Output:

The above is about the content of this article on "how to draw dynamic visual charts with Python". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report