会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132661个问题

代码:

from matplotlib import pyplot as plt
fig,axs=plt.subplots(2,2,figsize=(7,7))
people=[45, 48, 37, 47, 38, 41, 39, 37, 44, 44, 37, 35, 38, 46, 33, 35, 37, 36, 34, 38, 33, 33, 40, 37, 38]
bin_width=2
bin_count=int(max(people)-min(people)/bin_width)
x_ticks=range(31,52)
axs[0,0].xaxis.set_ticks(x_ticks)
axs[0,0].hist(people,bin_count)
real_names=["detection","allen","home"]
real_num1=[8414,4024,2088]
real_num2=[11526,5605,2490]
real_num3=[7675,2863,1311]
x_pos1=range (3)
x_pos2=[i+0.3 for i in x_pos1]
x_pos3=[i+2*0.3 for i in x_pos1]
axs[1,0].xaxis.set_ticks(x_pos2)
# axs[1,0].set_xticks(x_pos2)
# axs[1,0].set_xticklabels(x_pos2,real_names)
# plt.xticks(x_pos1,real_names)
# plt.xticks(x_pos2,real_names)
# plt.xticks(x_pos3,real_names)
# plt.xticks(x_pos2,real_names)
axs[1,0].bar(x_pos1,real_num1,width=0.3,label="dection")
axs[1,0].bar(x_pos2,real_num2,width=0.3,label="allen")
axs[1,0].bar(x_pos3,real_num3,width=0.3,label="home")
axs[1,0].legend(loc="upper right")
x=range(1,13)
x_ticks=range(1,13)
b=[]
for i in x:
    y=2*i+1
    b.append(y)
axs[1,1].xaxis.set_ticks(x)
axs[1,1].scatter(x,b)
# plt.show()
plt.show()
# plt.show()

屏幕截图 2021-04-06 123941.png

运行结果:

屏幕截图 2021-04-06 124029.png

老师请问一下,在绘制子图的时候怎么样对柱图的横坐标进行修改,把横坐标修改成real_names列表中的内容,就像

plt.xticks(x_pos2,real_names)

一样,我网上查了一下说可以使用

axs[1,0].xaxis.set_ticks(x_pos2)

但是这种方法只可以修改x轴的间距,没办法指定每个间隔显示的文本,请问老师这该如何解决?

Python 全系列/第十七阶段:数据分析-数据可视化/matplotlib 1726楼
Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/虚拟环境 1727楼

image.png

JAVA 全系列/第十一阶段:消息中间件与高并发处理/Nginx 1728楼
Python 全系列/第十四阶段:Python 爬虫开发/scrapy框架使用 1729楼
JAVA 全系列/第十阶段:百战旅游网项目/百战旅游网 1730楼
JAVA 全系列/第十三阶段:分布式文件存储与数据缓存/MongoDB 1732楼

#测试Text多行文本
from tkinter import *
import webbrowser

class Application(Frame):

    def __init__(self,master=None):
        super().__init__(master)
        self.master=master
        self.pack()
        self.createWidget()

    def createWidget(self):   #定义框内容以及属性

        self.w = Text(root,width=40,height=10,bg="white",font=("宋体",18))
        self.w.pack()
        #self.w.insert(1.0)代表在第1行0列插入内容
        #行:123...;列:012...
        self.w.insert(1.0,"0123456789\n0纵观世界与天下\n奇闻异事")
        self.w.insert(2.3, "xxx")

        Button(self, text="重复插入文本", command=self.insertText).pack(side="left")
        Button(self, text="命令行返回文本", command=self.returnText).pack(side="left")
        Button(self, text="添加图片", command=self.addImage).pack(side="left")
        Button(self, text="添加组件", command=self.addWidget).pack(side="left")
        Button(self, text="通过tag精确控制文本", command=self.TextTag).pack(side="left")  #Tag标记


    def insertText(self):
        #INSERT代表从光标处插入内容
        #END在末尾插入内容
        self.w.insert(INSERT,"HE")
        self.w.insert(END, "ST")
        #在指定位置插入指定文本内容
        self.w.insert(2.8,"acz")   #在第2行第8列的位置插入

    def returnText(self):
        #获取文本的内容,并在命令行打印输出,get(初始位置, 终止位置)--获得该区域内容
        print(self.w.get(2.0, 2.5))
        print("文本所有内容:" + self.w.get(1.0, END))

    def addImage(self):
        #添加图片image_create()
        #定义全局变量globle p 或 对象属性self.p
        #global p
        self.p = PhotoImage(file="imgs/13.gif")
        self.w.image_create(END, image=self.p)

    def addWidget(self):
        #添加组件window_create()
        b=Button(self.w,text="点击")  #master为建好的文本域self.w
        self.w.window_create(INSERT,window=b)

    def TextTag(self):
        self.w.delete(1.0,END)
        self.w.insert(1.0,"good dood study!\n尚学堂\n百度一下,你就知道")
        #Tag--标记
        self.w.tag_add("IM",1.0,1.5)   #在指定区域内添加标记,将标记命名为good
        #标记属性定义,标记backgrond背景色,foreground标记体颜色,underline下划线
        self.w.tag_config("IM",background="yellow",foreground="red",underline=True)
        #标记事件绑定,即点击会出现的情况
        self.w.tag_add("bd", 3.0, 3.2)  # 在指定区域内添加标记
        self.w.tag_config("bd",background="blue",underline=True)
        self.w.tag_bind("bd","<Button-1>",self.webshow)  #<Button-1>点击左键

    #定义参数event,将<Button-1>点击左键作为参数event传入
    def webshow(self,event):
        webbrowser.open("https://www.baidu.com/")


if __name__=="__main__":
    root=Tk()                        #创建框大小、位置以及标题
    app=Application(master=root)
    root.title("Text_test")
    root.geometry("500x300+450+200")
    root.mainloop()

老师,请问这个为什么这个程序中图片部分的不能显示,点了没反应是为什么呢?没有图片出来?


Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1733楼

image.png

java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support.
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
	at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
	at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
	at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197)
	at org.mybatis.generator.internal.db.ConnectionFactory.getConnection(ConnectionFactory.java:68)
	at org.mybatis.generator.config.Context.getConnection(Context.java:526)
	at org.mybatis.generator.config.Context.introspectTables(Context.java:436)
	at org.mybatis.generator.api.MyBatisGenerator.generate(MyBatisGenerator.java:222)
	at org.mybatis.generator.api.MyBatisGenerator.generate(MyBatisGenerator.java:133)
	at GeneratorSqlmap.generator(GeneratorSqlmap.java:27)
	at GeneratorSqlmap.main(GeneratorSqlmap.java:33)
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
	at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
	at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132)
	at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2120)
	at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2143)
	at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1310)
	at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:967)
	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826)
	... 10 more

我运行的时候就这个样子了,我的数据库是8的,jar包也换了。

JAVA 全系列/第六阶段:项目管理与SSM框架/Mybatis 1735楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库 1736楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637