echarts 折线图 设置y轴最小刻度_用plotly和plt画图的基本设置(标题、坐标轴、图例、注释、图像)...

参考链接:用plotly和plt画图的基本设置(标题、坐标轴、图例、注释、图像) - weixin_41670527的博客 - CSDN博客

非常好的文章,值得推荐。

一 用matplotlib.pyplot/或者seaborn画图

import 

''' matplotlib的图像都位于Figure对象中,不能通过空Figure绘图,必须用add_subplot创建一个或多个subplot才行: 创建包含subplot网格的figure是一个非常常见的任务,matplotlib有一个更为方便的方法plt.subplots, 它可以创建一个新的Figure,并返回一个含有已创建的subplot对象的NumPy数组 必须调用plt.legend(或使用ax.legend,如果引用了轴的话)来创建图例,无论绘图时是否传递label标签选项。 '''

#一图像figure
##首先新建一个图像(figure)和图(axes),图像figure包含多个或者一个axes(图)
fig, ax = plt.subplot(figsize=(图像宽度,图像高度))

##调整图axes到图像边缘的距离
fig.subplots_adjust(left=0.05,right=0.99,bottom=0.07,top=0.95,wspace=0.1,hspace=0.1 )
##这里的数字是相对整个图像的位置,图片的最低端是0,最左边是0

##存储图像到本地
fig.savefig('figpath.png', dpi=400, bbox_inches='tight')

'''文件类型是通过文件扩展名推断出来的。因此,如果你使用的是.pdf,就会得到一个PDF文件。发布图片时最常用到两个重要的选项是dpi(控制“每英寸点数”分辨率)

bbox_inches(可以剪除当前图表周围的空白部分)。要得到一张带有最小白边且分辨率为400DPI的PNG图片,可以向上一样设置:

'''

#二图axes
##图的标签
ax.set_title(label='', fontdict= , loc='center', pad= , **kwargs)

###fontdict的参数
fontdict = {'fontsize': plt.rcParams['axes.titlesize'], 
    'fontweight' : plt.rcParams['axes.titleweight'], 
    'verticalalignment': 'baseline', 
    'horizontalalignment': 'loc'}

titlefont = {'family': 'Microsoft YaHei', 'color':  'darkcyan',
             'weight': 'normal','size': 18}
##图的坐标轴
###坐标轴的标签
ax.set_xlabel(xlabel= ,fontdict= ,labelpad= ,)

###坐标轴刻度的范围
ax.xlim()
ax.xlim(0,100):x轴的刻度最小在0,最大在100

###坐标轴的刻度值
ax.set_xticks()
ax.set_xticks([0, 250, 500, 750, 1000])#要将x轴的刻度放在数据范围中的哪些位置

###坐标轴刻度值的标签
ax.set_xticklabels()
ax.set_xticklabels(['one', 'two', 'three', 'four', 'five'],#设置刻度对应的标签
                   rotation=30, fontsize='small')#rotation选项设定x刻度标签倾斜30度。
ax.xaxis.set_ticks_position('bottom')

###可批量设置这些参数
props = {
    'title': '',
    'xlabel': ''
}
ax.set(**props)

##图例:用于标识图表元素的重要工具。
ax.legend()或plt.legend()来自动创建图例
ax.legend(handles= ,labels= ,loc='best',)#参数loc指定图例放的位置
#handles: sequence of Artist, optional A list of Artists (lines, patches) to be added to the legend. 
#labels: sequence of strings, optional A list of labels to show next to the artists. 
#loc: 图例的位置,int or string or pair of floats, default: ‘upper right’ The location of the legend. Possible codes are:

##注解以:及在Subplot上绘图,text可以将文本绘制在图表的指定坐标(x,y),还可以加上一些自定义格式:
ax.text(x= , y= , s= ,fontdict= ,withdash=False)
#x,y:注释的x,y坐标
#s:注释的内容
#fontdict#设置注释的字体及颜色
#withdash:是否创建TextWithDash实例,
'''
TextWithDash的具体参数如下:
(x=0, y=0, text='', color=None, verticalalignment='center', 
horizontalalignment='center', multialignment=None, fontproperties=None, rotation=None, linespacing=None, dashlength=0.0, 
dashdirection=0, dashrotation=None, dashpad=3, dashpush=0)

text实例具体参数如下:
(x=0, y=0, text='', color=None, verticalalignment='baseline', horizontalalignment='left', 
multialignment=None, fontproperties=None, rotation=None, linespacing=None, rotation_mode=None, 
usetex=None, wrap=False, **kwargs)


ax.annotate(s= ,xy= ,xytext= ,)
#s: 注释的内容 
#xy: 可迭代的元组(x,y),箭头的坐标
#xytext: 注释的坐标
#xycoords: str, Artist, Transform, callable or tuple, optional The coordinate system that xy is given in. 
#For a str the allowed values are:
#textcoords 
#arrowprops:监听的参数设置
#annotation_clip:当注释超出轴区域时,控制注释的可见性。

除标准的绘图类型,你可能还希望绘制一些子集的注解,可能是文本、箭头或其他图形等。
注解和文字可以通过text、arrow和annotate函数进行添加。
'''
##画图
ax.bar(x= ,y= ,)

二.用plotly画图

import plotly
import plotly.graph_objs as go
import plotly.offline as py  #设置离线画图

#设置第一坐标轴画的条形图
trace0 = go.Bar(
    y=count_unit_2018.index,
    x=count_unit_2018,
    #设置图形的颜色外观等
    marker=dict(color='#483D8B',#设置条形图的颜色
        line=dict(color='rgb(256, 256, 256)',width=1.0,)),#设置条形图边框
    name='总次数',#设置这个图的名字,和图例对应
    orientation='h',#如果水平条形图需设置,竖直条形图不用设置
    opacity=0.9)#条形图颜色的不透明度
