python-matplotlibHow to chart candlestick
Usage example
import matplotlib.pyplot as plt
#create figure
plt.figure()
#define width of candlestick elements
width = .4
width2 = .05
#define up and down prices
up = prices[prices.close>=prices.open]
down = prices[prices.close<prices.open]
#define colors to use
col1 = 'green'
col2 = 'red'
#plot up prices
plt.bar(up.index,up.close-up.open,width,bottom=up.open,color=col1)
plt.bar(up.index,up.high-up.close,width2,bottom=up.close,color=col1)
plt.bar(up.index,up.low-up.open,width2,bottom=up.open,color=col1)
#plot down prices
plt.bar(down.index,down.close-down.open,width,bottom=down.open,color=col2)
plt.bar(down.index,down.high-down.open,width2,bottom=down.open,color=col2)
plt.bar(down.index,down.low-down.close,width2,bottom=down.close,color=col2)
#rotate x-axis tick labels
plt.xticks(rotation=45, ha='right')
#display candlestick chart
plt.show()
More of Python Matplotlib
- Errorbar usage example
- How to plot heatmap with values
- How to plot multiple lines on the same chart
- How to add legend to boxplot
- How to set chart opacity
- How to set subplot size
- How to change figure size
- How to add second Y axis
- How to install GeoPandas module
- How to plot multiple boxplots
See more codes...