Python使用杂记

python画动态可交互图

一般matplotlibseaborn 只能画静态图表,plotly https://plotly.com/python/line-charts/ 可以画可交互式图表。

import plotly.express as px

df = px.data.gapminder().query("continent=='Oceania'")
fig = px.line(df, x="year", y="lifeExp", color='country')
fig.show()

滑动图像

https://plotly.com/python/range-slider/

pandas时间格式列转换


from datetime import datetime
import pandas as pd


tran['date'] = tran['date'].astype('datetime64[ns]')
# 或者
tran['date'] = pd.to_datetime(tran['date']) # date转为时间格式

折线图分段画不同颜色

方法一:
指定c 颜色参数
方法二:
前一段数据置位None


from pandas import read_csv
from matplotlib import pyplot
series = read_csv('sunspots.csv', header=0, index_col=0)
X = series.values
train_size = int(len(X) * 0.66)
train, test = X[0:train_size], X[train_size:len(X)]
print('Observations: %d' % (len(X)))
print('Training Observations: %d' % (len(train)))
print('Testing Observations: %d' % (len(test)))
pyplot.plot(train)
pyplot.plot([None for i in train] + [x for x in test])
pyplot.show()

pandas 统计不同值出现的次数

y.value_counts()

# python 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×