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 plotly to draw partial magnification of python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to use plotly to draw partial magnification of python. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Final effect display

Realization idea

Insert an embedded map in the drawing area, which is consistent with the painting of the original picture, achieve the effect of scaling by limiting the display range of the x-axis and y-axis of the embedded image, and draw a rectangular box on the original image to highlight the scaled area. finally, the zoom relationship is highlighted by two straight lines.

Import library import plotly.io as pioimport plotly.graph_objects as goimport pandas as pdimport numpy as np# to set plotly default theme, white theme pio.templates.default = 'plotly_white' randomly generate some data # x coordinates x = np.arange (1, 1001) # generate y-axis data And add random fluctuation y1 = np.log (x) indexs = np.random.randint (0,800) for index in indexs: Y1 [index] + = np.random.rand ()-0.5y1 = y1 + 0.2y2 = np.log (x) indexs = np.random.randint (0, 1000, 800) for index in indexs: Y2 [index] + = np.random.rand ()-0.5y3 = np.log (x) indexs = np.random.randint (0, 1000) For index in indexs: Y3 [index] + = np.random.rand ()-0.5y3 = y3-0.2.Encapsulation drawing code class LocalZoomPlot: def _ init__ (self, x, y, colors, x_range, scale=0.): "": param x: X axis coordinates List type: param y: y axis coordinates, 2D list type, for example [y1, y2, y3]: param colors: the color of each curve Must be equal to len (y): param x_range: the x-axis range of the area to be scaled: param scale: see the getRangeMinMaxValue function "" self.x = x self.y = y self.colors = colors self.x_range = x_range self.y_range = self.getRangeMinMaxValue (x_range, scale) def getRangeMinMaxValue (self, x_range) for details Scale=0.): "gets the specified x-axis range Maximum and minimum values of all y data: param x_range: expected local magnification of the x-axis range: param scale: extend the maximum and minimum values to both sides by a certain distance "" min_value = np.min ([np.min (arr [x _ range [0]: x_range [1]]) for arr in self.y]) max_value = np. Max ([np.max [x _ range [0]: x_range [1]]) for arr in self.y]) # scale min_value = min_value-(max_value-min_value) * scale max_value = max_value + (max_value-min_value) * scale # returns the scaled result return min_value Max_value def originPlot (self, fig, * * kwargs): "draw initial line chart based on y data: param fig: go.Figure instance" fig.add_traces ([go.Scatter (x=self.x, y=arr, opacity=0.7, marker_color=self.colors [I], * * kwargs) for I " Arr in enumerate (self.y)]) return fig def insetPlot (self, fig, inset_axes): "" insert the embedded image on the original image: param fig: go.Figure object instance: param inset_axes: position and size of the embedded image [x-axis position of the lower left corner, y-axis position of the lower left corner, width Height] all coordinates are absolute (between 01s) "#" # use the embedded graph parameters in the creation subgraph Create an embedded graph fig = fig.set_subplots (insets= [dict (type='xy', l=inset_axes [0], b=inset_axes [1], w=inset_axes [2], h=inset_axes [3],)]) # the embedded image is consistent with the painting of the original drawing You need to specify xaxis and yaxis parameters to ensure that fig = self.originPlot (fig, xaxis='x2', yaxis='y2', showlegend=False) # limits the axis range of the embedded graph to the specified range fig.update_layout (xaxis2=dict (range=self.x_range), yaxis2=dict (range=self.y_range)) return fig def rectOriginArea (self) Fig): "frame the enlarged area: param fig: go.Figure instance" fig.add_trace (go.Scatter (# from the upper left corner Connect clockwise x=np.array (self.x_range) [[0,1,1,0,0]], y=np.array (self.y_range) [[1,1,0,0,1]], mode='lines', line= {'color':' # 737473, 'dash':' dash', 'width': 3} Showlegend=False) return fig def addConnectLine (self, fig, area_point_num, point): "" specify a point connection from the magnified area: param fig: go.Figure instance: param area_point_num: the anchor point of the magnified area For example, (0,0) represents the lower left corner coordinates of the enlarged region, (0,1) represents the upper left corner coordinates, (1,0) represents the lower right corner coordinates, and (1,1) represents the upper right corner coordinates. Only these four cases can be taken: param point: another point to be connected, usually located near the embedded map. Specify "" fig.add_shape (type='line', x0roomself.xrange [area _ point_num [0]], y0roomself.y _ range [area _ point_num [1]], x1=point [0], y1=point [1], line= {'color':' # 737473, 'dash':' dash', 'range 1}) according to the degree of beauty ) return fig begins to draw plot = LocalZoomPlot (x, [y1, y2, y3], ['# f0bc94percent,'# 7fe2b3regions,'# cba0e6'], (100,150), 0.) fig = go.Figure () fig = plot.originPlot (fig) fig = plot.insetPlot (fig, (0.4,0.2,0.4,0.3) fig = plot.rectOriginArea (fig) fig = plot.addConnectLine (fig, (0,0), (420) ) fig = plot.addConnectLine (fig, (1,1), (900,2.7)) # additional settings fig.update_layout (width=800, height=600, xaxis=dict (rangemode='tozero', showgrid=False, zeroline=False,), xaxis2=dict (showgrid=False, zeroline=False) ) fig.show (), this is the end of the article on "how to draw partial magnification in python with plotly". Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

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