Matplotlib 如何创建蜡烛图

Matplotlib 如何创建蜡烛图

蜡烛图是一种常用的可视化股市数据的方式。它们展示了给定时间段内某支股票或证券的开盘价、收盘价、最高价和最低价。蜡烛图由一系列垂直的条形图或“蜡烛”组成,其中每个蜡烛代表一段时间。每个蜡烛图的顶部和底部代表该时期内的最高价和最低价,而蜡烛图的实体代表开盘价和收盘价。

在本教程中,我们将探索一些代码,使用Python中流行的数据可视化库Matplotlib为一周的股票价格创建蜡烛图。

我们将使用Pandas库创建一个表示股票价格的DataFrame,然后使用Matplotlib的条形图函数绘制蜡烛图。

要创建蜡烛图,我们使用一种特定的语法,其中涉及使用Matplotlib中的plt.bar方法。

使用plt.bar创建条形图

plt.bar是Matplotlib库中的一个函数,它允许您在Python中创建条形图。它接受多个参数,包括x轴值、y轴值、条形的宽度和颜色。您可以使用此函数创建水平和垂直条形图,并可以自定义条形的外观以适应您的需求。

下面是plt.bar的语法:

plt.bar。

plt.bar(up.index,up.close-up.open,bottom=up.open,color)
The "up" dataframe contains the stock prices where the closing price is greater than or equal to the opening price.

plt.bar(down.index, down.close - down.open, bottom=down.open, color)
The "down" dataframe contains the stock prices where the closing price is less than the opening price.

示例:Matplotlib中的蜡烛图

现在让我们来探索下面展示的代码。这段代码使用Python中的Matplotlib库创建一个蜡烛图,以表示一只股票一周内的开盘价、收盘价、最高价和最低价。

首先,创建一个Pandas DataFrame来存储一周的股票价格。然后,创建两个新的DataFrames – “up”存储收盘价大于等于开盘价的股票价格,”down”存储收盘价小于开盘价的股票价格。

接下来,定义蜡烛图的颜色:”green”代表股票价格增长,”red”代表股票价格下跌。还设置了蜡烛图元素的宽度。

然后,使用plt.bar方法在图表上绘制上涨和下跌的股票价格,其中bottom参数指定每个柱形图的起始点。x轴刻度标签向右旋转45度以提高可读性。最后,为图表添加标题、x轴标签、y轴标签,并使用plt.show()进行显示。

import pandas as pd
import matplotlib.pyplot as plt

# Create a DataFrame to represent opening, closing, high, and low prices
# of a stock for a week
stock_prices = pd.DataFrame({'open': [60, 70, 80, 90, 100, 110, 120],
   'close': [55, 85, 75, 100, 95, 120, 105],
   'high': [70, 95, 85, 110, 105, 120, 125],
   'low': [50, 60, 70, 80, 85, 105, 100]},
   index=pd.date_range("2023-04-01", periods=7, freq="d"))

plt.figure()

# Create a new DataFrame called "up" that stores the stock_prices
# when the closing stock price is greater than or equal to the opening stock price
up = stock_prices[stock_prices.close >= stock_prices.open]

# Create a new DataFrame called "down" that stores the stock_prices
# when the closing stock price is lesser than the opening stock price
down = stock_prices[stock_prices.close < stock_prices.open]

# When the stock prices have decreased, then it
# will be represented by red color candlestick
col1 = 'red'

# When the stock prices have increased, then it
# will be represented by green color candlestick
col2 = 'green'

# Set the width of candlestick elements
width = 0.4
width2 = 0.05

# Plot the up prices of the stock
plt.bar(up.index, up.close-up.open, width, bottom=up.open, color=col2)
plt.bar(up.index, up.high-up.close, width2, bottom=up.close, color=col2)
plt.bar(up.index, up.low-up.open, width2, bottom=up.open, color=col2)

# Plot the down prices of the stock
plt.bar(down.index, down.close-down.open, width, bottom=down.open, color=col1)
plt.bar(down.index, down.high-down.open, width2, bottom=down.open, color=col1)
plt.bar(down.index, down.low-down.close, width2, bottom=down.close, color=col1)

# Rotate the x-axis tick labels at 45 degrees towards right
plt.xticks(rotation=45, ha='right')

# Display the candlestick chart of stock data for a week
plt.title('Stock Prices for a Week')
plt.xlabel('Date')
plt.ylabel('Price (USD)')
plt.show()

输出

执行后,将得到以下蜡烛图:

Matplotlib 如何创建蜡烛图

结论

总之,在Matplotlib中创建一个蜡烛图可以成为可视化股市数据的强大工具。通过使用不同的颜色和宽度,还可以传达关于股票价格随时间变化的额外信息。通过按照本教程中概述的步骤,您可以创建自己的蜡烛图,深入了解市场走势和股票表现。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程