#设置第二坐标轴画的散点图
trace1 = go.Scatter(
        y=count_unit_2018.index,
        x=round(count_unit_2018/130),
        text=round(count_unit_2018/130),#设置数值标签的值
        textposition='right center',#设置数值标签的位置
        #散点图特有的参数mode
        mode='text',#设置画图的种类,有'markers+text'、 mode='lines+markers+text',等各种组合
        textfont=dict(size=32,color='balck'),#设置标签的字体
        marker=dict(size=32,color='black',
                    line=dict(width=1,color='black'),),
        name = '日均次数',
        xaxis='x2')#如果不是第二坐标轴不用设置,如果是纵向的图,设置成yaxis='y2'
#组合所有图像展示的图
data = [trace0,trace1]

#设置图层
layout = go.Layout(
    plot_bgcolor='#E6E6FA',#图的背景颜色
    paper_bgcolor='#F8F8FF',#图像的背景颜色
    autosize=False,width=1450,height=800,#设置图像的大小
    #设置图离图像四周的边距
    margin=go.Margin(l=480,r=60,b=50,t=60,pad=0),#pad参数是刻度与标签的距离
    #设置y轴的刻度和标签
    yaxis=dict(title='人均会议申请次数',#设置坐标轴的标签
            titlefont=dict(color='rgb(148, 103, 189)',size=24),#设置坐标轴标签的字体及颜色
            tickfont=dict(color='rgb(148, 103, 189)',size = 24,),#设置刻度的字体大小及颜色
            showticklabels=False,#设置是否显示刻度
            #设置刻度的范围及刻度
            autorange=False,range=[-0.05674507980728292, -0.0527310420933204],type='linear',
            ),

    #设置x轴的刻度和标签    
    xaxis=dict(title='人均会议申请次数',#设置坐标轴的标签
            titlefont=dict(color='rgb(148, 103, 189)',size=24),
            tickfont=dict(color='rgb(148, 103, 189)',size = 24,),
            tickangle=270,#设置刻度旋转的角度
            showticklabels=False,#设置是否显示坐标轴
            #设置刻度的范围及刻度
            autorange=False,range=[-0.05674507980728292, -0.0527310420933204],type='linear',
            ),

    #设置第二坐标轴,如果第二坐标轴是纵向,设置yaxis2
    xaxis2=dict(overlaying='x',#设置第二坐标轴的在的方向,如果第二坐标轴是纵向,设置为'y'
            side='top',#设置第二坐标轴的位置,或者是'bottom',如果第二坐标轴是纵向,设置为'right'或者'left'
            title='人均会议申请次数',#设置坐标轴的标签
            titlefont=dict(color='rgb(148, 103, 189)',size=24),
            tickfont=dict(color='rgb(148, 103, 189)',size = 24,),
            tickangle=270,#设置刻度旋转的角度
            showticklabels=False,#设置是否显示该坐标轴
            #设置刻度的范围及刻度
            autorange=False,range=[-0.05674507980728292, -0.0527310420933204],type='linear',
            ),

    #设置图例
    legend=dict(x=0.5,y=0.8,#设置图例的位置,[0,1]之间
        font=dict(family='sans-serif',size=26,color='black'),#设置图例的字体及颜色
        bgcolor='#E2E2E2',bordercolor='#FFFFFF'),#设置图例的背景及边框的颜色
    showlegend=False,#设置不显示图例
    annotations=[#注释可以是列表,也可以是单个字符串
    #设置注释1
    dict(x=2,y=5,
        xref='x',yref='y',
        text='dict Text',
        #设置注释的字体参数
        font=dict(family='Courier New, monospace',size=16,color='#ffffff'),
        showarrow=True,#设置显示箭头
        #设置箭头的参数
        ax=20,ay=-30,align='center',arrowhead=2,arrowsize=1,arrowwidth=2,arrowcolor='#636363',
        #设置注释的边框
        bordercolor='#c7c7c7',borderwidth=2,borderpad=4,bgcolor='#ff7f0e',opacity=0.8),
    #设置注释2
    dict(x=2,y=5,
        xref='x',yref='y',
        text='dict Text',
        #设置注释的字体参数
        font=dict(family='Courier New, monospace',size=16,color='#ffffff'),
        showarrow=True,#设置显示箭头
        #设置箭头的参数
        ax=20,ay=-30,align='center',arrowhead=2,arrowsize=1,arrowwidth=2,arrowcolor='#636363',
        #设置注释的边框
        bordercolor='#c7c7c7',borderwidth=2,borderpad=4,bgcolor='#ff7f0e',opacity=0.8)]

三.整合层图

#整合图和图层
fig = go.Figure(data=data, layout=layout)
#画图
py.plot(fig,filename='D:/2018上半年.html',#会生成一个网页文件
    image='png',)#设置保存的文件类型,不会在本地有个png的文件,需要在生成的网页打开另存为png的文件

四.补充(刻度和字体大小,在图形中显示数据)

#设置图例并且设置图例的字体及大小
font1 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size'   : 23,
}
legend = plt.legend(handles=[A,B],prop=font1)
 
#设置坐标刻度值的大小以及刻度值的字体
plt.tick_params(labelsize=23)
labels = ax.get_xticklabels() + ax.get_yticklabels()
[label.set_fontname('Times New Roman') for label in labels]
 
#设置横纵坐标的名称以及对应字体格式
font2 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size'   : 30,
}
plt.xlabel('round',font2)
plt.ylabel('value',font2)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值