python绘制直方图,并且旋转label标签

更新时间:2023-06-04 13:25:30 阅读: 评论:0

python绘制直⽅图,并且旋转label标签
ax.t_xticklabels(labels,rotation=120) # 旋转标签,避免标签重叠覆盖
label翻转的实现,在输出到页⾯之前,使⽤:fig.autofmt_xdate() 或者 ax.t_xticklabels(group_labels, rotation=120)    rotation就是翻转的⾓度
# -*- coding: gbk -*-
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
def draw_pie(labels,quants):
# make a square figure
plt.figure(1, figsize=(6,6))
# For China, make the piece explode a bit
expl = [0,0.1,0,0,0,0,0,0,0,0]
# Colors ud. Recycle if not enough.
colors  = ["blue","red","coral","green","yellow","orange"]
# Pie Plot
# autopct: format of "percent" string;
plt.pie(quants, explode=expl, colors=colors, labels=labels, autopct='%1.1f%%',pctdistance=0.8, shadow=True)
plt.title('Top 10 GDP Countries', bbox={'facecolor':'0.8', 'pad':5})
plt.show()
def draw_bar(labels,quants):
width = 0.4
ind = np.linspace(0.5,9.5,10)
# make a square figure
fig = plt.figure(1)
ax  = fig.add_subplot(111)
# Bar Plot
ax.bar(ind-width/2,quants,width,color='green')
# Set the ticks on x-axis
ax.t_xticks(ind)
ax.t_xticklabels(labels,rotation=120) # 旋转标签,避免标签重叠覆盖
自身优缺点# labels
ax.t_xlabel('Country')
ax.t_ylabel('GDP (Billion US dollar)')
# title
ax.t_title('Top 10 GDP Countries', bbox={'facecolor':'0.8', 'pad':5})
plt.show()
def draw_line(labels,quants):
ind = np.linspace(0,9,10)
fig = plt.figure(1)
ax  = fig.add_subplot(111)
梦见与别人吵架ax.plot(ind,quants)
ax.t_title('Top 10 GDP Countries', bbox={'facecolor':'0.8', 'pad':5})
ax.t_xticklabels(labels)
樱美林大学plt.show()
# quants: GDP
# labels: country name
labels  = ['USA', 'China', 'India', 'Japan', 'Germany', 'Russia', 'Brazil', 'UK', 'France', 'Italy']
quants  = [15094025.0, 11299967.0, 4457784.0, 4440376.0, 3099080.0, 2383402.0, 2293954.0, 2260803.0, 2217900.0,1846950.0]
draw_pie(labels,quants)
#draw_bar(labels,quants)
#draw_line(labels,quants)
DataFrame.plot( )函数
DataFrame.plot(x=None, y=None, kind='line', ax=None, subplots=Fal,
sharex=None, sharey=Fal, layout=None,figsize=None,
u_index=True, title=None, grid=None, legend=True,
style=None, logx=Fal, logy=Fal, loglog=Fal,
xticks=None, yticks=None, xlim=None, ylim=None, rot=None,
xerr=None,condary_y=Fal, sort_columns=Fal, **kwds)
参数详解如下:
Parameters:
x : label or position, default None#指数据框列的标签或位置参数
y : label or position, default None
kind : str
‘line’ : line plot (default)#折线图
‘bar’ : vertical bar plot#条形图
‘barh’ : horizontal bar plot#横向条形图
‘hist’ : histogram#柱状图
‘box’ : boxplot#箱线图
‘kde’ : Kernel Density Estimation plot#Kernel 的密度估计图,主要对柱状图添加Kernel 概率密度线
‘density’ : same as ‘kde’
‘area’ : area plot#不了解此图
‘pie’ : pie plot#饼图
‘scatter’ : scatter plot#散点图  需要传⼊columns⽅向的索引
‘hexbin’ : hexbin plot#不了解此图
ax : matplotlib axes object, default None#**⼦图(axes, 也可以理解成坐标轴) 要在其上进⾏绘制的mat报考科类
plotlib subplot对象。如果没有设置,则使⽤当前matplotlib subplot**其中,变量和函数通过改变figure和axes中的元素(例如:title,label,点和线等等)⼀起描述figure和axes,也就是在画布上绘图。
subplots : boolean, default Fal#判断图⽚中是否有⼦图
Make parate subplots for each column
sharex : boolean, default True if ax is None el Fal#如果有⼦图,⼦图共x轴刻度,标签
In ca subplots=True, share x axis and t some x axis labels to invisible; defaults to True if ax is None otherwi Fal if an ax is pasd in; Be aware, that passing in both an ax and sharex=True will alter all x axis labels for all axis in a figure!
sharey : boolean, default Fal#如果有⼦图,⼦图共y轴刻度,标签
In ca subplots=True, share y axis and t some y axis labels to invisible
layout : tuple (optional)#⼦图的⾏列布局
(rows, columns) for the layout of subplots
figsize : a tuple (width, height) in inches#图⽚尺⼨⼤⼩
u_index : boolean, default True#默认⽤索引做x轴
U index as ticks for x axis
title : string#图⽚的标题⽤字符串
Title to u for the plot
grid : boolean, default None (matlab style default)#图⽚是否有⽹格
Axis grid lines
legend : Fal/True/’rever’#⼦图的图例,添加⼀个subplot图例(默认为True)
Place legend on axis subplots
style : list or dict#对每列折线图设置线的类型
matplotlib line style per column
logx : boolean, default Fal#设置x轴刻度是否取对数
U log scaling on x axis
logy : boolean, default Fal
U log scaling on y axis
loglog : boolean, default Fal#同时设置x,y轴刻度是否取对数
U log scaling on both x and y axes
xticks : quence#设置x轴刻度值,序列形式(⽐如列表)
Values to u for the xticks
开水冲蛋花技巧yticks : quence#设置y轴刻度,序列形式(⽐如列表)
Values to u for the yticks
xlim : 2-tuple/list#设置坐标轴的范围,列表或元组形式
ylim : 2-tuple/list
rot : int, default None#设置轴标签(轴刻度)的显⽰旋转度数
Rotation for ticks (xticks for vertical, yticks for horizontal plots)
fontsize : int, default None#设置轴刻度的字体⼤⼩
Font size for xticks and yticks
colormap : str or matplotlib colormap object, default None#设置图的区域颜⾊
Colormap to lect colors from. If string, load colormap with that name from matplotlib.
colorbar : boolean, optional  #图⽚柱⼦
If True, plot colorbar (only relevant for ‘scatter’ and ‘hexbin’ plots)
position : float
Specify relative alignments for bar plot layout. From 0 (left/bottom-end) to 1 (right/top-end). Default is 0.5 (center)
layout : tuple (optional)  #布局
(rows, columns) for the layout of the plot
table : boolean, Series or DataFrame, default Fal  #如果为正,则选择DataFrame类型的数据并且转换匹配matplotlib的布局。
If True, draw a table using the data in the DataFrame and the data will be transpod to meet matplotlib’s default layout. If a Series or DataFrame is pasd, u pasd data to draw a table.
yerr : DataFrame, Series, array-like, dict and str
花溪湖See Plotting with Error Bars for detail.
xerr : same types as yerr.
stacked : boolean, default Fal in line and
红膏大闸蟹bar plots, and True in area plot. If True, create stacked plot.
sort_columns : boolean, default Fal  # 以字母表顺序绘制各列,默认使⽤前列顺序
婴儿儿歌0至1岁condary_y : boolean or quence, default Fal  ##设置第⼆个y轴(右y轴)
Whether to plot on the condary y-axis If a list/tuple, which columns to plot on condary y-axis
mark_right : boolean, default True
When using a condary_y axis, automatically mark the column labels with “(right)” in the legend
kwds : keywords
Options to pass to matplotlib plotting method

本文发布于:2023-06-04 13:25:30,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/860494.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:标签   设置   刻度   实现   形式   绘制   添加   顺序
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图