Matplotlib 绘图分析代码示例——Python中数据可视化库

/ Python / 没有评论 / 205浏览

折线图

plot—画图

import pandas
import matplotlib.pyplot as plt
A = pandas.read_csv("test1.csv")
A["Date"] = pandas.to_datetime(A["Date"])
twelve = A[0: 12]
plt.plot(twelve["Date"], twelve["Values1"])
plt.show()

xticks—X轴旋转

import pandas
import matplotlib.pyplot as plt
A = pandas.read_csv("test1.csv")
A["Date"] = pandas.to_datetime(A["Date"])
twelve = A[0: 12]
plt.plot(twelve["Date"], twelve["Values1"])
plt.xticks(rotation=45)
plt.show()

tick_params—去掉锯齿

import pandas
import matplotlib.pyplot as plt
A = pandas.read_csv("test1.csv")
A["Date"] = pandas.to_datetime(A["Date"])
twelve = A[0: 12]
plt.plot(twelve["Date"], twelve["Values1"])
plt.xticks(rotation=45)
plt.tick_params(bottom="off", top = "off", left = "off", right = "off")
plt.show()

label,title—坐标轴与标题

import pandas
import matplotlib.pyplot as plt
A = pandas.read_csv("test1.csv")
A["Date"] = pandas.to_datetime(A["Date"])
twelve = A[0: 12]
plt.plot(twelve["Date"], twelve["Values1"])
plt.xticks(rotation=45)
plt.xlabel("month")
plt.ylabel("values")
plt.title("Statistics")
plt.show()

figure—指定图区域

import pandas
import matplotlib.pyplot as plt
A = pandas.read_csv("test1.csv")
A["Date"] = pandas.to_datetime(A["Date"])
fig = plt.figure(figsize=(20, 6))
plt.plot()
plt.show()

add_subplot—添加子图

import pandas
import matplotlib.pyplot as plt
A = pandas.read_csv("test1.csv")
A["Date"] = pandas.to_datetime(A["Date"])
fig = plt.figure(figsize=(20, 6))
picture1 = fig.add_subplot(2,2,1)
picture2 = fig.add_subplot(2,2,2)
picture3 = fig.add_subplot(2,2,4)
plt.show()

legend—指定图例

import pandas
import numpy
import matplotlib.pyplot as plt
A = pandas.read_csv("test1.csv")
A["Date"] = pandas.to_datetime(A["Date"])
twelve = A[0: 12]
plt.plot(twelve["Date"], twelve["Values"], label="values")
plt.plot(twelve["Date"], twelve["Values1"], label="values1")
plt.xticks(rotation=45)
plt.legend(loc="upper left")
plt.show()

c——颜色

import pandas
import numpy
import matplotlib.pyplot as plt
A = pandas.read_csv("test1.csv")
A["Date"] = pandas.to_datetime(A["Date"])
twelve = A[0: 12]
color = (210/255, 23/255, 68/255)
plt.plot(twelve["Date"], twelve["Values"], c = color, label="values")
plt.plot(twelve["Date"], twelve["Values1"], c = "blue", label="values1")
plt.xticks(rotation=45)
plt.legend(loc="upper left")
plt.show()

linewidth—折线宽度

import pandas
import numpy
import matplotlib.pyplot as plt
A = pandas.read_csv("test1.csv")
A["Date"] = pandas.to_datetime(A["Date"])
twelve = A[0: 12]
color = (210/255, 23/255, 68/255)
plt.plot(twelve["Date"], twelve["Values"], c = color, label="values", linewidth = 10)
plt.plot(twelve["Date"], twelve["Values1"], c = "blue", label="values1", linewidth = 5)
plt.xticks(rotation=45)
plt.legend(loc="upper left")
plt.show()

text—在指定位置添加文字

import pandas
import numpy
import matplotlib.pyplot as plt
A = pandas.read_csv("test1.csv")
A["Date"] = pandas.to_datetime(A["Date"])
twelve = A[0: 12]
color = (210/255, 23/255, 68/255)
plt.plot(twelve["Date"], twelve["Values"], c = color, label="values", linewidth = 10)
plt.plot(twelve["Date"], twelve["Values1"], c = "blue", label="values1", linewidth = 5)
plt.xticks(rotation=45)
plt.legend(loc="upper left")
plt.text("2017-03-27",150, "Word")
plt.show()

柱状图

bar—画柱状图

import pandas
import numpy
import matplotlib.pyplot as plt
A = pandas.read_csv("test1.csv")
A["Date"] = pandas.to_datetime(A["Date"])
bar = A["Values"].values
position = numpy.arange(5)+ 1
fig, picture = plt.subplots()
picture.bar(position, bar, 0.3)
plt.show()

barh—画横向条形图

import pandas
import numpy
import matplotlib.pyplot as plt
A = pandas.read_csv("test1.csv")
A["Date"] = pandas.to_datetime(A["Date"])
bar = A["Values"].values
position = numpy.arange(5)+ 1
fig, picture = plt.subplots()
picture.barh(position, bar, 0.3)
plt.show()

散裂图

scatter

import pandas
import numpy
import matplotlib.pyplot as plt
A = pandas.read_csv("test1.csv")
A["Date"] = pandas.to_datetime(A["Date"])
date = A["Date"].values
values = A["Values"].values
fig, picture = plt.subplots()
picture.scatter(date, values, 30)
plt.xticks(rotation=45)
plt.show()

条形图

hist—画条形图

import pandas
from pandas import Series
import matplotlib.pyplot as plt
A = pandas.read_csv("test1.csv")
A["Date"] = pandas.to_datetime(A["Date"])
no_counts = A["No"].value_counts()
no_counts = no_counts.sort_index()
fig, picture = plt.subplots()
picture.hist(no_counts,range=(1,3), bins=20)
plt.show()

set_ylim—设置y轴区间

import pandas
from pandas import Series
import matplotlib.pyplot as plt
A = pandas.read_csv("test1.csv")
A["Date"] = pandas.to_datetime(A["Date"])
no_counts = A["No"].value_counts()
no_counts = no_counts.sort_index()
fig, picture = plt.subplots()
picture.hist(no_counts,range=(1,3), bins=20)
picture.set_ylim(0,1)
plt.show()

盒图

boxplot—画盒图

import numpy
import pandas
import matplotlib.pyplot as plt
A = pandas.read_csv("test1.csv")
columns=["No", "Values2"]
fig, picture = plt.subplots()
picture.boxplot(A[columns].values)
picture.set_xticklabels(columns, rotation=90)
picture.set_ylim(0,40)
plt.show()