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

What are the basic customizations of Matplotlib

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

Share

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

This article is to share with you about the basic customization of Matplotlib. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

In the Matplotlib tutorial, we will discuss some possible chart customizations. In order to start modifying subgraphs, we must define them. We'll talk about them soon, but there are two main ways to define and construct subgraphs. For now, we only use one of them, but we will explain them soon.

Now, modify our graph_data function:

Def graph_data (stock): fig = plt.figure () ax1 = plt.subplot2grid ((1)), (0)) 1234

In order to modify the diagram, we need to reference it, so we store it in the variable fig. Then we define ax1 as a subgraph on the diagram. We use subplot2grid here, which is one of the two main ways to get a subgraph. We will discuss these things in depth, but by now, you should see that we have 2 tuples that provide (1) and (0). 1 1 indicates that this is a 1 × 1 grid. Then zero zero indicates that the "starting point" of this subgraph will be zero zero.

Next, we will get and parse the data through the code we have written:

Stock_price_url = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote; Range=10y/csv' source_code = urllib.request.urlopen (stock_price_url). Read (). Decode () stock_data = [] split_source = source_code.split ('n') for line in split_source: split_line = line.split (',') if len (split_line) = 6: if 'values' not in line and' labels' not in line: stock_data.append (line) date, closep, highp, lowp, openp Volume = np.loadtxt (stock_data, delimiter=',', unpack=True, converters= {0: bytespdate2num ('% Y% m% d')}) 1234567891011121314

Next, we draw the data as follows:

Ax1.plot_date (date, closep,'-', label='Price') 1

Now, since we are drawing the date, we may find that if we zoom in, the date will move horizontally. However, we can customize these scale labels, like this:

For label in ax1.xaxis.get_ticklabels (): label.set_rotation (45) 12

This rotates the label diagonally. Next, we can add a grid:

Ax1.grid (True) 1

Then, we leave everything else by default, but we may also need to adjust the drawing slightly because the date is out of the chart. Not only can we configure charts as configure subplots buttons, we can also configure them in the code. Here's how we set these parameters:

Plt.subplots_adjust (left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2, hspace=0) 1

Now, to prevent us from leaving you somewhere, here is the complete code:

Import matplotlib.pyplot as plt import numpy as np import urllib import matplotlib.dates as mdates def bytespdate2num (fmt, encoding='utf-8'): strconverter = mdates.strpdate2num (fmt) def bytesconverter (b): s = b.decode (encoding) return strconverter (s) return bytesconverter def graph_data (stock): fig = plt.figure () ax1 = plt.subplot2grid (0pc0) stock_price_url = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata; Type=quote Range=10y/csv' source_code = urllib.request.urlopen (stock_price_url). Read (). Decode () stock_data = [] split_source = source_code.split ('\ n') for line in split_source: split_line = line.split (' ') if len (split_line) = 6: if' values' not in line and 'labels' not in line: stock_data.append (line) date, closep, highp, lowp, openp, volume = np.loadtxt (stock_data, delimiter=',' Unpack=True, converters= {0: bytespdate2num ('% Y% m% d')}) ax1.plot_date (date, closep,'-' Label='Price') for label in ax1.xaxis.get_ticklabels (): label.set_rotation (45) ax1.grid (True) #, color='g', linestyle='-', linewidth=5) plt.xlabel ('Date') plt.ylabel (' Price') plt.title ('Interesting Graph\ nCheck it out') plt.legend () plt.subplots_adjust (left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2 Hspace=0) plt.show () graph_data ('TSLA') 12345678910111213141516171819202122232425262829303132333435363738394041424344454647

The result is:

Thank you for reading! This is the end of this article on "what are the basic customizations of Matplotlib?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can 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

Internet Technology

Wechat

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

12
Report