How easy is it to draw a multi-factor histogram in Python?

Background introduction

As two open-source and easy-to-use data analysis and graphing tools, R and Python are widely used in scientific research.

In the days that followed, we wrote a series of tweets around the topic of graphing and data analysis in Python, and in fact shared some of my own study notes, for my sake

Easy to read and easy for everyone to read.

Software introduction

[Software Name]: Anaconda | Spyder

[Software installation]: You can refer to this tweet below

Download and installation of Anaconda

drawing tutorial

1. Open the Spyder software, we import the corresponding library, and generate a set of data. (When you start drawing, don't think too much, just follow the code)

Python学习交流Q群:903971231###
# 导入相应的库(包)
import numpy as np     # 生成数据的包
import matplotlib.pyplot as plt    #作图的包

# 生成一组数据
x = np.arange(5)
y = [1, 5, 2, 3, 7]
y1 = [4, 6, 3, 5, 9]

2. Check the value

print(x,y,y1)

insert image description here

3. Start drawing, the following is the drawing code, tap it to find the feeling, at this time the drawing has been formed

Python学习交流Q群:906715085####
plt.bar(# 设置x和y
        x,y,   

        # 设置柱子宽度 
       width=0.3,        
        
        # 设置柱子颜色
       color = "red",         
        
        # 设置legend的名称
       label = "y")    

plt.bar(x+0.3,y1,
        width = 0.3,
        color = "green",
        label = "y1")

# 设置x轴tick的位置
plt.xticks(x+0.3/2,x) 

# 显示legend  
plt.legend()            

# 设置x的标签
plt.xlabel(# x标签的名称
          "x_names",   
           
           # x标签的字体大小
          size = 12,   
           
           # x标签的字体颜色
          color = "blue") 

plt.ylabel("y_names",
           size = 12,
           color = "blue")

# y轴的范围(x轴的范围只需要将y变为x即可)
plt.ylim(0,10)

# 显示图形
plt.show()

insert image description here

4. Currently doing two-column factors, what if we have three, or more? Below we assume that there are three data, after changing the corresponding parameters, the plot is as follows

# 增加一列数据
y2 = [5,3,7,9,6]

plt.bar(x,y,
        width=0.3,
        color = "red",
        label = "y")
plt.bar(x+0.3,y1,
        width = 0.3,
        color = "green",
        label = "y1")

# 新增绘图部分
plt.bar(x+0.6,y2,
        width = 0.3,
        color = "blue",
        label = "y1")

# 需要更改的部分
plt.xticks(x+0.6/2,x) 
plt.legend()         

plt.xlabel("x_names", 
           size = 12,
           color = "blue") 

plt.ylabel("y_names",
           size = 12,
           color = "blue")
plt.ylim(0,10)
plt.show()

insert image description here

5. Using the hatch parameter, you can fill the column such as: hatch = "/", and there are other fill shapes, such as '/', '', '|', '-', '+', 'x', ' o', 'O', '.', '*', for example, we use this parameter to fill the column to see the effect

plt.bar(x,y,
        width=0.28,
        color = "red",
        label = "y",
        # 填充形状
       hatch = "//")
plt.bar(x+0.3,y1,
        width = 0.28,
        color = "green",
        label = "y1",
        # 填充形状
       hatch = ".")

plt.bar(x+0.6,y2,
        width = 0.28,
        color = "blue",
        label = "y1",
        # 填充形状
       hatch = "-")

plt.xticks(x+0.6/2,x) 
plt.legend()         

plt.xlabel("x_names", 
           size = 12,
           color = "blue") 

plt.ylabel("y_names",
           size = 12,
           color = "blue")
plt.ylim(0,10)
plt.show()

insert image description here

At last

Well, I will introduce so much today. Students who are willing to learn Python drawing can type in the code, follow my study notes, learn little by little, and strive for an early date.

learn! come on!

Guess you like

Origin blog.csdn.net/xff123456_/article/details/124453614